Physics
This part includes the APIs for setting up physics in the stage , read more about USDPhysics
Rigid body & Collision
from pxr import UsdPhysics
# check rigidbody api
prim.HasAPI(UsdPhysics.RigidBodyAPI)
prim.HasAPI(pxr.UsdPhysics.CollisionAPI)
# get api
UsdPhysics.RigidBodyAPI.Get(stage, path)
UsdPhysics.CollisionAPI.Get(stage, path)
# apply/add api
UsdPhysics.RigidBodyAPI.Apply(stage, path)
UsdPhysics.CollisionAPI.Apply(stage, path)
Set up GPU for physics
from omni.physx import acquire_physx_interface
physx = acquire_physx_interface()
physx.overwrite_gpu_setting(1) # 1 means using GPU
Set mass
mass = 10
massAPI = UsdPhysics.MassAPI.Apply(prim)
massAPI.GetMassAttr().Set(mass)
Set/Get Gravity value
# FIXME: correct gravity api
# Assume _my_world is of type World
# self._my_world ._physics_context.get_gravity()
# meters_per_unit = get_stage_units()
# self._my_world ._physics_context.set_gravity(value=-9.81 / meters_per_unit)
Set up collision
from omni.physx.scripts import utils, physicsUtils
utils.setPhysics(prim=cup_shape_prim, kinematic=False)
utils.setCollider(prim=cup_shape_prim, approximationShape="convexDecomposition")
# other approximationShape: none, triangulate, convexHull, e.t.c.
Set up physical material
def _setup_physics_material(self, path: Sdf.Path):
from pxr import UsdGeom, UsdLux, Gf, Vt, UsdPhysics, PhysxSchema, Usd, UsdShade, Sdf
if self._physicsMaterialPath is None:
self._physicsMaterialPath = self._stage.GetDefaultPrim().GetPath().AppendChild("physicsMaterial")
UsdShade.Material.Define(self._stage, self._physicsMaterialPath)
material = UsdPhysics.MaterialAPI.Apply(self._stage.GetPrimAtPath(self._physicsMaterialPath))
material.CreateStaticFrictionAttr().Set(self._material_static_friction)
material.CreateDynamicFrictionAttr().Set(self._material_dynamic_friction)
material.CreateRestitutionAttr().Set(self._material_restitution)
collisionAPI = UsdPhysics.CollisionAPI.Get(self._stage, path)
prim = self._stage.GetPrimAtPath(path)
if not collisionAPI:
collisionAPI = UsdPhysics.CollisionAPI.Apply(prim)
# apply material
physicsUtils.add_physics_material_to_prim(self._stage, prim, self._physicsMaterialPath)
Set up force
The forceAPI is will a Xform. Pushing the parent Xform. e.g. …/Xform_ball/ballForce is pushing …/Xform_ball
import omni.timeline
from pxr import PhysxSchema, UsdGeom, Gf
stage = omni.usd.get_context().get_stage()
# changing timeline end time to avoid looping
omni.timeline.get_timeline_interface().set_end_time(10000/24)
xform = UsdGeom.Xform.Define(stage, "/World/ball1_04/Xform_01/ballForce")
forceApi = PhysxSchema.PhysxForceAPI.Apply(xform.GetPrim())
forceAttr = forceApi.GetForceAttr()
forceAttr.Set(time=0, value=Gf.Vec3f(0.0, 0, 300.0))
forceAttr.Set(time=20, value=Gf.Vec3f(0.0, 0, 300.0))
forceEnabledAttr = forceApi.GetForceEnabledAttr()
forceEnabledAttr.Set(time=0, value=True)
forceEnabledAttr.Set(time=20, value=False)
xformable = UsdGeom.Xformable(xform.GetPrim())
translateOp = xformable.AddTranslateOp()
translateOp.Set(time=0, value = Gf.Vec3d(0.0, 0.0, 0.0))
translateOp.Set(time=50, value = Gf.Vec3d(0.0, -1.0, 0.0))