trimesh.collision

class trimesh.collision.CollisionManager

Bases: object

A mesh-mesh collision manager.

__init__()

Initialize a mesh-mesh collision manager.

add_object(name, mesh, transform=None)

Add an object to the collision manager.

If an object with the given name is already in the manager, replace it.

Parameters:
  • name (str) – An identifier for the object

  • mesh (Trimesh object) – The geometry of the collision object

  • transform ((4,4) float) – Homogeneous transform matrix for the object

in_collision_internal(return_names=False, return_data=False)

Check if any pair of objects in the manager collide with one another.

Parameters:
  • return_names (bool) – If true, a set is returned containing the names of all pairs of objects in collision.

  • return_data (bool) – If true, a list of ContactData is returned as well

Returns:

  • is_collision (bool) – True if a collision occurred between any pair of objects and False otherwise

  • names (set of 2-tup) – The set of pairwise collisions. Each tuple contains two names in alphabetical order indicating that the two corresponding objects are in collision.

  • contacts (list of ContactData) – All contacts detected

in_collision_other(other_manager, return_names=False, return_data=False)

Check if any object from this manager collides with any object from another manager.

Parameters:
  • other_manager (CollisionManager) – Another collision manager object

  • return_names (bool) – If true, a set is returned containing the names of all pairs of objects in collision.

  • return_data (bool) – If true, a list of ContactData is returned as well

Returns:

  • is_collision (bool) – True if a collision occurred between any pair of objects and False otherwise

  • names (set of 2-tup) – The set of pairwise collisions. Each tuple contains two names (first from this manager, second from the other_manager) indicating that the two corresponding objects are in collision.

  • contacts (list of ContactData) – All contacts detected

in_collision_single(mesh, transform=None, return_names=False, return_data=False)

Check a single object for collisions against all objects in the manager.

Parameters:
  • mesh (Trimesh object) – The geometry of the collision object

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

  • return_names (bool) – If true, a set is returned containing the names of all objects in collision with the object

  • return_data (bool) – If true, a list of ContactData is returned as well

Returns:

  • is_collision (bool) – True if a collision occurs and False otherwise

  • names (set of str) – [OPTIONAL] The set of names of objects that collided with the provided one

  • contacts (list of ContactData) – [OPTIONAL] All contacts detected

min_distance_internal(return_names=False, return_data=False)

Get the minimum distance between any pair of objects in the manager.

Parameters:
  • return_names (bool) – If true, a 2-tuple is returned containing the names of the closest objects.

  • return_data (bool) – If true, a DistanceData object is returned as well

Returns:

  • distance (float) – Min distance between any two managed objects

  • names ((2,) str) – The names of the closest objects

  • data (DistanceData) – Extra data about the distance query

min_distance_other(other_manager, return_names=False, return_data=False)

Get the minimum distance between any pair of objects, one in each manager.

Parameters:
  • other_manager (CollisionManager) – Another collision manager object

  • return_names (bool) – If true, a 2-tuple is returned containing the names of the closest objects.

  • return_data (bool) – If true, a DistanceData object is returned as well

Returns:

  • distance (float) – The min distance between a pair of objects, one from each manager.

  • names (2-tup of str) – A 2-tuple containing two names (first from this manager, second from the other_manager) indicating the two closest objects.

  • data (DistanceData) – Extra data about the distance query

min_distance_single(mesh, transform=None, return_name=False, return_data=False)

Get the minimum distance between a single object and any object in the manager.

Parameters:
  • mesh (Trimesh object) – The geometry of the collision object

  • transform ((4,4) float) – Homogeneous transform matrix for the object

  • return_names (bool) – If true, return name of the closest object

  • return_data (bool) – If true, a DistanceData object is returned as well

Returns:

  • distance (float) – Min distance between mesh and any object in the manager

  • name (str) – The name of the object in the manager that was closest

  • data (DistanceData) – Extra data about the distance query

remove_object(name)

Delete an object from the collision manager.

Parameters:

name (str) – The identifier for the object

set_transform(name, transform)

Set the transform for one of the manager’s objects. This replaces the prior transform.

Parameters:
  • name (str) – An identifier for the object already in the manager

  • transform ((4,4) float) – A new homogeneous transform matrix for the object

class trimesh.collision.ContactData(names, contact)

Bases: object

Data structure for holding information about a collision contact.

__init__(names, contact)

Initialize a ContactData.

Parameters:
  • names (list of str) – The names of the two objects in order.

  • contact (fcl.Contact) – The contact in question.

property depth

The penetration depth of the 3D point of intersection for this contact.

Returns:

depth – The penetration depth.

Return type:

float

index(name)

Returns the index of the face in contact for the mesh with the given name.

Parameters:

name (str) – The name of the target object.

Returns:

index – The index of the face in collision

Return type:

int

property normal

The 3D intersection normal for this contact.

Returns:

normal – The intersection normal.

Return type:

(3,) float

property point

The 3D point of intersection for this contact.

Returns:

point – The intersection point.

Return type:

(3,) float

class trimesh.collision.DistanceData(names, result)

Bases: object

Data structure for holding information about a distance query.

__init__(names, result)

Initialize a DistanceData.

Parameters:
  • names (list of str) – The names of the two objects in order.

  • contact (fcl.DistanceResult) – The distance query result.

property distance

Returns the distance between the two objects.

Returns:

distance – The euclidean distance between the objects.

Return type:

float

index(name)

Returns the index of the closest face for the mesh with the given name.

Parameters:

name (str) – The name of the target object.

Returns:

index – The index of the face in collisoin.

Return type:

int

point(name)

The 3D point of closest distance on the mesh with the given name.

Parameters:

name (str) – The name of the target object.

Returns:

point – The closest point.

Return type:

(3,) float

trimesh.collision.mesh_to_BVH(mesh)

Create a BVHModel object from a Trimesh object

Parameters:

mesh (Trimesh) – Input geometry

Returns:

bvh – BVH of input geometry

Return type:

fcl.BVHModel

trimesh.collision.mesh_to_convex(mesh)

Create a Convex object from a Trimesh object

Parameters:

mesh (Trimesh) – Input geometry

Returns:

convex – Convex of input geometry

Return type:

fcl.Convex

trimesh.collision.scene_to_collision(scene)

Create collision objects from a trimesh.Scene object.

Parameters:

scene (trimesh.Scene) – Scene to create collision objects for

Returns:

  • manager (CollisionManager) – CollisionManager for objects in scene

  • objects ({node name: CollisionObject}) – Collision objects for nodes in scene