Trimesh
Trimesh is a Python library for loading, viewing, processing, and analyzing triangular meshes, with a strong emphasis on vectorized operations for performance. It's actively developed and frequently updated, providing a comprehensive set of tools for 3D geometry manipulation.
Warnings
- gotcha Trimesh has many optional dependencies for different features (e.g., `scipy` for algorithms, `pyrender` for visualization, `lxml` for file formats, `rtree` for spatial queries). Missing dependencies lead to `AttributeError` or `ModuleNotFoundError` when trying to use those features.
- breaking The `trimesh.visual` module and visualization backend configuration have seen significant API changes and refactors, particularly around versions 3.x and 4.x. Old visualization code, especially custom `ColorVisuals` or direct `viewer` interaction, may break.
- gotcha The `trimesh.show()` function relies on `pyglet` and an active OpenGL context, which often fails in headless environments (e.g., CI/CD, cloud instances, WSL without display server) or systems with non-standard display drivers, resulting in a blank window or errors.
Install
-
pip install trimesh -
pip install trimesh[full]
Imports
- Trimesh
import trimesh mesh = trimesh.Trimesh(...)
- load
import trimesh mesh = trimesh.load('path/to/mesh.obj') - Scene
import trimesh scene = trimesh.Scene(...)
Quickstart
import trimesh
import numpy as np
# 1. Create a simple mesh (a box)
mesh = trimesh.creation.box()
# 2. Perform basic analyses
volume = mesh.volume
area = mesh.area
inertia = mesh.moment_inertia
print(f"Mesh volume: {volume:.2f}")
print(f"Mesh surface area: {area:.2f}")
print(f"Mesh moment of inertia:\n{inertia}")
# 3. Load a remote mesh (requires network access)
try:
# Using a known stable URL for demonstration
mesh_loaded = trimesh.load_remote('https://raw.githubusercontent.com/mikedh/trimesh/main/models/feature.stl')
print(f"Loaded remote mesh, volume: {mesh_loaded.volume:.2f}")
except Exception as e:
print(f"Could not load remote mesh: {e}. Check internet connection or URL.")