Script for Convex Decomposition Collisions

Hello everyone, I have a small problem and would like to ask if anyone knows something about how to solve this. I wrote a small script to add collisionAPI to an object. As I do not yet specify which approximation for the collision mesh I want to use (triangle mesh, convex hull, convex decomposition, …) it automatically applies the convex hull approximation. I would like to use convex decomposition, though.
Unfortunately, I cannot just set the attribute “pyhsics:approximation” to “convexDecomposition” because a Token is expected (I am unsure of how this mechanic works…). Does anyone know how to set these kinds of specific attributes correctly?

Here is a cleaned and minimal working version of the script that loads a usd file, sets the CollisionAPI and then saves it:

def add_collision(usd_path_load, usd_path_save):
usd_context = get_context()
stage = usd_context.open_stage(usd_path_load)
stage = get_context().get_stage()
rigidPrim = stage.GetPrimAtPath(“/World/model_normalized/mesh”)
UsdPhysics.CollisionAPI.Apply(rigidPrim)
# rigidPrim.GetAttribute(“physics:approximation”).Set(“convexDecomposition”)
stage.Export(usd_path_save)

GetAttribute(): Universal Scene Description: UsdPrim Class Reference

Hi @christopher117 - The physics:approximation attribute expects a token, which is a specific type of string used in USD to represent a limited set of predefined values. In this case, the valid tokens for physics:approximation are none, convexHull, box, capsule, sphere, plane, and convexDecomposition.

To set the physics:approximation attribute to convexDecomposition, you can use the UsdPhysics.Tokens class, which provides predefined tokens for all the valid values. Here’s how you can modify your script:

def add_collision(usd_path_load, usd_path_save):
    usd_context = get_context()
    stage = usd_context.open_stage(usd_path_load)
    stage = get_context().get_stage()
    rigidPrim = stage.GetPrimAtPath("/World/model_normalized/mesh")
    collisionAPI = UsdPhysics.CollisionAPI.Apply(rigidPrim)
    collisionAPI.GetPhysicsApproximationAttr().Set(UsdPhysics.Tokens.convexDecomposition)
    stage.Export(usd_path_save)

In this script, UsdPhysics.Tokens.convexDecomposition is a token representing the string "convexDecomposition". The GetPhysicsApproximationAttr().Set() method is used to set the physics:approximation attribute of the CollisionAPI.

Please note that the actual path to your mesh and the context setup might need to be adjusted according to your specific setup.