Can't not update rigid body velocity(physic attribute) during step update

Isaac Sim Version

4.5.0

Operating System

Windows 11

hi,

the purpose is update the velocity of rigidbody during step update (as the screenshot) for sync with other tools.
however, it not always working. (however, update position is always okay)
want to know do i miss any fundamental concept? or there’s any suggestion?
thank you!

Hi @yuchunlin33, thank you for posting your question. Is it possible that you may be setting to the incorrect attribute? One way to make sure you are setting to the correct attribute is to set it through the RigidBodyAPI. To do that, on a prim that has UsdPhysics.RigidBodyAPI call

GetVelocityAttr().Set(..)

I’m attempting to set the object velocity in this way (GetVelocityAttr().Set(..)) in an extension.

It works - velocity is changed - when invoked as the result of a button click in the UI.

It does NOT work when invoked as part of a physics step callback.

i think the root cause is the simulation buffer rule.

Python API — Omni Physics

velcocity can be chaged immediately by calling flush_changes().

the code below(omit unnecessary part) is workable for me.


import omni.physx as physx
from pxr import Gf
import asyncio

physx_sim = physx.acquire_physx_simulation_interface()
 
...

async def test():

    def update_every_2s(dt):
      prim = stage.GetPrimAtPath("/World/Cube")
      attr = prim.GetAttribute("physics:velocity").set(Gf.Vec3f(v[step], 0, 0))
      physx_sim.flush_changes() # add this

    simulation_context = SimulationContext()
    await simulation_context.initialize_simulation_context_async()
    await simulation_context.reset_async()
    simulation_context.add_physics_callback("velocity_updater", update_every_2s)

asyncio.ensure_future(test())
3 Likes

i had updated the solution for reference.

physx_sim.flush_changes() did the trick!’

Well done!!!