Quick Start#

A simple example showing various properties of Trimesh objects.

import numpy as np
import trimesh
# load a file by name or from a buffer
mesh = trimesh.load_mesh('../models/featuretype.STL')
# to keep the raw data intact, disable any automatic processing
#mesh = trimesh.load_mesh('../models/featuretype.STL', process=False)
# is the current mesh watertight?
# what's the euler number for the mesh?
# the convex hull is another Trimesh object that is available as a property
# lets compare the volume of our mesh with the volume of its convex hull
np.divide(mesh.volume, mesh.convex_hull.volume)
# since the mesh is watertight, it means there is a
# volumetric center of mass which we can set as the origin for our mesh
mesh.vertices -= mesh.center_mass
# what's the moment of inertia for the mesh?
array([[ 6.93059627e+00, -1.43877613e-03, -1.49424850e-01],
       [-1.43877613e-03,  2.19191960e+01, -1.25194047e-04],
       [-1.49424850e-01, -1.25194047e-04,  2.62344872e+01]])
# if there are multiple bodies in the mesh we can split the mesh by
# connected components of face adjacency
# since this example mesh is a single watertight body we get a list of one mesh
array([<trimesh.Trimesh(vertices.shape=(1722, 3), faces.shape=(3476, 3), name=`featuretype.STL`)>],
# preview mesh in a pyglet window from a terminal, or inline in a notebook
# facets are groups of coplanar adjacent faces
# set each facet to a random color
# colors are 8 bit RGBA by default (n,4) np.uint8
for facet in mesh.facets:
    mesh.visual.face_colors[facet] = trimesh.visual.random_color()
# transform method can be passed a (4,4) matrix and will cleanly apply the transform
<trimesh.Trimesh(vertices.shape=(1722, 3), faces.shape=(3476, 3), name=`featuretype.STL`)>
# an axis aligned bounding box is available
TrackedArray([5.04135165, 2.99641508, 5.49556319])
# a minimum volume oriented bounding box is available
TrackedArray([1.375, 2.5  , 5.   ])
TrackedArray([[ 0.36752287,  0.73821093,  0.56566029, -0.05691765],
              [ 0.91422276, -0.17516341, -0.3653964 , -0.12780712],
              [-0.17065663,  0.65143105, -0.73926579,  0.03022871],
              [ 0.        ,  0.        ,  0.        ,  1.        ]])
# the bounding box is a trimesh.primitives.Box object, which subclasses
# Trimesh and lazily evaluates to fill in vertices and faces when requested