Repository navigation
[Fix] Defer pxr loading in isaaclab.sim, .assets, .scene, and .sim/utils (#5826) - #5901
Conversation
…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``.
There was a problem hiding this comment.
🤖 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 fullrl_gamestrain/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. |
There was a problem hiding this comment.
🔵 Suggestion: The comment is helpful, but could be slightly clearer:
| # ``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.
There was a problem hiding this comment.
🤖 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:
-
TYPE_CHECKINGguards — All type-annotation-only uses ofpxrsymbols are placed underif TYPE_CHECKING:blocks. Since every affected file hasfrom __future__ import annotations, these annotations are stored as strings and never trigger imports at runtime. ✅ -
Function-local imports — Every runtime use of
pxrsymbols has a correspondingfrom pxr import ...inside the function body that uses it, marked with# noqa: PLC0415(consistent with existing repo style). ✅ -
No public API changes — The diff is purely import placement; function bodies, signatures, and return values are unchanged. ✅
-
No circular import risk —
pxris 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:UsdUtilsin__init__,Gf/UsdGeom/UsdPhysicsin_init_usd_physics_scene— correctasset_base.py:Usdmoved toTYPE_CHECKINGonly (sole usage isself.stage: Usd.Stageannotation, safe under__future__.annotations) — correctinteractive_scene.py:SdfinTYPE_CHECKING+ runtime import inclone_environmentsconditional branch — correctfrom_files.py:Gf/SdfandUsdGeomdeferred into conditional branches ofspawn_ground_plane— correctprims.py: 8 function-local imports covering all runtime uses ofSdf/Usd/UsdGeom/UsdPhysics/UsdShade/UsdUtils— correctqueries.py:SdfandUsd/UsdPhysicsdeferred into 4 functions — correctsemantics.py:UsdSemantics/Usd/UsdGeomdeferred into 5 functions — correctstage.py: 6 functions each import exactly thepxrsymbols they need — correcttransforms.py: 5 functions each import exactly thepxrsymbols 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.pyvalidates that all 197Isaac-*task env-cfg parses import zeropxr/omni/carb/isaacsimmodules — 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 SummaryThis PR cherry-picks a deferred-import fix from the
Confidence Score: 4/5Safe 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 (
Important Files Changed
Sequence DiagramsequenceDiagram
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
Reviews (1): Last reviewed commit: "[Fix] Defer pxr loading in isaaclab.sim,..." | Re-trigger Greptile |
| if TYPE_CHECKING: | ||
| from pxr import Sdf # noqa: F401 |
There was a problem hiding this comment.
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!
b87079c
into
isaac-sim:release/3.0.0-beta2
Cherry-pick of #5826 (#5826) into
release/3.0.0-beta2.1. Summary
--visualizer kitcrashed duringSimulationApp.startupwith theTfNotice/UsdAPISchemaBase/GfVec3fconverter cascade.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 cleanpxrinstead of a partially-initialized one.2. Cherry-pick details
feba3705d72from develop, single-parent — clean cherry-pick,Auto-mergingonly (no conflict resolution needed) onisaaclab/sim/simulation_context.pyandisaaclab/sim/utils/queries.py.3. Verification on this branch
./isaaclab.sh -fclean.test_env_cfg_no_forbidden_imports.py::[Isaac-Cartpole-Direct-v0]passes insidenvcr.io/nvidian/isaac-lab:latest-develop(env-cfg parse loads 0pxr/omni/carb/isaacsimmodules).rl_gamestrain (5 epochs,--visualizer kit) completes; Kit visualizer backend registers, checkpoint written,Simulation App Shutting Downclean.play.py --task Isaac-Cartpole-Direct-v0 --use_last_checkpoint --visualizer kitadvances pastSimulationApp.startupand Kit visualizer registration without theGfVec3f/TfNotice/UsdAPISchemaBasecascade.4. Test plan
--visualizer kittrain end-to-end--visualizer kitplay with checkpoint reaches scene setup