Selecting Objects#

Before your program reads or writes values, it picks the objects to act on. A query picks them by type, and a path pattern narrows them down to part of the scene.

A scene tree holds World, Shelf with BoxA and BoxB, and Floor with Ground and Ball. Three patterns type themselves in turn, and the rigid bodies that each one selects light up: /World/Shelf/* selects the two boxes, /World/** selects the boxes and the ball but not the ground, and /World/Box* finds the boxes inside Shelf.

Object Types#

A query selects objects of one type, a SimObjectType from ovphysx.types. The table lists the most common types. The ARTICULATION types are for articulations, such as robots.

Type

Selects

RIGID_BODY

Rigid bodies that are not part of an articulation, such as loose boxes and balls.

ARTICULATION

Whole articulations, one row each.

ARTICULATION_LINK

The links of articulations.

ARTICULATION_JOINT

The joints of articulations, with one value for each axis that can move.

CONTACT_SENSOR

Bodies that report the contact forces they receive.

DEFORMABLE_VOLUME

Soft bodies. They require a GPU.

PARTICLE_SET

Sets of particles. They require a GPU.

Each type has its own attributes. Object types lists every type.

Path Patterns#

A path pattern selects objects by their place in the scene tree, using prim paths such as /World/Shelf/BoxA. Pass it as the pattern argument. ovphysx compares it with each path one level at a time, so a part between two slashes matches one name:

Pattern Part

Matches

*

Any characters within one name. Box* matches BoxA and BoxB.

?

Exactly one character.

**

Any number of levels, when it is a whole part, as in /World/**.

A name without wildcards

That exact name.

The last name in a pattern is also searched for deeper in the tree, so /World/Box* finds /World/Shelf/BoxA. Path Patterns has the exact rules.

A pattern must start with /. A pattern that selects nothing is not an error: the query is empty.

Trying Patterns#

The following program, selecting_objects_patterns.py, loads the scene from the figure. For each of four patterns, it reads the rigid bodies that the pattern selects and prints the path of each row:

The imports and the scene, in USD
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

# Two boxes under Shelf, and the ground and a ball under Floor.
# The boxes and the ball are rigid bodies; the ground is not.
SCENE = """#usda 1.0
(
    metersPerUnit = 1
    kilogramsPerUnit = 1
    upAxis = "Z"
)

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

    def Xform "Shelf"
    {
        def Cube "BoxA" (
            prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
        )
        {
            double size = 0.2
            double3 xformOp:translate = (-0.3, 0, 1.1)
            uniform token[] xformOpOrder = ["xformOp:translate"]
        }

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

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

        def Sphere "Ball" (
            prepend apiSchemas = ["PhysicsRigidBodyAPI", "PhysicsCollisionAPI"]
        )
        {
            double radius = 0.1
            double3 xformOp:translate = (0, 0, 0.1)
            uniform token[] xformOpOrder = ["xformOp:translate"]
        }
    }
}
"""


def selected_paths(physx, pattern):
    """Return the path of every rigid body that the pattern selects."""
    paths = []
    with physx.read_sync(
        SimObjectType.RIGID_BODY, ["position"], pattern=pattern
    ) as result:
        for group in result.groups:
            paths += result.dictionary.get_path_strings(group.prim_list)
    return paths


ovstage.population.register_usd_schemas([str(p) for p in physx_schema_paths()])
stage = ovstage.Stage("selecting_objects")
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))

for pattern in ["/World/Shelf/*", "/World/**", "/World/Floor/Ball", "/World/Box*"]:
    print(f"{pattern:<18} {', '.join(selected_paths(physx, pattern))}")

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

It prints:

/World/Shelf/*     /World/Shelf/BoxB, /World/Shelf/BoxA
/World/**          /World/Shelf/BoxB, /World/Shelf/BoxA, /World/Floor/Ball
/World/Floor/Ball  /World/Floor/Ball
/World/Box*        /World/Shelf/BoxB, /World/Shelf/BoxA

Notice the following:

  • /World/** also matches the ground, but the query keeps only rigid bodies, and the ground is not one.

  • The rows come back as BoxB, then BoxA, not in the order of the scene file, so tell objects apart by their paths, as Reading Values shows.

Keeping a Query#

read_sync and write_sync create a new query, and match the pattern again, on every call. A program that reads or writes the same objects at every step can create the query once, with query = physx.query(SimObjectType.RIGID_BODY, pattern="/World/Shelf/*").wait(). It then passes the query to physx.read and physx.write at every step, and calls query.close() at the end. Reads and writes through a kept query also reuse their setup work, so each one costs less. A query with a pattern keeps the objects it matched when you created it. The session model shows a complete loop.

More Detail#