Ivan Hahanov
Ivan Hahanov

Reputation: 21

How to load textured mesh in trimesh?

My final purpose is to either load the textured obj model into trimesh as a SINGLE TriangleMesh object, or convert textures into colored point cloud, so that I could just use the colors stored in vertex_colors property.

When I'm using trimesh.load, it returns me a scene with multiple geometry. When I do trimesh.load(force='mesh') call, all the textures get messed up. I also tried to load obj file with open3d and then convert it to trimesh: tm.Trimesh(np.asarray(mesh.vertices), np.asarray(mesh.triangles), vertex_normals=np.asarray(mesh.vertex_normals)

Using this approach, I'm able to get the 3d model with triangles and vertices, but I don't know how to pass open3d textures to trimesh. I know that trimesh has visual property that accounts for texturing, however I don't know how to build it from open3d textures.

Upvotes: 1

Views: 7475

Answers (1)

Realistic3D
Realistic3D

Reputation: 33

from trimesh.visual import texture, TextureVisuals
from trimesh import Trimesh

def get_texture(my_uvs, img):
    # img is PIL Image
    uvs = my_uvs
    material = texture.SimpleMaterial(image=img)    
    texture = TextureVisuals(uv=uvs, image=img, material=material)

my_uvs = [....] # 2d array list
vertices = [....] # 3d array list
faces = [....] # indices list
face_normals = [....] # 3d array list
texture_visual = get_texture(my_uvs, img)
mesh = Trimesh(
            vertices=vertices,
            faces=faces,
            face_normals=face_normals,
            visual=texture_visual,
            validate=True,
            process=False
        )

Upvotes: 0

Related Questions