Hello.
I am trying to implement the collision report function in Extension python file, but it is not working as the demo that I referenced (Physics demo scene → Contact report).
In my case, I want to report the collision information whenever the even occurs.
This is the part of my code and the result.
...
from pxr import Usd, UsdLux, UsdGeom, UsdShade, Sdf, Gf, Tf, Vt, UsdPhysics, PhysxSchema
from omni.physx.scripts.physicsUtils import *
from omni.physx import get_physx_interface, get_physx_simulation_interface
from omni.physx.bindings._physx import SimulationEvent
import omni.physxdemos as demo
class Extension(omni.ext.IExt):
def on_startup(self):
"""Initialize extension and UI elements"""
self._timeline = omni.timeline.get_timeline_interface()
self._viewport = omni.kit.viewport.get_default_viewport_window()
self._usd_context = omni.usd.get_context()
self._stage = self._usd_context.get_stage()
self._window = None
...
def _menu_callback(self):
self._on_environment_setup()
pass
async def _create_scenario(self):
await omni.usd.get_context().new_stage_async()
await omni.kit.app.get_app().next_update_async()
...
create_prim(
prim_path="/robot",
usd_path=self._nucleus_path + "robot.usd")
create_prim(
prim_path="/obstacle",
usd_path=self._nucleus_path + "obstacle.usd")
await omni.kit.app.get_app().next_update_async()
RobotPrim = self._stage.GetPrimAtPath("/robot")
contactReportAPI = PhysxSchema.PhysxContactReportAPI.Apply(RobotPrim)
contactReportAPI.CreateThresholdAttr().Set(200000)
await omni.kit.app.get_app().next_update_async()
self._timeline.play()
***### I think this codelet is related with report function ###***
events = get_physx_interface().get_simulation_event_stream()
self._simulation_event_sub = events.create_subscription_to_pop(self._on_simulation_event)
self.collider0 = ''
def _on_environment_setup(self):
...
asyncio.ensure_future(self._create_scenario())
*## This part was referenced from the demo python codes.*
def _print_event(self, event, contactDict):
print("Actor0: " + str(contactDict["actor0"]))
print("Actor1: " + str(contactDict["actor1"]))
print("Collider0: " + str(contactDict["collider0"]))
print("Collider1: " + str(contactDict["collider1"]))
print("Num contacts: " + str(event.payload['numContactData']))
def _on_simulation_event(self, event):
if event.type == int(SimulationEvent.CONTACT_DATA):
print("Normal: " + str(event.payload['normal']))
print("Position: " + str(event.payload['position']))
print("Impulse: " + str(event.payload['impulse']))
otherPrim = self._stage.GetPrimAtPath(self.collider0)
usdGeom = UsdGeom.Mesh.Get(self._stage, otherPrim.GetPath())
color = Vt.Vec3fArray([Gf.Vec3f(random(), random(), random())])
usdGeom.GetDisplayColorAttr().Set(color)
if event.type == int(SimulationEvent.CONTACT_FOUND):
print("Contact found:")
contactDict = resolveContactEventPaths(event)
self._print_event(event, contactDict)
self.collider0 = contactDict["collider0"]
if event.type == int(SimulationEvent.CONTACT_PERSISTS):
print("Contact persist:")
contactDict = resolveContactEventPaths(event)
self._print_event(event, contactDict)
self.collider0 = contactDict["collider0"]
if event.type == int(SimulationEvent.CONTACT_LOST):
print("Contact lost:")
contactDict = resolveContactEventPaths(event)
self._print_event(event, contactDict)
self.collider0 = ''
Could you let me know some python code or functions proper in my case.
Thank you.