Debian Buster image with trimesh and dependencies.
8.0K
Trimesh is a pure Python (2.7- 3.3+) library for loading and using triangular meshes with an emphasis on watertight surfaces. The goal of the library is to provide a full featured and well tested Trimesh object which allows for easy manipulation and analysis, in the style of the Polygon object in the Shapely library.
The API is mostly stable, but this should not be relied on and is not guaranteed; install a specific version if you plan on deploying something using trimesh as a backend.
Pull requests are appreciated and responded to promptly! If you'd like to contribute, here is an up to date list of potential enhancements although things not on that list are also welcome. Here are some tips for writing mesh code in Python.
The minimal requirements to import trimesh are numpy, scipy and networkx. Installing other packages mentioned adds functionality but is not required.
For the easiest install with only these minimal dependencies pip can generally install trimesh cleanly on Windows, Linux, and OSX:
pip install trimesh
For more functionality, like faster ray queries (pyembree), vector path handling (shapely and rtree), preview windows (pyglet), faster cache checks (xxhash) and more, the easiest way to get a full trimesh install is a conda environment:
# this will install all soft dependencies available on your current platform
conda install -c conda-forge trimesh
If you're feeling lucky, you can try:
# will try to install things that aren't too tricky
pip install trimesh[easy]
# will try to install everything
pip install trimesh[all]
Further information is available in the advanced installation documentation.
Here is an example of loading a mesh from file and colorizing its faces. Here is a nicely formatted ipython notebook version of this example. Also check out the cross section example or possibly the integration of a function over a mesh example.
import numpy as np
import trimesh
# attach to logger so trimesh messages will be printed to console
trimesh.util.attach_to_log()
# load a file by name or from a buffer
mesh = trimesh.load('../models/featuretype.STL')
# is the current mesh watertight?
mesh.is_watertight
# what's the euler number for the mesh?
mesh.euler_number
# 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
print(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?
mesh.moment_inertia
# 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()
# 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()
# preview mesh in an opengl window if you installed pyglet with pip
mesh.show()
# transform method can be passed a (4,4) matrix and will cleanly apply the transform
mesh.apply_transform(trimesh.transformations.random_rotation_matrix())
# axis aligned bounding box is available
mesh.bounding_box.extents
# a minimum volume oriented bounding box also available
# primitives are subclasses of Trimesh objects which automatically generate
# faces and vertices from data stored in the 'primitive' attribute
mesh.bounding_box_oriented.primitive.extents
mesh.bounding_box_oriented.primitive.transform
# show the mesh appended with its oriented bounding box
# the bounding box is a trimesh.primitives.Box object, which subclasses
# Trimesh and lazily evaluates to fill in vertices and faces when requested
# (press w in viewer to see triangles)
(mesh + mesh.bounding_box_oriented).show()
# 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)
Trimesh includes an optional pyglet based viewer for debugging and inspecting. In the mesh view window, opened with mesh.show(), the following commands can be used:
mouse click + drag rotates the viewctl + mouse click + drag pans the viewmouse wheel zoomsz returns to the base vieww toggles wireframe modec toggles backface cullingf toggles between fullscreen and windowed modem maximizes the windowq closes the windowa toggles an XYZ-RGB axis marker between three states: off, at world frame, or at every frameIf called from inside a jupyter notebook, mesh.show() displays an in-line preview using three.js to display the mesh or scene. For more complete rendering (PBR, better lighting, shaders, better off-screen support, etc) pyrender is designed to interoperate with trimesh objects.
If you want to deploy something in a container that uses trimesh, automated builds containing trimesh and its dependencies are available on Docker Hub:
docker pull mikedh/trimesh
Content type
Image
Digest
Size
484.2 MB
Last updated
over 4 years ago
docker pull mikedh/trimesh