In [ ]:
import numpy as np
import trimesh

In [ ]:
# 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)

In [ ]:
# is the current mesh watertight?
mesh.is_watertight

In [ ]:
# what's the euler number for the mesh?
mesh.euler_number

In [ ]:
# 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)

In [ ]:
# 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

In [ ]:
# what's the moment of inertia for the mesh?
mesh.moment_inertia

In [ ]:
# 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
mesh.split()

In [ ]:
# preview mesh in a pyglet window from a terminal, or inline in a notebook
mesh.show()

In [ ]:
# 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()

In [ ]:
# transform method can be passed a (4,4) matrix and will cleanly apply the transform
mesh.apply_transform(trimesh.transformations.random_rotation_matrix())

In [ ]:
# an axis aligned bounding box is available
mesh.bounding_box.primitive.extents

In [ ]:
# a minimum volume oriented bounding box is available
mesh.bounding_box_oriented.primitive.extents

In [ ]:
mesh.bounding_box_oriented.primitive.transform

In [ ]:
# the bounding box is a trimesh.primitives.Box object, which subclasses
# Trimesh and lazily evaluates to fill in vertices and faces when requested
mesh.bounding_box_oriented.show()

In [ ]:
# bounding spheres and bounding cylinders of meshes are also
# available, and will be the minimum volume version of each
# except in certain degenerate cases, where they will be no worse
# than a least squares fit version of the primitive.
print(mesh.bounding_box_oriented.volume, 
      mesh.bounding_cylinder.volume,
      mesh.bounding_sphere.volume)