trimesh.primitives
primitives.py
Subclasses of Trimesh objects that are parameterized as primitives.
Useful because you can move boxes and spheres around and then use trimesh operations on them at any point.
- class trimesh.primitives.Box(extents=None, transform=None, **kwargs)
Bases:
_Primitive
- __init__(extents=None, transform=None, **kwargs)
Create a Box Primitive as a subclass of Trimesh
- Parameters
extents ((3,) float) – Length of each side of the 3D box
transform ((4, 4) float) – Homogeneous transformation matrix for box center
- as_outline()
Return a Path3D containing the outline of the box.
- Returns
outline – Outline of box primitive
- Return type
- property is_oriented
Returns whether or not the current box is rotated at all.
- sample_grid(count=None, step=None)
Return a 3D grid which is contained by the box. Samples are either ‘step’ distance apart, or there are ‘count’ samples per box side.
- Parameters
count (int or (3,) int) – If specified samples are spaced with np.linspace
step (float or (3,) float) – If specified samples are spaced with np.arange
- Returns
grid – Points inside the box
- Return type
(n, 3) float
- sample_volume(count)
Return random samples from inside the volume of the box.
- Parameters
count (int) – Number of samples to return
- Returns
samples – Points inside the volume
- Return type
(count, 3) float
- to_dict()
Get a copy of the current Box primitive as a JSON-serializable dict that matches the schema in trimesh/resources/schema/box.schema.json
- Returns
as_dict – Serializable data for this primitive.
- Return type
dict
- property transform
The transform of the Primitive object.
- Returns
transform – Homogeneous transformation matrix
- Return type
(4, 4) float
- property volume
Volume of the box Primitive.
- Returns
volume – Volume of box.
- Return type
float
- class trimesh.primitives.Capsule(radius=1.0, height=10.0, transform=None, sections=32, **kwargs)
Bases:
_Primitive
- __init__(radius=1.0, height=10.0, transform=None, sections=32, **kwargs)
Create a Capsule Primitive, a subclass of Trimesh.
- Parameters
radius (float) – Radius of cylinder
height (float) – Height of cylinder
transform ((4, 4) float) – Transformation matrix
sections (int) – Number of facets in circle
- property direction
The direction of the capsule’s axis.
- Returns
axis – Vector along the cylinder axis
- Return type
(3,) float
- to_dict()
Get a copy of the current Capsule primitive as a JSON-serializable dict that matches the schema in trimesh/resources/schema/capsule.schema.json
- Returns
as_dict – Serializable data for this primitive.
- Return type
dict
- property transform
The transform of the Primitive object.
- Returns
transform – Homogeneous transformation matrix
- Return type
(4, 4) float
- class trimesh.primitives.Cylinder(radius=1.0, height=1.0, transform=None, sections=32, **kwargs)
Bases:
_Primitive
- __init__(radius=1.0, height=1.0, transform=None, sections=32, **kwargs)
Create a Cylinder Primitive, a subclass of Trimesh.
- Parameters
radius (float) – Radius of cylinder
height (float) – Height of cylinder
transform ((4, 4) float) – Homogeneous transformation matrix
sections (int) – Number of facets in circle
- buffer(distance)
Return a cylinder primitive which covers the source cylinder by distance: radius is inflated by distance height by twice the distance.
- Parameters
distance (float) – Distance to inflate cylinder radius and height
- Returns
buffered – Cylinder primitive inflated by distance
- Return type
- property direction
The direction of the cylinder’s axis.
- Returns
axis
- Return type
(3,) float, vector along the cylinder axis
- property moment_inertia
The analytic inertia tensor of the cylinder primitive.
- Returns
tensor – 3D inertia tensor
- Return type
(3, 3) float
- property segment
A line segment which if inflated by cylinder radius would represent the cylinder primitive.
- Returns
segment – Points representing a single line segment
- Return type
(2, 3) float
- to_dict()
Get a copy of the current Cylinder primitive as a JSON-serializable dict that matches the schema in trimesh/resources/schema/cylinder.schema.json
- Returns
as_dict – Serializable data for this primitive.
- Return type
dict
- property volume
The analytic volume of the cylinder primitive.
- Returns
volume – Volume of the cylinder
- Return type
float
- class trimesh.primitives.Extrusion(polygon=None, transform=None, height=1.0, **kwargs)
Bases:
_Primitive
- __init__(polygon=None, transform=None, height=1.0, **kwargs)
Create an Extrusion primitive, which is a subclass of Trimesh.
- Parameters
polygon (shapely.geometry.Polygon) – Polygon to extrude
transform ((4, 4) float) – Transform to apply after extrusion
height (float) – Height to extrude polygon by
- property area
The surface area of the primitive extrusion.
Calculated from polygon and height to avoid mesh creation.
- Returns
area – Surface area of 3D extrusion
- Return type
float
- property bounding_box_oriented
An oriented bounding box for the current mesh.
- Returns
obb – Box object with transform and extents defined representing the minimum volume oriented bounding box of the mesh
- Return type
- buffer(distance, distance_height=None, **kwargs)
Return a new Extrusion object which is expanded in profile and in height by a specified distance.
- Parameters
distance (float) – Distance to buffer polygon
distance_height (float) – Distance to buffer above and below extrusion
kwargs (dict) – Passed to Extrusion constructor
- Returns
buffered – Extrusion object with new values
- Return type
- property direction
Based on the extrudes transform what is the vector along which the polygon will be extruded.
- Returns
direction – Unit direction vector
- Return type
(3,) float
- property origin
Based on the extrude transform what is the origin of the plane it is extruded from.
- Returns
origin – Origin of extrusion plane
- Return type
(3,) float
- slide(distance)
Alter the transform of the current extrusion to slide it along its extrude_direction vector
- Parameters
distance (float) – Distance along self.extrude_direction to move
- to_dict()
Get a copy of the current Extrusion primitive as a JSON-serializable dict that matches the schema in trimesh/resources/schema/extrusion.schema.json
- Returns
as_dict – Serializable data for this primitive.
- Return type
dict
- property transform
The transform of the Primitive object.
- Returns
transform – Homogeneous transformation matrix
- Return type
(4, 4) float
- property volume
The volume of the Extrusion primitive. Calculated from polygon and height to avoid mesh creation.
- Returns
volume – Volume of 3D extrusion
- Return type
float
- class trimesh.primitives.Sphere(radius=1.0, center=None, transform=None, subdivisions=3, **kwargs)
Bases:
_Primitive
- __init__(radius=1.0, center=None, transform=None, subdivisions=3, **kwargs)
Create a Sphere Primitive, a subclass of Trimesh.
- Parameters
radius (float) – Radius of sphere
center (None or (3,) float) – Center of sphere.
transform (None or (4, 4) float) – Full homogenous transform. Pass center OR `transform.
subdivisions (int) – Number of subdivisions for icosphere.
- property area
Surface area of the current sphere primitive.
- Returns
area
- Return type
float, surface area of the sphere Primitive
- property bounding_box_oriented
An oriented bounding box for the current mesh.
- Returns
obb – Box object with transform and extents defined representing the minimum volume oriented bounding box of the mesh
- Return type
- property bounds
The axis aligned bounds of the faces of the mesh.
- Returns
bounds – Bounding box with [min, max] coordinates If mesh is empty will return None
- Return type
(2, 3) float or None
- property center
- property moment_inertia
The analytic inertia tensor of the sphere primitive.
- Returns
tensor – 3D inertia tensor.
- Return type
(3, 3) float
- to_dict()
Get a copy of the current Sphere primitive as a JSON-serializable dict that matches the schema in trimesh/resources/schema/sphere.schema.json
- Returns
as_dict – Serializable data for this primitive.
- Return type
dict
- property volume
Volume of the current sphere primitive.
- Returns
volume
- Return type
float, volume of the sphere Primitive