trimesh.creation module

creation.py

Create meshes from primitives, or with operations.

trimesh.creation.annulus(r_min: float | floating | int | integer | unsignedinteger, r_max: float | floating | int | integer | unsignedinteger, height: float | floating | int | integer | unsignedinteger | None = None, sections: int | integer | unsignedinteger | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, segment: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs)

Create a mesh of an annular cylinder along Z centered at the origin.

Parameters:
  • r_min (float) – The inner radius of the annular cylinder

  • r_max (float) – The outer radius of the annular cylinder

  • height (float) – The height of the annular cylinder

  • sections (int or None) – How many pie wedges should the annular cylinder have

  • transform ((4, 4) float or None) – Transform to apply to move result from the origin

  • segment (None or (2, 3) float) – Override transform and height with a line segment

  • **kwargs – passed to Trimesh to create annulus

Returns:

annulus – Mesh of annular cylinder

Return type:

trimesh.Trimesh

trimesh.creation.axis(origin_size: float | floating | int | integer | unsignedinteger = 0.04, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, origin_color: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, axis_radius: float | floating | int | integer | unsignedinteger | None = None, axis_length: float | floating | int | integer | unsignedinteger | None = None)

Return an XYZ axis marker as a Trimesh, which represents position and orientation. If you set the origin size the other parameters will be set relative to it.

Parameters:
  • origin_size (float) – Radius of sphere that represents the origin

  • transform ((4, 4) float) – Transformation matrix

  • origin_color ((3,) float or int, uint8 or float) – Color of the origin

  • axis_radius (float) – Radius of cylinder that represents x, y, z axis

  • axis_length (float) – Length of cylinder that represents x, y, z axis

Returns:

marker – Mesh geometry of axis indicators

Return type:

trimesh.Trimesh

trimesh.creation.box(extents: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, bounds: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs)

Return a cuboid.

Parameters:
  • extents ((3,) float) – Edge lengths

  • transform ((4, 4) float) – Transformation matrix

  • bounds (None or (2, 3) float) – Corners of AABB, overrides extents and transform.

  • **kwargs – passed to Trimesh to create box

Returns:

geometry – Mesh of a cuboid

Return type:

trimesh.Trimesh

trimesh.creation.camera_marker(camera, marker_height: float | floating | int | integer | unsignedinteger = 0.4, origin_size: float | floating | int | integer | unsignedinteger | None = None)

Create a visual marker for a camera object, including an axis and FOV.

Parameters:
  • camera (trimesh.scene.Camera) – Camera object with FOV and transform defined

  • marker_height (float) – How far along the camera Z should FOV indicators be

  • origin_size (float) – Sphere radius of the origin (default: marker_height / 10.0)

Returns:

meshes – Contains Trimesh and Path3D objects which can be visualized

Return type:

list

trimesh.creation.capsule(height: float | floating | int | integer | unsignedinteger = 1.0, radius: float | floating | int | integer | unsignedinteger = 1.0, count: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None)

Create a mesh of a capsule, or a cylinder with hemispheric ends.

Parameters:
  • height (float) – Center to center distance of two spheres

  • radius (float) – Radius of the cylinder and hemispheres

  • count ((2,) int) – Number of sections on latitude and longitude

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

Returns:

capsule

Capsule geometry with:
  • cylinder axis is along Z

  • one hemisphere is centered at the origin

  • other hemisphere is centered along the Z axis at height

Return type:

trimesh.Trimesh

trimesh.creation.cone(radius: float | floating | int | integer | unsignedinteger, height: float | floating | int | integer | unsignedinteger, sections: int | integer | unsignedinteger | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs) Trimesh

Create a mesh of a cone along Z centered at the origin.

Parameters:
  • radius (float) – The radius of the cylinder

  • height (float) – The height of the cylinder

  • sections (int or None) – How many pie wedges per revolution

  • transform ((4, 4) float or None) – Transform to apply after creation

  • **kwargs (dict) – Passed to Trimesh constructor

Returns:

cone – Resulting mesh of a cone

Return type:

trimesh.Trimesh

