Trimesh
Loads and analyzes triangle meshes, with an emphasis on watertight surfaces.
- Downloads / mo
- 14.5M
- Stars
- 3.7K
- Last release
- 5.2.1
- License
- MIT
Strengths
- Computes properties of closed meshes. Compute mass properties for closed meshes with a uniform density.
- Preserves instances when loading scenes. Use load_scene when meshes or repeated instances must stay separate.
Limitations
- Simplification needs fast-simplification. Install fast-simplification before using simplify_quadric_decimation.
- Mass properties need closed meshes. Treat volume and mass properties as unreliable for open triangle surfaces.
Quickstart
import trimesh mesh = trimesh.creation.box(extents=(1.0, 2.0, 3.0)) print(mesh.volume)
Trimesh examples
def to_glb( app_rep: Union[Strivec, Gaussian], mesh: MeshExtractResult, simplify: float = 0.95, fill_holes: bool = True, fill_holes_max_size: float = 0.04, texture_size: int = 1024, debug: bool = False, verbose: bool = True, ) -> trimesh.Trimesh: """ Convert a generated asset to a glb file. Args: app_rep (Union[Strivec, Gaussian]): Appearance representation. mesh (MeshExtractResult): Extracted mesh. simplify (float): Ratio of faces to remove in simplification. fill_holes (bool): Whether to fill holes in the mesh. fill_holes_max_size (float): Maximum area of a hole to fill. texture_size (int): Size of the texture. debug (bool): Whether to print debug information. verbose (bool): Whether to print progress. """ vertices = mesh.vertices.cpu().numpy() faces = mesh.faces.cpu().numpy() # mesh postprocess vertices, faces = postprocess_mesh( vertices, faces, simplify=simplify > 0, simplify_ratio=simplify, fill_holes=fill_holes, fill_holes_max_hole_size=fill_holes_max_size, fill_holes_max_hole_nbe=int(250 * np.sqrt(1-simplify)), fill_holes_resolution=1024, fill_holes_num_views=1000, debug=debug, verbose=verbose, ) # parametrize mesh vertices, faces, uvs = parametrize_mesh(vertices, faces) # bake texture observations, extrinsics, intrinsics = render_multiview(app_rep, resolution=1024, nviews=100) masks = [np.any(observation > 0, axis=-1) for observation in observations] extrinsics = [extrinsics[i].cpu().numpy() for i in range(len(extrinsics))] intrinsics = [intrinsics[i].cpu().numpy() for i in range(len(intrinsics))] texture = bake_texture( vertices, faces, uvs, observations, masks, extrinsics, intrinsics, texture_size=texture_size, mode='opt', lambda_tv=0.01, verbose=verbose ) texture = Image.fromarray(texture) # rotate mesh (from z-up to y-up) vertices = vertices @ np.array([[1, 0, 0], [0, 0, -1], [0, 1, 0]]) material = trimesh.visual.material.PBRMaterial( roughnessFactor=1.0, baseColorTexture=texture, baseColorFactor=np.array([255, 255, 255, 255], dtype=np.uint8) ) mesh = trimesh.Trimesh(vertices, faces, visual=trimesh.visual.TextureVisuals(uv=uvs, material=material)) return mesh