SyncAI.news, a Varaisys broadcasting
Converting Vertex-Colored Meshes to Textured Meshes
HF

Hugging Face Blog

· 1 min read

AI LabsHugging Face Blog

Converting Vertex-Colored Meshes to Textured Meshes

Convert vertex-colored meshes to UV-mapped, textured meshes.

Introduction

Vertex colors are a straightforward way to add color information directly to a mesh's vertices. This is often the way generative 3D models like InstantMesh produce meshes. However, most applications prefer UV-mapped, textured meshes.

This tutorial walks through a quick solution to convert vertex-colored meshes to UV-mapped, textured meshes. This includes The Short Version to get results quickly, and The Long Version for an in-depth walkthrough.

The Short Version

Install the InstantTexture library for easy conversion. This is a small library we wrote that implements the steps described in The Long Version below.

pip install git+https://github.com/dylanebert/InstantTexture

Usage

The code below converts a vertex-colored .obj mesh to a UV-mapped, textured .glb mesh and saves it to output.glb.

from instant_texture import Converter

input_mesh_path = "https://raw.githubusercontent.com/dylanebert/InstantTexture/refs/heads/main/examples/chair.obj"

converter = Converter()
converter.convert(input_mesh_path)

Let's visualize the output mesh.

import trimesh

mesh = trimesh.load("output.glb")
mesh.show()

That's it!

For a detailed walkthrough, continue reading.

The Long Version

Install the following dependencies:

  • numpy for numerical operations
  • trimesh for loading and saving mesh data
  • xatlas for generating uv maps
  • Pillow for image processing
  • opencv-python for image processing
  • httpx for downloading the input mesh
pip install numpy trimesh xatlas opencv-python pillow httpx

Import dependencies.

import cv2
import numpy as np
import trimesh
import xatlas
from PIL import Image, ImageFilter

Load the vertex-colored input mesh. This should be a .obj file located at input_mesh_path.

If it's a local file, use trimesh.load() instead of trimesh.load_remote().

mesh = trimesh.load_remote(input_mesh_path)
mesh.show()

Access the vertex colors of the mesh.

If this fails, ensure the mesh is a valid .obj file with vertex colors.

Original source

This story was published by Hugging Face Blog. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on huggingface.co

Similar News