trimesh.creation.cylinder(radius: float | floating | int | integer | unsignedinteger, height: float | floating | int | integer | unsignedinteger | None = None, sections: int | integer | unsignedinteger | None = None, segment: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs)

Create a mesh of a cylinder along Z centered at the origin.

Parameters:
  • radius (float) – The radius of the cylinder

  • height (float or None) – The height of the cylinder, or None if segment has been passed.

  • sections (int or None) – How many pie wedges should the cylinder have

  • segment ((2, 3) float) – Endpoints of axis, overrides transform and height

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

  • **kwargs – passed to Trimesh to create cylinder

Returns:

cylinder – Resulting mesh of a cylinder

Return type:

trimesh.Trimesh

trimesh.creation.extrude_polygon(polygon: Polygon, height: float | floating | int | integer | unsignedinteger, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs) Trimesh

Extrude a 2D shapely polygon into a 3D mesh

Parameters:
  • polygon (shapely.geometry.Polygon) – 2D geometry to extrude

  • height (float) – Distance to extrude polygon along Z

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

  • triangle_args (str or None) – Passed to triangle

  • **kwargs (dict) – Passed to triangulate_polygon

Returns:

mesh – Resulting extrusion as watertight body

Return type:

trimesh.Trimesh

trimesh.creation.extrude_triangulation(vertices: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], faces: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], height: float | floating | int | integer | unsignedinteger, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs) Trimesh

Extrude a 2D triangulation into a watertight mesh.

Parameters:
  • vertices ((n, 2) float) – 2D vertices

  • faces ((m, 3) int) – Triangle indexes of vertices

  • height (float) – Distance to extrude triangulation

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

  • **kwargs (dict) – Passed to Trimesh constructor

Returns:

mesh – Mesh created from extrusion

Return type:

trimesh.Trimesh

trimesh.creation.icosahedron(**kwargs) Trimesh

Create an icosahedron, one of the platonic solids which is has 20 faces.

Parameters:

kwargs (dict) – Passed through to Trimesh constructor.

Returns:

ico – Icosahederon centered at the origin.

Return type:

trimesh.Trimesh

trimesh.creation.icosphere(subdivisions: int | integer | unsignedinteger = 3, radius: float | floating | int | integer | unsignedinteger = 1.0, **kwargs)

Create an isophere centered at the origin.

Parameters:
  • subdivisions (int) – How many times to subdivide the mesh. Note that the number of faces will grow as function of 4 ** subdivisions, so you probably want to keep this under ~5

  • radius (float) – Desired radius of sphere

  • kwargs (dict) – Passed through to Trimesh constructor.

Returns:

ico – Meshed sphere

Return type:

trimesh.Trimesh

trimesh.creation.random_soup(face_count: int | integer | unsignedinteger = 100)

Return random triangles as a Trimesh

Parameters:

face_count (int) – Number of faces desired in mesh

Returns:

soup – Geometry with face_count random faces

Return type:

trimesh.Trimesh

trimesh.creation.revolve(linestring: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], angle: float | floating | int | integer | unsignedinteger | None = None, cap: bool = False, sections: int | integer | unsignedinteger | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs) Trimesh

Revolve a 2D line string around the 2D Y axis, with a result with the 2D Y axis pointing along the 3D Z axis.

This function is intended to handle the complexity of indexing and is intended to be used to create all radially symmetric primitives, eventually including cylinders, annular cylinders, capsules, cones, and UV spheres.

Note that if your linestring is closed, it needs to be counterclockwise if you would like face winding and normals facing outwards.

Parameters:
  • linestring ((n, 2) float) – Lines in 2D which will be revolved

  • angle – Angle in radians to revolve curve by or if not passed will be a full revolution (angle = 2*pi)

  • cap – If not a full revolution (0.0 < angle < 2 * pi) and cap is True attempt to add a tesselated cap.

  • sections – Number of sections result should have If not specified default is 32 per revolution

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

  • **kwargs (dict) – Passed to Trimesh constructor

Returns:

revolved – Mesh representing revolved result

Return type:

Trimesh

