Cloning – Replicate Environments#

This tutorial shows how to use the clone API to replicate sub-sections of a USD scene. Cloning creates copies in the internal physics representation (not USD prims), optimized for large-scale parallel simulation.

Prerequisites#

  • Complete the Hello World tutorial.

  • A USD scene with a hierarchy suitable for cloning (for example /World/envs/env0). This tutorial uses basic_simulation.usda, which ships with every package under ovphysx/samples/data/ in the wheel, <sdk-root>/samples/data/ in the C/C++ SDK, and tests/data/ in a repository checkout. The source can be any prim subtree that carries physics: an Xform environment root such as /World/envs/env0, or a single rigid-body prim with no wrapper (for example a Cube that itself carries PhysicsRigidBodyAPI). Clones are runtime physics only, so no USD prim has to exist at the target paths.

Key Concepts#

Stage and physics copies are separate.

  • Direct ovphysx API (PhysX.clone() / ovphysx_clone()) — the approach used in this tutorial. After an ovstage-backed scene is attached and drained, clone() copies a source subtree in the internal physics representation. No USD prims are created, so it stays fast and memory-light at large environment counts.

  • ovstage copies (Stage.clone() / ovstage_clone) duplicate the populated stage data for other consumers. When using both APIs, complete the attach before cloning the stage. Clone the same source and targets separately in ovphysx, before warmup or the first step. Use a dedicated ordinal for the stage clone, seal it, and exclude it from every update_from_ovstage() range. Forwarding that ordinal can ingest the copied physics data as new scene objects; it does not invoke the PhysX replicator.

Refer to Scene Cloning for ordinal ordering and the effect of later structural changes.

Tensor-binding path patterns include direct ovphysx runtime clones even when an intermediate target path has no authored USD prim. For example, after cloning /World/envs/env0/robot to /World/envs/env1/robot, the pattern /World/envs/env*/robot resolves both objects.

Do not use authored-ancestor deletion to remove runtime clones. PhysX.clone() and ovphysx_clone() copies exist only in the physics runtime. They have no authored prim. In a sibling-environment layout, cloning /World/envs/env0 to /World/envs/env1 and then deleting /World/envs through ovstage removes the authored source bodies but leaves the runtime clone addressable. This is not a general guarantee that clones survive ancestor deletion in other layouts.

Clone before warmup. Call clone() before warmup and before the first step(). A later clone fails because changing the physics layout would corrupt initialized solver state. Python reports this as TaskError when you wait. To clone later, detach, wait, reload or reattach the source Stage, then clone. See Warmup and Determinism.

Grouping copies with env_ids. A single clone() call numbers its copies automatically. One logical environment is sometimes assembled from several clone() calls — for example, first every environment’s robot, then every environment’s object. In that case, pass the optional per-target env_ids with the same ids in every call. Copies sharing an id land in the same runtime environment and can collide.

Mixing automatic and explicit ids can join environments unintentionally. The automatic numbering base belongs to the attachment; explicit logical ids always map to runtime id id + 1. With environment-id filtering active on a fresh attachment, a one-target automatic call uses runtime id 1. A later call with env_ids=[0] also uses runtime id 1, so those copies can collide. In the reverse order, the explicit call raises the base and the automatic call uses runtime id 2. There is no warning or error for this overlap. Choose one ID strategy for an attachment; use explicit ids consistently when you need to control grouping across calls.

Environment ids provide cross-environment collision isolation only when the scene uses GPU dynamics and GPU broadphase. They do not isolate CPU clones. In CPU mode, give every clone a spatially disjoint anchor_transforms pose; co-located clones share one collision space and can push each other apart.

When env ids are requested but the scene runs CPU dynamics or a CPU broadphase, the runtime logs EnvIds requested but gpu dynamic is disabled and EnvIds requested but gpu broadphase is not set. Those records go to the Carbonite log, not to Python’s warnings module or sys.stderr, so a caller who wants to observe, assert on, or escalate them has to attach a log consumer: ovphysx.enable_python_logging() routes them to the ovphysx Python logger, and ovphysx_set_log_callback() delivers them to a C callback. Refer to Logging.

Code Language#

Python#

This complete sample attaches basic_simulation.usda, clones /World/envs/env0 into three spatially disjoint targets before the first step, runs 10 steps, and reads back one rigid-body position per environment:

# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# @implements REQ-PYTHON-CLONE-001
# @covers AC-1

# NOTE: This file is included verbatim in documentation via literalinclude.

"""
Clone sample demonstrating scene replication with the clone API.

This sample demonstrates:
1. Loading a USD scene with an environment hierarchy
2. Cloning the environment to create multiple copies
3. Running simulation with all clones
"""

# @implements REQ-CAPI-OVSTAGE-XFORM-001
# @covers AC-1

