| name | trimesh-geometry |
| description | Load, analyze, and manipulate 3D meshes using trimesh library. Use for mesh boolean operations, ray tracing, convex hulls, mesh repair, and export to multiple formats. Comprehensive mesh processing toolkit. |
Trimesh Geometry
Full-featured mesh processing with trimesh.
Installation
pip install trimesh[all] # Full installation with optional dependencies
Loading and Saving
import trimesh
# Load mesh
mesh = trimesh.load('model.stl')
# Load with specific format
mesh = trimesh.load('model.obj', file_type='obj')
# Save mesh
mesh.export('output.stl')
mesh.export('output.ply')
mesh.export('output.glb') # GLTF binary
Creating Primitives
import trimesh
# Box
box = trimesh.creation.box(extents=[10, 20, 5])
# Cylinder
cylinder = trimesh.creation.cylinder(radius=5, height=20)
# Sphere
sphere = trimesh.creation.icosphere(radius=10, subdivisions=3)
# Cone
cone = trimesh.creation.cone(radius=5, height=10)
# From vertices and faces
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
Boolean Operations
import trimesh
a = trimesh.creation.box(extents=[10, 10, 10])
b = trimesh.creation.cylinder(radius=3, height=20)
# Union
union = a.union(b)
# Difference
difference = a.difference(b)
# Intersection
intersection = a.intersection(b)
Transformations
import trimesh
import numpy as np
mesh = trimesh.load('model.stl')
# Translation
mesh.apply_translation([10, 0, 0])
# Rotation (4x4 matrix)
angle = np.radians(45)
rotation = trimesh.transformations.rotation_matrix(angle, [0, 0, 1])
mesh.apply_transform(rotation)
# Scaling
mesh.apply_scale(2.0)
Mesh Analysis
import trimesh
mesh = trimesh.load('model.stl')
# Properties
print(f"Vertices: {len(mesh.vertices)}")
print(f"Faces: {len(mesh.faces)}")
print(f"Volume: {mesh.volume}")
print(f"Surface area: {mesh.area}")
print(f"Center of mass: {mesh.center_mass}")
print(f"Is watertight: {mesh.is_watertight}")
print(f"Is convex: {mesh.is_convex}")
# Bounding box
print(f"Bounds: {mesh.bounds}")
print(f"Extents: {mesh.extents}")
Mesh Repair
import trimesh
mesh = trimesh.load('broken_model.stl')
# Fix normals
mesh.fix_normals()
# Fill holes
mesh.fill_holes()
# Remove degenerate faces
mesh.remove_degenerate_faces()
# Merge duplicate vertices
mesh.merge_vertices()
# Process into watertight mesh
mesh.process(validate=True)
Ray Tracing
import trimesh
import numpy as np
mesh = trimesh.load('model.stl')
# Single ray
origin = [0, 0, 100]
direction = [0, 0, -1]
locations, index_ray, index_tri = mesh.ray.intersects_location(
ray_origins=[origin],
ray_directions=[direction]
)
# Check if point is inside mesh
points = [[0, 0, 0], [100, 100, 100]]
inside = mesh.contains(points)
Slicing
import trimesh
mesh = trimesh.load('model.stl')
# Slice at Z=5
slice_2d = mesh.section(plane_origin=[0, 0, 5], plane_normal=[0, 0, 1])
# Get 2D path
path_2d, to_3d = slice_2d.to_planar()