Showing the Simulation#

ovphysx computes motion but draws nothing. To see the motion, your program passes the results to a renderer, a library that turns a scene into pictures.

Three panels joined by arrows. On the left, ovphysx steps the simulation. In the middle, the ovstage stage lists six boxes with the height from each box's world transform. The heights fall as the program writes each step and seals a new ordinal. On the right, a renderer shows the same six boxes falling onto a dark floor and coming to rest.

Two Ways#

Write Into the Stage

Hand the Arrays to Your Renderer

After each step

Call step_and_write_to_ovstage, which also steps

Read position and orientation

Renderer

One that reads the ovstage stage, such as ovrtx, NVIDIA’s RTX rendering library

Any renderer, such as your own engine

Shapes, lights, and cameras

Already in the stage

Your renderer loads or builds them

Write the Results Into the Stage#

ovphysx never writes to the stage on its own, as Concepts explains. With a scene attached, the helper step_and_write_to_ovstage from ovphysx.utils does it in one call:

  1. It steps the simulation by dt.

  2. It writes each body’s new pose into the stage, where a renderer reads it.

  3. It seals the ordinal that you pass.

This program, showing_the_simulation_write_back.py, runs the falling box from Your First Simulation, writes its pose into the stage after every step, and reads it back from the stage:

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

# The scene from Your First Simulation: a ground plane and a 1 m box whose
# center starts 2 m above it.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

def Xform "World"
{
    def PhysicsScene "PhysicsScene" {}

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

    def Cube "Box" (
        prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
    )
    {
        double size = 1
        double3 xformOp:translate = (0, 0, 2)
        uniform token[] xformOpOrder = ["xformOp:translate"]
    }
}
"""
How the program reads the stage


def stage_height(stage, ordinal):
    """Read the box's world transform back from the stage and return its z."""
    with (
        ovstage.PathDictionary(stage) as names,
        names.create_path_list_from_strings(["/World/Box"]) as box,
        stage.query_from_path_list(box) as query,
        stage.read_attributes(
            query,
            [names.intern_token("omni:fabric:worldMatrix")],
            ovstage.OrdinalRange.latest(ordinal),
        ) as read,
    ):
        for group in read.wait().groups():
            with group:
                # A 4x4 matrix; its last row holds the position.
                matrix = group.columns[0].array(0)[0]
                return float(matrix[3, 2])


# ovstage must know the PhysX USD schemas before it reads a scene.
ovstage.population.register_usd_schemas([str(p) for p in physx_schema_paths()])

# Load the scene, including what a renderer needs, and seal it at ordinal 1.
stage = ovstage.Stage("write_back")
ovstage.population.open_usd_from_string_with_desc(
    stage, SCENE, ordinal=1, time=ovstage.population.DEFAULT_TIME, descs=physics_population_descs(rendering=True)
)
stage.advance_write_floor(ordinal=1).wait()

physx = PhysX()
physx.wait_task(physx.attach_ovstage(stage, read_ordinal=1))

print(f"t=0.00 s  box z in the stage = {stage_height(stage, 1):.3f} m")
for step in range(1, 61):
    # Step 1/60 s, write each rigid body's pose into the stage at a new
    # ordinal, and seal that ordinal.
    ordinal = 1 + step
    step_and_write_to_ovstage(
        physx,
        dt=1.0 / 60.0,
        output_ordinal=ordinal,
        outputs={SimObjectType.RIGID_BODY: ["position", "orientation"]},
    )
    if step % 10 == 0:
        z = stage_height(stage, ordinal)
        print(f"t={step / 60:.2f} s  box z in the stage = {z:.3f} m")

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

It prints:

t=0.00 s  box z in the stage = 2.000 m
t=0.17 s  box z in the stage = 1.863 m
t=0.33 s  box z in the stage = 1.453 m
t=0.50 s  box z in the stage = 0.771 m
t=0.67 s  box z in the stage = 0.500 m
t=0.83 s  box z in the stage = 0.500 m
t=1.00 s  box z in the stage = 0.500 m

Notice the following:

  • The heights match those of Your First Simulation, because the stage now holds the simulated pose.

  • Each call writes at a new ordinal, here 2 to 61. Give every call an ordinal higher than the last.

  • physics_population_descs(rendering=True) also loads the meshes, lights, materials, and cameras that a renderer needs.

The helper sets the transform of each body only. Prims nested under a body, such as the meshes of a robot link, keep the transforms they had.

If your program also edits the scene, never pass these output ordinals to update_from_ovstage. The Key Principle explains why.

Hand the Arrays to Your Own Renderer#

After each step, read position and orientation of every body, as Reading Values shows, and move the matching object in your renderer. Keep these points in mind:

  • orientation is a quaternion in x, y, z, w order. Check which order your renderer expects.

  • The results hold no scale. Keep each object’s size from your scene.

Rendering Handoff streams these arrays to the Rerun viewer. Its sample is in the source repository, not in the Python package.

More Detail#