from pathlib import Path

import numpy as np

import ovphysx
from ovphysx.samples.python_samples.schema_support import physx_schema_paths
from ovphysx import PhysX
from ovphysx.types import ObjectScope, SimObjectType


def _to_host(column):
    """A read column as host NumPy, whether it came back as NumPy (CPU) or a Warp array (GPU)."""
    return column if isinstance(column, np.ndarray) else column.numpy()


_physx_schemas_registered = False


def attach_scene(physx, usd_path, stage_name):
    import ovstage
    from ovphysx.population import physics_population_descs

    if not ovstage.population.available():
        raise RuntimeError("ovstage population bridge is unavailable")

    # Register external PhysX and Newton schemas before the first population.
    global _physx_schemas_registered
    if not _physx_schemas_registered:
        ovstage.population.register_usd_schemas(
            [*[str(p) for p in physx_schema_paths()], str(ovphysx.newton_schema_root())]
        )
        _physx_schemas_registered = True
    stage = ovstage.Stage(stage_name)
    ordinal = 1
    try:
        ovstage.population.open_usd_with_desc(stage, str(usd_path), ordinal=ordinal, time=ovstage.population.DEFAULT_TIME, descs=physics_population_descs(rendering=True))
        stage.advance_write_floor(ordinal=ordinal).wait()
        physx.wait_task(physx.attach_ovstage(stage, read_ordinal=ordinal))
        return stage
    except Exception:
        if physx.is_stage_attached(stage):
            physx.wait_task(physx.detach_ovstage())
        stage.destroy()
        raise


def main():
    # Initialize PhysX SDK
    PhysX.set_cpu_mode(True)
    physx = PhysX()
    stage = None

    try:
        # Prefer package data so a copied sample works. Fall back to the checked-in
        # sample's adjacent data directory when package data is absent.
        usd_path = (
            Path(ovphysx.__file__).resolve().parent
            / "samples"
            / "data"
            / "basic_simulation.usda"
        )
        if not usd_path.is_file():
            usd_path = Path(__file__).resolve().parent.parent / "data" / "basic_simulation.usda"
        if not usd_path.is_file():
            raise FileNotFoundError(f"ovphysx sample data is missing: {usd_path}")

        print(f"Loading USD scene through ovstage: {usd_path}")
        stage = attach_scene(physx, usd_path, "ovphysx-clone-sample")
        physx.wait_all()

        # Clone env0 to create env1, env2, env3
        targets = ["/World/envs/env1", "/World/envs/env2", "/World/envs/env3"]
        anchor_transforms = [
            (4.0 * env_idx, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0)
            for env_idx in range(1, len(targets) + 1)
        ]
        print(f"Cloning /World/envs/env0 to {len(targets)} targets...")
        # CPU mode has no environment-id collision filtering, so place each
        # environment in a spatially disjoint lane.
        physx.clone("/World/envs/env0", targets, anchor_transforms=anchor_transforms)
        physx.wait_all()
        print(f"  Created {len(targets)} clones successfully")

        # Run simulation with all environments
        print("Running 10 simulation steps...")
        dt = 1.0 / 60.0
        for i in range(10):
            physx.step(dt)
        physx.wait_all()
        print("  All steps completed")

        # Read rigid-body positions across all environments via the session read API. After
        # cloning, the tables are the scene's only rigid bodies, so reading RIGID_BODY covers
        # exactly them, one row per environment.
        with physx.read_sync(SimObjectType.RIGID_BODY, ["position"], scope=ObjectScope.ALL) as result:
            positions = np.concatenate(
                [_to_host(group.tensors[0]).reshape(group.prim_count, -1) for group in result.groups]
            )
        for env_idx in range(positions.shape[0]):
            px, py, pz = positions[env_idx, 0:3]
            print(f"  env{env_idx}: pos=({px:.4f}, {py:.4f}, {pz:.4f})")

        print("Clone sample completed successfully")

    finally:
        # A closed read result's arrays are invalid; drop the last reference to it.
        result = None
        if stage is not None:
            physx.wait_task(physx.detach_ovstage())
            stage.destroy()
        physx.destroy()
        print("Cleanup complete")


if __name__ == "__main__":
    main()

C#

The C sample performs the same three-target clone, passing NULL for env_ids so the call numbers its copies automatically:

// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

/**
 * @implements REQ-PYTHON-CLONE-001
 * @covers AC-1
 */

// NOTE: This file is included verbatim in the documentation via literalinclude.

#include <ovphysx/ovphysx.h>
#include <ovphysx/ovphysx_types.h>
#include "ovstage_sample.h"
#include <stdio.h>

