# Recording object movements and forces using Python script

**URL:** <https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845>\
**Category:** Samples & Examples\
**Tags:** force-sensor, physx, python\
**Created:** [April 28, 2022, 12:41pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845 "2022-04-28T12:41:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![axel.goedrich](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@axel.goedrich](https://forums.developer.nvidia.com/u/axel.goedrich)\
**Post date:** [April 28, 2022, 12:41pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/1 "2022-04-28T12:41:55Z")

</div>

Hello,  
I would like to record the positions of objects and also the forces acting on these objects. I am planning on using Matplotlib to visualize the recorded data.

To determine the object positions, I have written a small script:

import time

from pxr import Usd, UsdGeom, UsdPhysics, UsdShade, Sdf, Gf, Tf  
from omni.physx.scripts.physicsUtils import \*

gripperPath = ‘/World/o\_ring\_montage\_asm\_v4/MeshInstance\_3’  
gripperPrim = stage.GetPrimAtPath(gripperPath)

# recording movement data for 30 seconds

start\_time = time.time()  
t = 0.0  
while t \< 30:  
print(get\_translation(gripperPrim))  
time.sleep(0.1)  
t = time.time() - start\_time

However, as long as the script is executed in the script editor, the simulation freezes.

Since I am quite new to Omniverse, I am not sure which approach is suited to record such data.

Would it be best to write an extension for this?

I would also appreciate any tips on how to read out forces that are acting on Objects in Omniverse.

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![AlesBorovicka](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/alesborovicka/32/21824_2.png) [@AlesBorovicka](https://forums.developer.nvidia.com/u/AlesBorovicka)\
**Post date:** [April 29, 2022, 8:55am UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/2 "2022-04-29T08:55:05Z")

</div>

Hi,  
yes you would have to create an extension here, you need to gather the data over time while allowing the application to step.  
So in the extension you would have to register to the application update loop like this:  
self.\_update\_sub = omni.kit.app.get\_app().get\_update\_event\_stream().create\_subscription\_to\_pop(  
self.\_on\_update, name=“my update”  
)

Then in the update you can query the actual transformation for the given prim. However you can query only current position and velocity, you cant query the forces. If thats an articulation you can add articulation sensor to the rigid body and the articulation sensor would get you the forces. But for rigid bodies that do not belong to an articulation you wont get the forces.

---

<div class="post-metadata">

**Author:** ![axel.goedrich](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@axel.goedrich](https://forums.developer.nvidia.com/u/axel.goedrich)\
**Post date:** [May 2, 2022, 11:49am UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/3 "2022-05-02T11:49:24Z")

</div>

Thanks for the tip! Then I will write an extension for it.

---

<div class="post-metadata">

**Author:** ![axel.goedrich](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@axel.goedrich](https://forums.developer.nvidia.com/u/axel.goedrich)\
**Post date:** [May 6, 2022, 7:32am UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/4 "2022-05-06T07:32:51Z")

</div>

Hi Ales,

thanks again for the tip.  
I have experimented a bit with extensions and most of it works fine.

However, there are two things that are not clear to me:

First, how do I load external python packages, like matplotlib?  
The documentation mentions that omni.kit.pipapi can be used to install pip packages in the extension, but not how to do it. I found an example in another extension were numpy is installed, but that doesn’t work for other packages, like matplotlib (see code for the .toml file below).

 ![code_snippet_toml](https://global.discourse-cdn.com/nvidia/original/3X/2/4/24c807f073cc227c7bfc3102ffd19f0d0a635282.png)

Secondly, how do I trigger code to run when the simulation starts and when it ends? “def on\_startup(self, ext\_id):” is triggered when the extension loads and “def on\_shutdown(self):” is triggered when the extension closes. Are there equivalents to this that are triggered when the simulation starts and when it ends?

Many thanks in advance!

---

<div class="post-metadata">

**Author:** ![AlesBorovicka](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/alesborovicka/32/21824_2.png) [@AlesBorovicka](https://forums.developer.nvidia.com/u/AlesBorovicka)\
**Post date:** [May 6, 2022, 8:10am UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/5 "2022-05-06T08:10:42Z")

</div>

Hi Axel,  
For the second part of the question, you can use something like this:

```auto
        # simulation events callback
        def on_simulation_event(event):
            if event.type == int(SimulationEvent.RESUMED):
                self._physx_simulation_started = True

            if event.type == int(SimulationEvent.PAUSED):

            if event.type == int(SimulationEvent.STOPPED):
                self._physx_simulation_started = False

        events = get_physx_interface().get_simulation_event_stream_v2()
        self._subs.append(events.create_subscription_to_pop(on_simulation_event))

```

For the first issue, I think the problem might be syntax, I think for multiple packages, you should write it like this:

```auto
[python.pipapi]
requirements = ["psutil", "pyperclip"]

```

Can you please try it out?

Ales

---

<div class="post-metadata">

**Author:** ![axel.goedrich](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@axel.goedrich](https://forums.developer.nvidia.com/u/axel.goedrich)\
**Post date:** [May 6, 2022, 1:02pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/6 "2022-05-06T13:02:40Z")

</div>

Hi Ales,

thank you very much for your answer!

Regarding the first issue, it actually was the syntax. Now everything works fine!

As for the second problem, I’m not sure what to do with the line “self.\_subs.append(events.create\_subscription\_to\_pop(on\_simulation\_event))”. When I put it directly into the “def **init** ” function, I get an error stating, that \_subs is not defined. Is “\_subs” meant to be a list, containing all the functions that should get executed each update?

My code is below:  
Subscribing to updates generally works fine. I have tested two different versions. The first one was the one you suggested (highlighted in blue). With this solution, I get an update on every step (probably on every new frame), regardless of whether the simulation is running or not.

With the second solution (highlighted in red), I get an update at every simulation step, so only while the simulation is running. But this remains active, even after the extension is deactivated.

 ![code_snippet_test_file](https://global.discourse-cdn.com/nvidia/original/3X/f/f/ff60c0826debc52816aca21047b26668d3492131.png)

Here is the actual python file:

[test.py](https://forums.developer.nvidia.com/uploads/short-url/zRnFr2jpR5J9zh9LwYiQP5pQgvg.py) (1.9 KB)

---

<div class="post-metadata">

**Author:** ![AlesBorovicka](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/alesborovicka/32/21824_2.png) [@AlesBorovicka](https://forums.developer.nvidia.com/u/AlesBorovicka)\
**Post date:** [May 6, 2022, 1:24pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/7 "2022-05-06T13:24:19Z")

</div>

Ah yes, because I send you a code that stores these in an array, you can just store the regular subscription:  
events = get\_physx\_interface().get\_simulation\_event\_stream\_v2()  
self.\_simulation\_event\_sub = events.create\_subscription\_to\_pop(self.\_on\_simulation\_event)

Yes, you can subscribe to the step event too, this is called after each simulation step (there can be more steps per frame). So it really depends how you need to react to physics stepping.

---

<div class="post-metadata">

**Author:** ![axel.goedrich](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@axel.goedrich](https://forums.developer.nvidia.com/u/axel.goedrich)\
**Post date:** [May 6, 2022, 1:59pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/8 "2022-05-06T13:59:47Z")

</div>

Thanks a lot, now everything works!

Python didn’t know what to do with “SimulationEvent.Resume” etc, but when the respective integers are directly inserted, everything works as expected.

For reference, here is the working code:

 ![code_snippet_ales_rocks](https://global.discourse-cdn.com/nvidia/original/3X/3/f/3f635548c0368085b3d5098f98c6a68499cee83c.png)

and the matching Python file:  
[test.py](https://forums.developer.nvidia.com/uploads/short-url/sCWKmf7rpWuskyVHJZgKsFpW4Uy.py) (1.9 KB)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/system/32/68080_2.png) [@system](https://forums.developer.nvidia.com/u/system)\
**Post date:** [May 20, 2022, 1:59pm UTC](https://forums.developer.nvidia.com/t/recording-object-movements-and-forces-using-python-script/212845/9 "2022-05-20T13:59:50Z")

</div>

Closed on May 20, 2022, 1:59pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
