Here’s an example:
from pxr import Usd, UsdGeom, UsdPhysics, Gf, PhysxSchema
import omni.usd
# Create a stage
omni.usd.get_context().new_stage()
stage = omni.usd.get_context().get_stage()
# Define the root Xform (transformable object)
rootxform = UsdGeom.Xform.Define(stage, "/World")
rigidBodyXform = UsdGeom.Xform.Define(stage, "/World/rigidBody")
UsdPhysics.RigidBodyAPI.Apply(rigidBodyXform.GetPrim())
# Apply also the collision definitions here
UsdPhysics.CollisionAPI.Apply(rigidBodyXform.GetPrim())
collisionMeshAPI = UsdPhysics.MeshCollisionAPI.Apply(rigidBodyXform.GetPrim())
collisionMeshAPI.GetApproximationAttr().Set(UsdPhysics.Tokens.convexHull)
# Define shared mesh points
halfSize = 0.5
points = [
Gf.Vec3f(halfSize, -halfSize, -halfSize),
Gf.Vec3f(halfSize, halfSize, -halfSize),
Gf.Vec3f(halfSize, halfSize, halfSize),
Gf.Vec3f(halfSize, -halfSize, halfSize),
Gf.Vec3f(-halfSize, -halfSize, -halfSize),
Gf.Vec3f(-halfSize, halfSize, -halfSize),
Gf.Vec3f(-halfSize, halfSize, halfSize),
Gf.Vec3f(-halfSize, -halfSize, halfSize),
]
normals = [
Gf.Vec3f(1, 0, 0),
Gf.Vec3f(1, 0, 0),
Gf.Vec3f(1, 0, 0),
Gf.Vec3f(1, 0, 0),
Gf.Vec3f(-1, 0, 0),
Gf.Vec3f(-1, 0, 0),
Gf.Vec3f(-1, 0, 0),
Gf.Vec3f(-1, 0, 0),
]
indices = [
0, 1, 2, 3,
1, 5, 6, 2,
3, 2, 6, 7,
0, 3, 7, 4,
1, 0, 4, 5,
5, 4, 7, 6
]
vertexCounts = [4, 4, 4, 4, 4, 4]
for i in range(3):
meshPath = "/World/rigidBody/mesh" + str(i)
mesh = UsdGeom.Mesh.Define(stage, meshPath)
mesh.CreateFaceVertexCountsAttr().Set(vertexCounts)
mesh.CreateFaceVertexIndicesAttr().Set(indices)
mesh.CreatePointsAttr().Set(points)
mesh.CreateDoubleSidedAttr().Set(False)
mesh.CreateNormalsAttr().Set(normals)
if i == 1:
mesh.AddTranslateOp().Set(Gf.Vec3f(1.0 * i, 0.0, 1.0))
else:
mesh.AddTranslateOp().Set(Gf.Vec3f(1.0 * i, 0.0, 0.0))
# Now setup the mesh merging to include mesh 0 and mesh 2, excluding mesh 1
meshMergeCollision = PhysxSchema.PhysxMeshMergeCollisionAPI.Apply(rigidBodyXform.GetPrim())
meshMergeCollection = meshMergeCollision.GetCollisionMeshesCollectionAPI()
meshMergeCollection.GetIncludesRel().AddTarget("/World/rigidBody")
meshMergeCollection.GetExcludesRel().AddTarget("/World/rigidBody/mesh1")