trimesh.creation module

creation.py

Create meshes from primitives, or with operations.

Functions

annulus([r_min, r_max, height, sections, …])

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

axis([origin_size, transform, origin_color, …])

Return an XYZ axis marker as a Trimesh, which represents position and orientation.

box([extents, transform])

Return a cuboid.

camera_marker(camera[, marker_height, …])

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

capsule([height, radius, count])

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

cylinder([radius, height, sections, …])

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

extrude_polygon(polygon, height[, …])

Extrude a 2D shapely polygon into a 3D mesh

extrude_triangulation(vertices, faces, height)

Turn a 2D triangulation into a watertight Trimesh.

icosahedron()

Create an icosahedron, a 20 faced polyhedron.

icosphere([subdivisions, radius, color])

Create an isophere centered at the origin.

random_soup([face_count])

Return random triangles as a Trimesh

sweep_polygon(polygon, path[, angles])

Extrude a 2D shapely polygon into a 3D mesh along an arbitrary 3D path.

triangulate_polygon(polygon[, …])

Given a shapely polygon create a triangulation using one

uv_sphere([radius, count, theta, phi])

Create a UV sphere (latitude + longitude) centered at the origin.

validate_polygon(obj)

Make sure an input can be returned as a valid polygon.

trimesh.creation.annulus(r_min=1.0, r_max=2.0, height=1.0, sections=32, transform=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) – How many pie wedges should the annular cylinder have

  • **kwargs – passed to Trimesh to create annulus

Returns

annulus – Mesh of annular cylinder

Return type

trimesh.Trimesh

trimesh.creation.axis(origin_size=0.04, transform=None, origin_color=None, axis_radius=None, axis_length=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
  • transform ((4, 4) float) – Transformation matrix

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

  • 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=None, transform=None, **kwargs)

Return a cuboid.

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

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

  • **kwargs – passed to Trimesh to create box

Returns

geometry – Mesh of a cuboid

Return type

trimesh.Trimesh

trimesh.creation.camera_marker(camera, marker_height=0.4, origin_size=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=1.0, radius=1.0, count=[32, 32])

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

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.cylinder(radius=1.0, height=1.0, sections=32, segment=None, transform=None, **kwargs)

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

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

  • height (float) – The height of the cylinder

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

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

  • transform ((4, 4) float) – Transform to apply

  • **kwargs – passed to Trimesh to create cylinder

Returns

cylinder – Resulting mesh of a cylinder

Return type

trimesh.Trimesh

trimesh.creation.extrude_polygon(polygon, height, transform=None, triangle_args=None, **kwargs)

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

  • triangle_args (str or None) – Passed to triangle

  • **kwargs – passed to Trimesh

Returns

mesh – Resulting extrusion as watertight body

Return type

trimesh.Trimesh

trimesh.creation.extrude_triangulation(vertices, faces, height, transform=None, **kwargs)

Turn a 2D triangulation into a watertight Trimesh.

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

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

  • height (float) – Distance to extrude triangulation

  • **kwargs – passed to Trimesh

Returns

mesh – Mesh created from extrusion

Return type

trimesh.Trimesh

trimesh.creation.icosahedron()

Create an icosahedron, a 20 faced polyhedron.

Returns

ico – Icosahederon centered at the origin.

Return type

trimesh.Trimesh

trimesh.creation.icosphere(subdivisions=3, radius=1.0, color=None)

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

  • color ((3,) float or uint8) – Desired color of sphere

Returns

ico – Meshed sphere

Return type

trimesh.Trimesh

trimesh.creation.random_soup(face_count=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.sweep_polygon(polygon, path, angles=None, **kwargs)

Extrude a 2D shapely polygon into a 3D mesh along an arbitrary 3D path. Doesn’t handle sharp curvature.

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

Returns

mesh – Geometry of result

Return type

trimesh.Trimesh

trimesh.creation.triangulate_polygon(polygon, triangle_args=None, engine='auto', **kwargs)

Given a shapely polygon create a triangulation using one of the python interfaces to triangle.c: > pip install meshpy > pip install triangle

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

  • triangle_args (str or None) – Passed to triangle.triangulate i.e: ‘p’, ‘pq30’

  • engine (str) – ‘meshpy’, ‘triangle’, or ‘auto’

  • kwargs (passed directly to meshpy.triangle.build:) –

    triangle.build(mesh_info,

    verbose=False, refinement_func=None, attributes=False, volume_constraints=True, max_volume=None, allow_boundary_steiner=True, allow_volume_steiner=True, quality_meshing=True, generate_edges=None, generate_faces=False, min_angle=None)

Returns

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

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

trimesh.creation.uv_sphere(radius=1.0, count=[32, 32], theta=None, phi=None)

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

  • theta ((n,) float) – Optional theta angles in radians

  • phi ((n,) float) – Optional phi angles in radians

Returns

mesh – Mesh of UV sphere with specified parameters

Return type

trimesh.Trimesh

trimesh.creation.validate_polygon(obj)

Make sure an input can be returned as a valid polygon.

Parameters

obj (shapely.geometry.Polygon, str (wkb), or (n, 2) float) – Object which might be a polygon

Returns

polygon – Valid polygon object

Return type

shapely.geometry.Polygon

Raises

ValueError – If a valid finite- area polygon isn’t available