trimesh

repository·main·Indexed 25 days ago

https://github.com/mikedh/trimesh

A pure Python 3.10+ library for importing, exporting, processing, analyzing, and viewing triangular meshes, with a strong focus on watertight surfaces. It provides a feature-rich API for geometric analysis, mesh repair, transformations, and ray-mesh queries. Supported formats include GLB/GLTF, STL, OBJ, PLY, 3MF, and others. The library supports OpenGL previews via pyglet and in-line visualization in Jupyter/marimo notebooks via three.js.

Tokens
28K
Snippets
36
Records
256
Agent score
86%

What's inside trimesh

  1. Overview of trimesh features

    main

    trimesh is a pure Python 3.10+ library for loading and using triangular meshes, with an emphasis on watertight surfaces.

    Key capabilities include:

    • Import/Export: Supports STL, OBJ, PLY, GLTF/GLB, 3MF, XAML, 3DXML, and more.
    • Analysis: Calculate volume, center of mass, moment of inertia, watertightness, and convexity.
    • Geometry Operations: Convex hulls, slicing, boolean operations (via Manifold3D or Blender), voxelization, and smoothing.
    • Queries: Ray-mesh queries and nearest point calculations.
    • Primitives: Built-in support for Box, Cylinder, Sphere, and Extrusion objects.
    • Visualization: OpenGL preview via pyglet or in-line in Jupyter/marimo notebooks via three.js.
  2. Install trimesh in a Docker container using pip

    main

    To create a lightweight Docker image, use an official first-party base image (such as python:3.11-slim-bullseye) and install trimesh with the [easy] extra via pip.

    FROM python:3.11-slim-bullseye
    RUN pip install trimesh[easy]
  3. Render meshes using Docker with software rasterization

    main
    The trimesh/trimesh:latest Docker images include a helper script that installs LLVMpipe (a software rasterizer) and XVFB. This allows you to perform mesh rendering in environments without a GPU, such as standard cloud instances, by using the CPU instead. This approach avoids the complexity of configuring GPU drivers (like CUDA or EGL) inside containers and provides more consistent results across different operating systems.
  4. Compare `nricp_sumner` and `nricp_amberg` methods

    main

    When choosing between the two Non-Rigid ICP methods, consider the following:

    Featurenricp_sumner
    Optimization TargetImplicitly solves for transformations acting on triangle orientation frames.
    Result QualityTends to output smoother results with fewer high frequencies.
    ComplexityRebuilds correspondence matrices more frequently.
    Featurenricp_amberg
    Optimization TargetExplicitly solves for per-vertex $4\times3$ affine transformations.
    TuningGenerally easier to tune.
    EfficiencyThe correspondence cost matrix $\mathbf{A}_C$ is built only once at initialization.

    Users are advised to experiment with both algorithms using different steps parameter sets to find the best fit for their specific geometry.

  5. Best practices for freezing dependencies

    main

    When freezing dependencies (e.g., in pyproject.toml, requirements.txt, or uv.lock), it is recommended not to hard-code extras like trimesh[easy].

    Instead, depend on specific packages (e.g., trimesh scipy). This helps avoid functionality mismatches with the latest versions used in Trimesh's test matrix and makes it easier to diagnose faults with specific specialized dependencies (like vhacdx).

  6. Build and run the Docker rendering example

    main

    To build the rendering image and run the provided example (which renders a sphere to a PNG), follow these steps in the directory containing the Dockerfile:

    1. Build the image: Tag the image as renderworker.
    2. Run the container: Mount your current working directory to the /output folder inside the container so that the rendered files are saved to your host machine.

    Upon successful completion, the logs will indicate the number of rendered bytes, and an output.png file will appear in your current directory.

    # build the image in the current directory
    docker build -t renderworker .
    
    # run the example
    # `-v`: mount the current directory as a volume
    # in the `/output` folder inside the image
    docker run -v `pwd`:/output renderworker
  7. Install trimesh

    main

    You can install trimesh with just its hard dependency (numpy) or with a set of common soft dependencies for expanded functionality (like convex hulls, graph operations, and preview windows).

    To install the minimal version:

    pip install trimesh

    To install with the recommended [easy] extra (includes common soft dependencies like scipy, networkx, pyglet, etc.):

    pip install trimesh[easy]
  8. Choose a mesh format

    main

    When selecting a mesh format for use with trimesh, consider the following recommendations:

    • Recommended: GLB/GLTF. It is a scene format with flat arrays that allows for extremely fast loading via numpy.frombuffer. It has a well-defined specification and supports textures, point clouds, and more.
    • For simple ASCII importers: OFF is a minimal text format containing only vertices and faces in ASCII.
    • For wide tool compatibility: OBJ (Wavefront) is widely supported but can be slow to parse and may require re-indexing to match trimesh's internal data structure.
    • For 3D printing: 3MF is an XML-based format focused on 3D printing.
    • For CAD/Solidworks workflows: 3DXML is the easiest way to transition from Solidworks to a 3D scene in trimesh.