#ifdef _WIN32
#include <windows.h>
#define sleep_ms(ms) Sleep(ms)
#else
#include <unistd.h>
#define sleep_ms(ms) usleep((ms) * 1000)
#endif

// The sample exercises the plain C API, so a C++ compiler is rejected.
#ifdef __cplusplus
#error "This file must be compiled as C, not C++"
#endif

static int wait_task_success(
    ovphysx_instance_handle_t handle,
    ovphysx_enqueue_result_t res,
    ovphysx_timeout_t timeout_ns) {
  if (res.status != OVPHYSX_API_SUCCESS) {
    return 0;
  }
  ovphysx_wait_task_result_t wait_result = {0};
  ovphysx_result_t wait_res = ovphysx_wait_task(handle, res, timeout_ns, &wait_result);
  int success = (wait_res.status == OVPHYSX_API_SUCCESS && wait_result.num_errors == 0);
  ovphysx_destroy_wait_task_result(&wait_result);
  return success;
}

static int run(void)
{
  printf("=== ovphysx Clone Example (C API) ===\n\n");

  ovphysx_result_t init_res = ovphysx_initialize();
  if (init_res.status != OVPHYSX_API_SUCCESS) {
    fprintf(stderr, "Failed to initialize ovphysx\n");
    return 1;
  }

  printf("Creating PhysX instance...\n");
  ovphysx_instance_handle_t handle = 0;
  ovphysx_result_t create_res = ovphysx_create_instance(&handle);
  if (create_res.status != OVPHYSX_API_SUCCESS) {
    fprintf(stderr, "Failed to create PhysX instance\n");
    ovphysx_shutdown();
    return 1;
  }
  printf("  [OK] PhysX instance created\n\n");

  // Populate an ovstage instance from the USD file and attach it to ovphysx.
  printf("Loading USD scene...\n");
  ovphysx_sample_stage_attachment_t stage_attachment = {0};
  if (!ovphysx_sample_attach_usd_with_ovstage(
          handle, OVPHYSX_TEST_DATA "/basic_simulation.usda", &stage_attachment)) {
    fprintf(stderr, "ovstage attach/update failed\n");
    ovphysx_destroy_instance(handle);
    ovphysx_shutdown();
    return 1;
  }
  printf("  [OK] USD scene loaded\n\n");

  // Clone env0 into three new environments.
  printf("Cloning /World/envs/env0 to create env1, env2, env3...\n");
  const char* clone_targets[] = {
    "/World/envs/env1",
    "/World/envs/env2",
    "/World/envs/env3"
  };
  enum { NUM_TARGETS = 3 };

  ovphysx_string_t target_strings[NUM_TARGETS];
  for (uint32_t i = 0; i < NUM_TARGETS; ++i) {
    target_strings[i] = ovphysx_cstr(clone_targets[i]);
  }

  // CPU mode has no environment-id collision filtering, so place each
  // environment in a spatially disjoint lane.
  const float anchor_transforms[NUM_TARGETS * 7] = {
      4.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
      8.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
      12.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f
  };

  ovphysx_enqueue_result_t clone_res = ovphysx_clone(
      handle,
      ovphysx_cstr("/World/envs/env0"),
      target_strings,
      NUM_TARGETS,
      anchor_transforms,
      NULL);  /* env_ids: NULL selects automatic numbering from a per-attachment base. */
  if (!wait_task_success(handle, clone_res, 10ULL * 1000 * 1000 * 1000)) {
    fprintf(stderr, "Clone operation failed or timed out\n");
    ovphysx_sample_destroy_stage(handle, &stage_attachment);
    ovphysx_destroy_instance(handle);
    ovphysx_shutdown();
    return 1;
  }
  printf("  [OK] Created 3 clones successfully\n\n");

  // A few simulation steps confirm the clones simulate.
  printf("Running simulation with clones (10 steps)...\n");
  for (int i = 0; i < 10; i++) {
    ovphysx_enqueue_result_t step_res = ovphysx_step(handle, 1.0f/60.0f);
    if (!wait_task_success(handle, step_res, 10ULL * 1000 * 1000 * 1000)) {
      fprintf(stderr, "Failed to run simulation step %d\n", i);
      ovphysx_sample_destroy_stage(handle, &stage_attachment);
      ovphysx_destroy_instance(handle);
      ovphysx_shutdown();
      return 1;
    }
  }
  printf("  [OK] All 10 simulation steps completed successfully\n\n");

  printf("=== Clone Example Completed Successfully ===\n");

  ovphysx_sample_destroy_stage(handle, &stage_attachment);
  ovphysx_destroy_instance(handle);
  ovphysx_shutdown();
  printf("Cleanup complete\n");

  return 0;
}

int main(void) {
  int rc = run();
  return rc;
}

Result#

After this tutorial, you can replicate environments through the clone API and simulate all copies together.