In [1]:
"""
A simple example showing various properties of `Trimesh` objects.
"""
Out[1]:
'\nA simple example showing various properties of `Trimesh` objects.\n'
In [2]:
import numpy as np
import trimesh
In [3]:
# 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 [4]:
# is the current mesh watertight?
mesh.is_watertight
Out[4]:
True
In [5]:
# what's the euler number for the mesh?
mesh.euler_number
Out[5]:
-16
In [6]:
# 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)
Out[6]:
0.7792407744466932
In [7]:
# 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 [8]:
# what's the moment of inertia for the mesh?
mesh.moment_inertia
Out[8]:
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]])
In [9]:
# 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()
Out[9]:
array([<trimesh.Trimesh(vertices.shape=(1722, 3), faces.shape=(3476, 3))>],
      dtype=object)
In [10]:
# preview mesh in a pyglet window from a terminal, or inline in a notebook
mesh.show()
Out[10]: