Loading Scenes#

This page shows how your program gets a USD file, a URL, or USD text into ovphysx, and how to check what ovphysx found in it.

The USD file crates.usda flows into an ovstage stage that already knows the PhysX schemas. The stage is sealed at ordinal 1, ovphysx attaches to it, and a read lists the three rigid bodies that ovphysx found. The band at the bottom shows the call that reads their positions.

Four Steps, in Order#

With ovphysx installed as in Install, every program loads a scene in the same four steps:

  1. Register the PhysX schemas with register_usd_schemas, once, before the first load.

  2. Load the scene into an ovstage.Stage with open_usd_with_desc, or open_usd_from_string_with_desc for text, using descs=physics_population_descs(rendering=True) with the helper from ovphysx.population.

  3. Seal ordinal 1 with stage.advance_write_floor(ordinal=1).wait().

  4. Attach with physx.attach_ovstage(stage, read_ordinal=1) and wait for its task.

Registering late or not at all leads to the errors in Common Mistakes. Concepts explains stages, ordinals, and sealing.

Why are the PhysX schemas a separate package?

The PhysX schemas hold no code. They are definitions that tell USD which PhysX settings a scene can hold. ovphysx is only one of the tools that read those settings: a USD editor or your own usd-core script reads them too. As a package of their own, the schemas give every tool in your program one shared copy, and tools that never simulate can use them without ovphysx. Install shows how to add them, for Python and for C.

Load a File#

The following program, loading_scenes_file.py, loads a scene file with three crates on the ground and prints the rigid bodies that ovphysx found. Save the scene as loading_scenes_crates.usda in the same folder as the program:

loading_scenes_crates.usda
#usda 1.0
(
    defaultPrim = "World"
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

# Three 0.5 m crates on the ground: two side by side and one on top.
def Xform "World"
{
    def PhysicsScene "PhysicsScene"
    {
    }

    def Plane "Ground" (
        prepend apiSchemas = ["PhysicsCollisionAPI"]
    )
    {
        uniform token axis = "Z"
    }

    def Xform "Crates"
    {
        def Cube "CrateA" (
            prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
        )
        {
            double size = 0.5
            double3 xformOp:translate = (-0.3, 0, 0.25)
            uniform token[] xformOpOrder = ["xformOp:translate"]
        }

        def Cube "CrateB" (
            prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
        )
        {
            double size = 0.5
            double3 xformOp:translate = (0.3, 0, 0.25)
            uniform token[] xformOpOrder = ["xformOp:translate"]
        }

        def Cube "CrateC" (
            prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
        )
        {
            double size = 0.5
            double3 xformOp:translate = (0, 0, 0.75)
            uniform token[] xformOpOrder = ["xformOp:translate"]
        }
    }
}
from pathlib import Path

import ovstage
from ovphysx import PhysX
from ovphysx.population import physics_population_descs
from ovphysx.samples.python_samples.schema_support import physx_schema_paths
from ovphysx.types import SimObjectType

# The scene file is in the same folder as this program.
SCENE_FILE = Path(__file__).with_name("loading_scenes_crates.usda")


def rigid_bodies(physx):
    """Return the path and position of every rigid body in the simulation."""
    bodies = []
    with physx.read_sync(SimObjectType.RIGID_BODY, ["position"]) as result:
        for group in result.groups:
            names = result.dictionary.get_path_strings(group.prim_list)
            for name, position in zip(names, group.tensors[0].numpy()):
                bodies.append((name, position.tolist()))
    return bodies


# Register the PhysX schemas once per process, before the first load.
ovstage.population.register_usd_schemas([str(p) for p in physx_schema_paths()])

# Load the file into a stage at ordinal 1, then seal ordinal 1.
stage = ovstage.Stage("crates")
ovstage.population.open_usd_with_desc(
    stage, str(SCENE_FILE), ordinal=1, time=ovstage.population.DEFAULT_TIME, descs=physics_population_descs(rendering=True)
)
stage.advance_write_floor(ordinal=1).wait()

# Attach the sealed stage. ovphysx builds the simulation from it.
physx = PhysX()
physx.wait_task(physx.attach_ovstage(stage, read_ordinal=1))

bodies = rigid_bodies(physx)
print(f"ovphysx found {len(bodies)} rigid bodies:")
for path, (x, y, z) in sorted(bodies):
    print(f"  {path}  at ({x:.2f}, {y:.2f}, {z:.2f}) m")

physx.wait_task(physx.detach_ovstage())
physx.destroy()
stage.destroy()

It prints:

ovphysx found 3 rigid bodies:
  /World/Crates/CrateA  at (-0.30, 0.00, 0.25) m
  /World/Crates/CrateB  at (0.30, 0.00, 0.25) m
  /World/Crates/CrateC  at (0.00, 0.00, 0.75) m

Notice the following:

  • open_usd_with_desc takes the path as a string. A URL goes in the same place.

  • The ground is a static collider, not a rigid body, so it is not in the list. Rigid Bodies explains the difference.

Choose What to Load#

Pass descs=physics_population_descs(rendering=True) to open_usd_with_desc. The helper, imported from ovphysx.population, combines the physics population description with the rendering description for meshes, lights, materials and cameras. Use physics_population_descs() for physics alone, including native USD scene-graph instances. Add rendering when a renderer shares the stage or the scene uses point-instancer prototype geometry.

Where Scenes Come From#

open_usd_with_desc reads local files and URLs:

Source

Example

Local file

/home/me/scenes/crates.usda

Omniverse Nucleus

omniverse://server/path/scene.usd

Amazon S3

https://my-bucket.s3.us-east-1.amazonaws.com/path/scene.usd

Azure Blob Storage

https://account.blob.core.windows.net/container/scene.usd

Write S3 locations as https:// URLs, not s3:// URIs. Public files need no setup. For a private S3 bucket or Azure container, your program gives the Omniverse Client Library its credentials before it loads the scene. Remote USD Loading names the calls.

Scenes Without a USD File#

A scene can also be built without any USD file: the helpers in ovphysx.population, such as create_physics_scenes, create_static_plane_colliders, and create_rigid_body_cubes, write objects straight into the stage. Changing the Scene uses one to add a box while the simulation runs.

More Detail#