Import, export, process, analyze and view triangular meshes.
Essential mesh creation, loading, and basic geometric operations that form the foundation for all mesh processing workflows in trimesh.
Load meshes from files or create them from vertex and face arrays.
def load(file_obj, file_type=None, resolver=None, **kwargs):
"""
Load mesh from file with automatic format detection.
Parameters:
- file_obj: file path, file-like object, or URL
- file_type: str, force specific file type
- resolver: function to resolve referenced assets
- **kwargs: format-specific loading options
Returns:
Trimesh, Scene, or Path object depending on file content
"""
def load_mesh(file_obj, **kwargs) -> Trimesh:
"""
Load file specifically as Trimesh object.
Parameters:
- file_obj: file path or file-like object
- **kwargs: loading options
Returns:
Trimesh object
"""Core properties providing essential geometric information about the mesh.
class Trimesh(Geometry3D):
"""Main triangular mesh class"""
@property
def vertices(self) -> np.ndarray:
"""Vertex coordinates as (n, 3) float array"""
@property
def faces(self) -> np.ndarray:
"""Face indices as (m, 3) int array"""
@property
def face_normals(self) -> np.ndarray:
"""Face normal vectors as (m, 3) array"""
@property
def vertex_normals(self) -> np.ndarray:
"""Vertex normal vectors as (n, 3) array"""
@property
def bounds(self) -> np.ndarray:
"""Axis-aligned bounding box as (2, 3) array"""
@property
def extents(self) -> np.ndarray:
"""Size in each dimension as (3,) array"""
@property
def centroid(self) -> np.ndarray:
"""Geometric center as (3,) array"""
@property
def center_mass(self) -> np.ndarray:
"""Center of mass as (3,) array"""
@property
def volume(self) -> float:
"""Mesh volume (positive for watertight meshes)"""
@property
def area(self) -> float:
"""Surface area"""
@property
def mass(self) -> float:
"""Mass (volume * density)"""
@property
def density(self) -> float:
"""Material density"""
@density.setter
def density(self, value: float) -> None:
"""Set material density"""Check mesh properties and quality metrics.
@property
def is_watertight(self) -> bool:
"""True if mesh is watertight (manifold and closed)"""
@property
def is_winding_consistent(self) -> bool:
"""True if face winding is consistent"""
@property
def is_volume(self) -> bool:
"""True if mesh bounds a volume"""
@property
def euler_number(self) -> int:
"""Euler characteristic (V - E + F)"""Apply transformations to modify mesh position, orientation, and scale.
def apply_transform(self, matrix: np.ndarray) -> 'Trimesh':
"""
Apply 4x4 transformation matrix to mesh.
Parameters:
- matrix: (4, 4) transformation matrix
Returns:
Self for method chaining
"""Create copies and modify mesh structure.
def copy(self, include_cache=False, include_visual=True) -> 'Trimesh':
"""
Create a copy of the mesh.
Parameters:
- include_cache: bool, copy cached properties
- include_visual: bool, copy visual properties
Returns:
New Trimesh object
"""
def update_vertices(self, vertices: np.ndarray) -> None:
"""
Update vertex positions.
Parameters:
- vertices: (n, 3) new vertex coordinates
"""
def update_faces(self, faces: np.ndarray) -> None:
"""
Update face connectivity.
Parameters:
- faces: (m, 3) new face indices
"""
def rezero(self) -> 'Trimesh':
"""Center mesh at origin and reset cached properties"""Split meshes into components or combine multiple meshes.
def split(self, only_watertight=True, adjacency=None) -> list:
"""
Split mesh into separate components.
Parameters:
- only_watertight: bool, only return watertight components
- adjacency: face adjacency matrix (computed if not provided)
Returns:
List of Trimesh objects for each component
"""import trimesh
import numpy as np
# Load and examine a mesh
mesh = trimesh.load('model.stl')
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"Is watertight: {mesh.is_watertight}")
# Apply transformations using transformation matrices
translation_matrix = trimesh.transformations.translation_matrix([1.0, 0.0, 0.5])
mesh.apply_transform(translation_matrix)
# Scale mesh to unit volume using transformation matrix
target_volume = 1.0
scale_factor = (target_volume / mesh.volume) ** (1/3)
scale_matrix = trimesh.transformations.scale_matrix(scale_factor)
mesh.apply_transform(scale_matrix)
# Split into components
components = mesh.split()
print(f"Found {len(components)} components")
# Create mesh from arrays
vertices = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
faces = np.array([[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]])
tetrahedron = trimesh.Trimesh(vertices=vertices, faces=faces)# Check mesh quality
if mesh.is_watertight:
print("Mesh is watertight")
print(f"Volume: {mesh.volume}")
else:
print("Mesh has holes or non-manifold edges")
# Check winding consistency
if not mesh.is_winding_consistent:
print("Face winding is inconsistent")
# Get mesh statistics
print(f"Euler number: {mesh.euler_number}")
print(f"Bounds: {mesh.bounds}")
print(f"Extents: {mesh.extents}")
print(f"Centroid: {mesh.centroid}")Install with Tessl CLI
npx tessl i tessl/pypi-trimesh