trimesh.creation.sweep_polygon(polygon: Polygon, path: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], angles: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, cap: bool = True, connect: bool = True, kwargs: dict | None = None, **triangulation) Trimesh

Extrude a 2D polygon into a 3D mesh along a 3D path. Note that this does not handle the case where there is very sharp curvature leading the polygon to intersect the plane of a previous slice, and does not scale the polygon along the induced normal to result in a constant cross section.

You may want to resample your path with a B-spline, i.e:

trimesh.path.simplify.resample_spline(path, smooth=0.2, count=100)

Parameters:
  • polygon (shapely.geometry.Polygon) – Profile to sweep along path

  • path ((n, 3) float) – A path in 3D

  • angles ((n,) float) – Optional rotation angle relative to prior vertex at each vertex.

  • cap – If an open path is passed apply a cap to both ends.

  • connect – If a closed path is passed connect the sweep into a single watertight mesh.

  • kwargs (dict) – Passed to the mesh constructor.

  • **triangulation – Passed to triangulate_polygon, i.e. engine=’triangle’

Returns:

mesh – Geometry of result

Return type:

trimesh.Trimesh

trimesh.creation.torus(major_radius: float | floating | int | integer | unsignedinteger, minor_radius: float | floating | int | integer | unsignedinteger, major_sections: int | integer | unsignedinteger = 32, minor_sections: int | integer | unsignedinteger = 32, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs)

Create a mesh of a torus around Z centered at the origin.

Parameters:
  • major_radius ((float)) – Radius from the center of the torus to the center of the tube.

  • minor_radius ((float)) – Radius of the tube.

  • major_sections (int) – Number of sections around major radius result should have If not specified default is 32 per revolution

  • minor_sections (int) – Number of sections around minor radius result should have If not specified default is 32 per revolution

  • transform ((4, 4) float) – Transformation matrix

  • **kwargs – passed to Trimesh to create torus

Returns:

geometry – Mesh of a torus

Return type:

trimesh.Trimesh

trimesh.creation.triangulate_polygon(polygon, triangle_args: str | None = None, engine: str | None = None, force_vertices: bool = False, **kwargs) tuple[ndarray[Any, dtype[float64]], ndarray[Any, dtype[int64]]]

Given a shapely polygon create a triangulation using a python interface to the permissively licensed mapbox-earcut or the more robust triangle.c. > pip install manifold3d > pip install triangle > pip install mapbox_earcut

Parameters:
  • polygon (Shapely.geometry.Polygon) – Polygon object to be triangulated.

  • triangle_args – Passed to triangle.triangulate i.e: ‘p’, ‘pq30’, ‘pY’=”don’t insert vert”

  • engine – None or ‘earcut’ will use earcut, ‘triangle’ will use triangle

  • force_vertices – Many operations can’t handle new vertices being inserted, so this will attempt to generate a triangulation without new vertices and raise a ValueError if it is unable to do so.

Returns:

  • vertices ((n, 2) float) – Points in space

  • faces ((n, 3) int) – Index of vertices that make up triangles

trimesh.creation.truncated_prisms(tris: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes], origin: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, normal: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None)

Return a mesh consisting of multiple watertight prisms below a list of triangles, truncated by a specified plane.

Parameters:
  • triangles ((n, 3, 3) float) – Triangles in space

  • origin (None or (3,) float) – Origin of truncation plane

  • normal (None or (3,) float) – Unit normal vector of truncation plane

Returns:

mesh – Triangular mesh

Return type:

trimesh.Trimesh

trimesh.creation.uv_sphere(radius: float | floating | int | integer | unsignedinteger = 1.0, count: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, transform: Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes] | None = None, **kwargs) Trimesh

Create a UV sphere (latitude + longitude) centered at the origin. Roughly one order of magnitude faster than an icosphere but slightly uglier.

Parameters:
  • radius (float) – Radius of sphere

  • count ((2,) int) – Number of latitude and longitude lines

  • transform (None or (4, 4) float) – Transform to apply to mesh after construction

  • kwargs (dict) – Passed thgrough

Returns:

mesh – Mesh of UV sphere with specified parameters

Return type:

trimesh.Trimesh