Skip to content

[Fix] Defer pxr loading in isaaclab.sim, .assets, .scene, and .sim/utils (#5826) - #5901

Merged
kellyguo11 merged 1 commit into
isaac-sim:release/3.0.0-beta2from
hujc7:jichuanh/cherrypick-5826-release-3.0.0-beta2
Jun 2, 2026
Merged

kellyguo11 merged 1 commit into
isaac-sim:release/3.0.0-beta2from
hujc7:jichuanh/cherrypick-5826-release-3.0.0-beta2

Conversation

@hujc7

@hujc7 hujc7 commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

Cherry-pick of #5826 (#5826) into release/3.0.0-beta2.

1. Summary

  • Closes NVBug 6121889 on the 3.0 beta 2 release branch: direct Cartpole (Warp) + --visualizer kit crashed during SimulationApp.startup with the TfNotice / UsdAPISchemaBase / GfVec3f converter cascade.
  • Fix defers module-level from pxr import ... into the function bodies that use it across 9 modules on the env-cfg parse path, so Kit's USD-binding registration runs against a clean pxr instead of a partially-initialized one.

2. Cherry-pick details

  • Squash-merge commit feba3705d72 from develop, single-parent — clean cherry-pick, Auto-merging only (no conflict resolution needed) on isaaclab/sim/simulation_context.py and isaaclab/sim/utils/queries.py.
  • 10 files changed, +113 / -17. No public API change — import placement only.

3. Verification on this branch

  • ./isaaclab.sh -f clean.
  • Structural guard test_env_cfg_no_forbidden_imports.py::[Isaac-Cartpole-Direct-v0] passes inside nvcr.io/nvidian/isaac-lab:latest-develop (env-cfg parse loads 0 pxr/omni/carb/isaacsim modules).
  • Full rl_games train (5 epochs, --visualizer kit) completes; Kit visualizer backend registers, checkpoint written, Simulation App Shutting Down clean.
  • Literal failing command play.py --task Isaac-Cartpole-Direct-v0 --use_last_checkpoint --visualizer kit advances past SimulationApp.startup and Kit visualizer registration without the GfVec3f / TfNotice / UsdAPISchemaBase cascade.

4. Test plan

  • Pre-commit clean
  • Env-cfg structural guard (no backend imports on parse path)
  • Cartpole-Direct (Warp) + --visualizer kit train end-to-end
  • Cartpole-Direct (Warp) + --visualizer kit play with checkpoint reaches scene setup

…ils (isaac-sim#5826)

## 1. Summary

- Closes NVBug 6121889: the direct Cartpole env (Warp backend) +
``--visualizer kit`` crashes during ``SimulationApp.startup`` with a
``TfNotice`` / ``UsdAPISchemaBase`` / ``GfVec3f`` converter cascade.
- Root cause: module-import-time ``from pxr import …`` in IsaacLab
library code. Env-cfg parse runs *before* ``AppLauncher`` and
transitively pulls ``pxr`` into ``sys.modules``, so Kit's USD-binding
registration fails against an already partially-initialized ``pxr``.
- Fix: defer ``pxr`` into the function bodies that use it across the 9
modules on the env-cfg parse path. **0**
``pxr``/``omni``/``carb``/``isaacsim`` modules now leak across all
**197** ``Isaac-*`` env-cfg parses (the Cartpole path previously pulled
in 63 ``pxr``/``omni``/``isaacsim`` modules).
- No public API change: function bodies are otherwise unchanged; the
diff is import placement only.

## 2. Mechanism

### 2.1 Per-file pattern

Each module-top ``from pxr import …`` is removed. The runtime import
moves into each function that uses USD; type hints stay under
``TYPE_CHECKING`` so static checkers and IDE autocomplete keep resolving
``Usd.Stage``, ``Sdf.Path``, etc. against the real classes.

```python
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from pxr import Sdf, Usd  # noqa: F401

def some_fn(prim: Usd.Prim) -> Sdf.Path:
    from pxr import Sdf, Usd  # noqa: PLC0415
    ...
```

``# noqa: PLC0415`` matches the function-local import sites already
present across the repo; the ``TYPE_CHECKING`` guard matches the 200+
files already using ``if TYPE_CHECKING: from X import Y``.

### 2.2 Affected modules

| Module | pxr names deferred to function bodies |
|---|---|
| ``isaaclab.sim.simulation_context`` | ``Gf, UsdGeom, UsdPhysics,
UsdUtils`` |
| ``isaaclab.assets.asset_base`` | ``Usd`` (type-only — no runtime pxr
use) |
| ``isaaclab.scene.interactive_scene`` | ``Sdf`` |
| ``isaaclab.sim.spawners.from_files.from_files`` | ``Gf, Sdf, UsdGeom``
|
| ``isaaclab.sim.utils.prims`` | ``Sdf, Usd, UsdGeom, UsdPhysics,
UsdShade, UsdUtils`` |
| ``isaaclab.sim.utils.queries`` | ``Sdf, Usd, UsdPhysics`` |
| ``isaaclab.sim.utils.semantics`` | ``Usd, UsdGeom, UsdSemantics`` |
| ``isaaclab.sim.utils.stage`` | ``Sdf, Usd, UsdUtils`` |
| ``isaaclab.sim.utils.transforms`` | ``Gf, Sdf, Usd, UsdGeom`` |

## 3. Verification

- ``source/isaaclab_tasks/test/test_env_cfg_no_forbidden_imports.py``
(existing integration guard): all 197 ``Isaac-*`` tasks parse their
env-cfg with **0** ``pxr``/``omni``/``carb``/``isaacsim``/``scipy``
imports.
- Static check: every runtime ``pxr`` symbol use in the 9 modules is
covered by a function-local ``from pxr import …``; type-only uses remain
under ``TYPE_CHECKING`` (no module-scope ``pxr`` reference remains).
- ``./isaaclab.sh -f`` clean.

## 4. Test plan

- [x] ``./isaaclab.sh -f`` clean.
- [x] ``test_env_cfg_no_forbidden_imports.py`` — 197 tasks, no backend
leak.
- [x] Direct Cartpole (Warp) + ``--visualizer kit``: env-cfg parse loads
0 ``pxr`` modules; ``SimulationApp.startup`` proceeds without the
``TfNotice`` / ``GfVec3f`` cascade.
- [x] PhysX backend tests unaffected — function bodies unchanged,
``pxr`` still imports on first use.

## 5. Out of scope / follow-ups

~32 more files in ``isaaclab/``, ``isaaclab_newton/``,
``isaaclab_physx/`` still ``from pxr import …`` at module top. They are
not on the Cartpole-Warp env-cfg parse path so they don't block this
fix, but should be converted the same way as the bug class will
resurface for new envs that touch them:

- **Tier A — likely on env-cfg chains (~14 files):**
``cloner/cloner_utils``,
``sim/spawners/{lights,materials,meshes,shapes,sensors,wrappers}``,
``sim/schemas/schemas``, ``sim/utils/{legacy,newton_model_utils}``,
``sim/views/usd_frame_view``,
``isaaclab_newton/{assets,cloner,sensors,sim/views}``.
- **Tier B — PhysX equivalents (~13 files):** ``isaaclab_physx/*``.
- **Tier C — feature-gated, narrow trigger (~7 files):**
``sensors/{camera,ray_caster}``,
``envs/mdp/actions/task_space_actions``, ``envs/ui/base_env_window``,
``ui/xr_widgets``, ``sim/converters/mesh_converter``, ``utils/mesh``.
@github-actions github-actions Bot added the isaac-lab Related to Isaac Lab team label Jun 1, 2026

@isaaclab-review-bot isaaclab-review-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Isaac Lab Review Bot

Summary

Cherry-pick of #5826 that defers from pxr import ... statements from module-level to function-level across 10 files in isaaclab.sim, isaaclab.assets, and isaaclab.scene. This fixes a USD binding registration cascade failure (TfNotice/UsdAPISchemaBase/GfVec3f converter errors) when using --visualizer kit after Kit-less env-cfg parsing.

Design Assessment

Design is sound. Deferring pxr imports into function bodies is the correct approach to avoid polluting sys.modules before AppLauncher initializes Kit's USD bindings. The pattern is consistently applied: type hints moved under TYPE_CHECKING, runtime imports moved to function scope with # noqa: PLC0415 annotations.

Findings

🔵 Suggestion: Comment clarification in simulation_context.py — source/isaaclab/isaaclab/sim/simulation_context.py:991

The inline comment explains the shadowing issue, but could be clearer about why sim_utils.create_new_stage() is used instead of the local function.

Test Coverage

  • Bug fix: Yes — the PR description confirms the structural guard test (test_env_cfg_no_forbidden_imports.py) passes and full rl_games train/play workflows complete successfully.
  • New code: N/A — no new functionality, only import relocation.
  • Gaps: None identified. The verification plan is thorough.

CI Status

Not yet available — PR just opened.

Verdict

No issues found

This is a clean cherry-pick with a well-understood fix. The import deferral pattern is correctly and consistently applied across all affected modules. The PR description includes comprehensive verification steps. The one suggestion is purely cosmetic.

sim: SimulationContext | None = None
try:
if create_new_stage:
# ``create_new_stage`` is shadowed here by the bool parameter, so call via the namespace.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Suggestion: The comment is helpful, but could be slightly clearer:

Suggested change
# ``create_new_stage`` is shadowed here by the bool parameter, so call via the namespace.
# Call via the namespace because the local bool `create_new_stage` shadows the function name.
sim_utils.create_new_stage()

Minor wording tweak — feel free to ignore.

@isaaclab-review-bot isaaclab-review-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Isaac Lab Review Bot

Summary

This PR cherry-picks #5826 to release/3.0.0-beta2, deferring module-level from pxr import ... into function bodies across 9 modules on the env-cfg parse path. The fix prevents Kit's USD-binding registration from failing when pxr is prematurely loaded during env-cfg parsing. The cherry-pick is clean (auto-merge only, no conflict resolution).

Design Assessment

Architecture: Well-designed deferred import pattern.

The approach is sound and follows best practices for lazy loading of heavy/side-effectful dependencies:

  1. TYPE_CHECKING guards — All type-annotation-only uses of pxr symbols are placed under if TYPE_CHECKING: blocks. Since every affected file has from __future__ import annotations, these annotations are stored as strings and never trigger imports at runtime. ✅

  2. Function-local imports — Every runtime use of pxr symbols has a corresponding from pxr import ... inside the function body that uses it, marked with # noqa: PLC0415 (consistent with existing repo style). ✅

  3. No public API changes — The diff is purely import placement; function bodies, signatures, and return values are unchanged. ✅

  4. No circular import risk — pxr is an external USD library (provided by Kit/Pixar), so deferring its import cannot create circular dependencies within Isaac Lab. ✅

Findings

✅ No Issues Found

Completeness check (all 9 modules verified):

  • simulation_context.py: UsdUtils in __init__, Gf/UsdGeom/UsdPhysics in _init_usd_physics_scene — correct
  • asset_base.py: Usd moved to TYPE_CHECKING only (sole usage is self.stage: Usd.Stage annotation, safe under __future__.annotations) — correct
  • interactive_scene.py: Sdf in TYPE_CHECKING + runtime import in clone_environments conditional branch — correct
  • from_files.py: Gf/Sdf and UsdGeom deferred into conditional branches of spawn_ground_plane — correct
  • prims.py: 8 function-local imports covering all runtime uses of Sdf/Usd/UsdGeom/UsdPhysics/UsdShade/UsdUtils — correct
  • queries.py: Sdf and Usd/UsdPhysics deferred into 4 functions — correct
  • semantics.py: UsdSemantics/Usd/UsdGeom deferred into 5 functions — correct
  • stage.py: 6 functions each import exactly the pxr symbols they need — correct
  • transforms.py: 5 functions each import exactly the pxr symbols they need — correct

NameError risk: None. Every runtime code path that references a pxr symbol has a local import preceding it.

Performance: Python caches module imports in sys.modules, so repeated from pxr import X calls after first load are essentially free dict lookups — negligible overhead.

Cherry-pick cleanliness: Single squash-merge commit, auto-merge only, no conflict markers. The changelog entry is appropriate for a release branch.

Test Coverage

Well-covered:

  • Existing structural guard test_env_cfg_no_forbidden_imports.py validates that all 197 Isaac-* task env-cfg parses import zero pxr/omni/carb/isaacsim modules — this directly validates the fix
  • PR author verified end-to-end with --visualizer kit (train + play) for the specific failing scenario
  • Pre-commit (./isaaclab.sh -f) passes, confirming no import errors

Note: Since function bodies are unchanged, existing unit/integration tests that exercise these utilities (prim creation, deletion, stage operations, physics queries, etc.) implicitly validate that the deferred imports resolve correctly at runtime.

Verdict

No issues found — This is a clean, well-executed cherry-pick of a targeted import-deferral fix. The pattern is correct, complete, and consistent with existing repo conventions. Safe to merge.

@greptile-apps

greptile-apps Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR cherry-picks a deferred-import fix from the develop branch into release/3.0.0-beta2 to resolve a USD binding initialization crash (TfNotice / UsdAPISchemaBase / GfVec3f cascade) when Kit's --visualizer kit was combined with env-cfg parsing that triggered from pxr import … before SimulationApp.startup completed its binding registration.

  • Across 9 modules, every module-level from pxr import … is either moved behind if TYPE_CHECKING: (for pure type annotations, safe because all files carry from __future__ import annotations) or migrated into the first line of the function body that actually calls a pxr API at runtime.
  • No public API changes are introduced; all function signatures and return types remain identical — only the physical location of the import statement changes.

Confidence Score: 4/5

Safe to merge. The change is import-location only — no logic, no API surface, and no data flow is altered.

Every runtime call site that resolves a pxr class constructor (Usd.Stage.CreateInMemory, Gf.Vec3f, Sdf.ChangeBlock, UsdGeom.Imageable, etc.) has a matching deferred from pxr import … placed at the top of that function body. All remaining pxr references in function signatures are pure type annotations, made safe by the from __future__ import annotations present in every changed file. The one cosmetic concern is the superfluous if TYPE_CHECKING: from pxr import Sdf # noqa: F401 in interactive_scene.py, which is unused in annotations and only suppressed by the noqa comment, but it cannot cause a runtime failure.

interactive_scene.py has a redundant TYPE_CHECKING guard for Sdf that is unused in annotations and only suppressed via # noqa: F401; all other files are straightforward and correct.

Important Files Changed

Filename Overview
source/isaaclab/isaaclab/assets/asset_base.py Moves Usd to TYPE_CHECKING; self.stage: Usd.Stage annotation is a string at runtime due to PEP 563, so no runtime pxr call is added or removed.
source/isaaclab/isaaclab/scene/interactive_scene.py Moves Sdf to a second TYPE_CHECKING block and adds a local deferred import inside _setup_scene; the # noqa: F401 on the guard is needed because Sdf is not referenced in any annotation in this file, making the guard cosmetically redundant but harmless.
source/isaaclab/isaaclab/sim/simulation_context.py Defers UsdUtils into __init__ and Gf, UsdGeom, UsdPhysics into _init_usd_physics_scene; all runtime pxr call-sites are confirmed covered.
source/isaaclab/isaaclab/sim/spawners/from_files/from_files.py Conditional code paths for cfg.color and light_prim.IsValid() now each import only what they need; clean and minimal.
source/isaaclab/isaaclab/sim/utils/prims.py 9 functions each receive their own deferred import covering every pxr constructor called at runtime.
source/isaaclab/isaaclab/sim/utils/queries.py Moves Sdf, Usd, UsdPhysics to TYPE_CHECKING; deferred imports placed in all functions that call pxr class constructors at runtime.
source/isaaclab/isaaclab/sim/utils/semantics.py All five functions receive targeted deferred imports; clean and minimal.
source/isaaclab/isaaclab/sim/utils/stage.py Seven stage-utility functions each defer only the symbols they need; all runtime uses of Usd.Stage, Sdf.Layer, and UsdUtils.StageCache are covered.
source/isaaclab/isaaclab/sim/utils/transforms.py All five transform functions receive correct deferred imports covering every Gf, Sdf, Usd, and UsdGeom constructor used at runtime.

Sequence Diagram

sequenceDiagram
    participant User as User Script
    participant EL as Env-Cfg Parser
    participant AL as AppLauncher
    participant Kit as Kit/SimulationApp
    participant pxr as pxr (USD bindings)

    Note over User,pxr: BEFORE this PR (broken path)
    User->>EL: import isaaclab.assets / isaaclab.sim
    EL->>pxr: from pxr import Usd, Gf, Sdf, ... (module-level)
    pxr-->>EL: pxr partially initialised in sys.modules
    User->>AL: AppLauncher(kit_args)
    AL->>Kit: SimulationApp.startup()
    Kit->>pxr: Register USD bindings (TfNotice, UsdAPISchemaBase, GfVec3f converters)
    pxr-->>Kit: CRASH — bindings conflict with already-loaded pxr state

    Note over User,pxr: AFTER this PR (fixed path)
    User->>EL: import isaaclab.assets / isaaclab.sim
    EL-->>User: No pxr loaded (all imports deferred or under TYPE_CHECKING)
    User->>AL: AppLauncher(kit_args)
    AL->>Kit: SimulationApp.startup()
    Kit->>pxr: Register USD bindings cleanly
    pxr-->>Kit: Bindings registered successfully
    User->>EL: Instantiate scene / SimulationContext
    EL->>pxr: from pxr import ... (deferred, inside function body)
    pxr-->>EL: Fully initialised pxr available
Loading

Reviews (1): Last reviewed commit: "[Fix] Defer pxr loading in isaaclab.sim,..." | Re-trigger Greptile

Comment on lines +47 to +48
if TYPE_CHECKING:
from pxr import Sdf # noqa: F401

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Redundant TYPE_CHECKING guard for Sdf

Sdf is not referenced in any type annotation in this file — the only runtime use is inside _setup_scene via the local from pxr import Sdf deferred there. The # noqa: F401 is evidence of this: the guard is there purely to satisfy type checkers even though no signature in interactive_scene.py carries a Sdf.* annotation. The guard is harmless but unnecessary; you can drop it without any type-checker regression, since pyright/mypy will resolve Sdf from the deferred import inside the function body directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@hujc7 hujc7 added the bug Something isn't working label Jun 1, 2026
@kellyguo11
kellyguo11 merged commit b87079c into isaac-sim:release/3.0.0-beta2 Jun 2, 2026
60 of 61 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working isaac-lab Related to Isaac Lab team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants