Changing object properties through python

Hello,

I want to automate a test-scenario for our current project, and I would therefore need to change some object properties through python code.

One example for this would be the number of time steps per second in the physx scene (see image below).

If I print dir(physx_scene_object) I get a list with object attributes. The Gravity Direction and the Gravity Magnitude are in this list, but there is no mention of the other attributes.

Is there an easy way to list all attributes of such an object and a general way to change them?

Many thanks in advance!

Hi @axel.goedrich

The following snippet shows how to list all prim attributes and how to change one of them.
You can try it in the ScriptEditor

To get or set an attribute’s value, you need to use its name (one way to obtain it is by placing the cursor over the attribute)

import omni

stage = omni.usd.get_context().get_stage()
prim = stage.GetPrimAtPath("/physicsScene")

# list all attributes
for attribute in prim.GetAttributes():
    print()
    print("name:", "{}:{}".format(attribute.GetNamespace(), attribute.GetBaseName()) if attribute.GetNamespace() else attribute.GetBaseName())
    print("display name:", attribute.GetDisplayName())

# set an attribute
prim.GetAttribute("physxScene:timeStepsPerSecond").Set(120)

1 Like

Hi @toni.sm,

thank you very much, that is what I was looking for.

But unfortunately, this doesn’t work for a freshly created physics scene.
If I, for example, create the physics scene directly in the code snipped, or through: create → Physics → Physics Scene
I only get the gravity direction and gravity magnitude as prim attributes. You can test it with this code snipped in an empty project:

import omni
from pxr import UsdPhysics


stage = omni.usd.get_context().get_stage()

# Physics scene definition
scenePath = "/physicsScene"
scene = UsdPhysics.Scene.Define(stage, scenePath)

prim = stage.GetPrimAtPath("/physicsScene")

# list all attributes
for attribute in prim.GetAttributes():

    print()

    print("name:", "{}:{}".format(attribute.GetNamespace(), attribute.GetBaseName()) if attribute.GetNamespace() else attribute.GetBaseName())

    print("display name:", attribute.GetDisplayName())



# set an attribute
prim.GetAttribute("physxScene:timeStepsPerSecond").Set(120)

I can only see the other attributes, if I manually change an attribute once (for example, the number of time steps per second).

Is there a workaround for this? Otherwise, I would have to manually change the attribute once, after the scene is created.

Hi @axel.goedrich! You’ll actually want to use the APIs provided by the PhysxSchema for this. Here’s what that looks like:

from pxr import PhysxSchema

stage = omni.usd.get_context().get_stage()
physx_scene = stage.GetPrimAtPath("/World/PhysicsScene")
scene_api = PhysxSchema.PhysxSceneAPI.Apply(physx_scene)
scene_api.CreateTimeStepsPerSecondAttr(120)
1 Like

To expand and explain the logic of the physics property widgets and why you see more in the UI than is sometimes accessible from the script: in our widgets we group the properties by logical components that consist of combinations of a prim and applied APIs and this should be transparent to the user (we deem it too low-level USD to show a per-schema view explicitly). Some of the schemas are thus considered an extension to others (like PhysxSchema.PhysxSceneAPI is to UsdPhysics.Scene), so when we see a Scene prim we show also the extension’s properties so that the users sees the whole range of available options even when the extension’s API is not applied at the moment. When the user edits one of the extension’s properties it will automatically apply the correct API (if not already applied).

(Note that there’s a sort of a debug view, which is the Raw widget, which is always shown as last in the property window and it includes only the actually authored properties.)

A bit low-level, but you can have a look at the extension_api_map dictionary in database.py of the physics property GUI extension omni.kit.property.physx to see all extensions we have defined by now. (This is not public API at the moment, so I do not guarantee the naming or the placement would stay the same moving forward)

1 Like

@axel.goedrich Michal also made me aware of the Physics Scripting Manual in case you haven’t seen that:
image

1 Like

@mati-nvidia Thank you very much! Everything is working now!

@mhapala Thank you for the explanation, that was very helpful to understand the underlying process.

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