-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Complete OVRTX cloning #5781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
huidongc
wants to merge
3
commits into
isaac-sim:develop
Choose a base branch
from
huidongc:heterogeneous-ovrtx-cloning
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Complete OVRTX cloning #5781
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
source/isaaclab_ov/changelog.d/ovrtx-heterogeneous-cloning.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| Changed | ||
| ^^^^^^^ | ||
|
|
||
| * Extended the :attr:`~isaaclab_ov.renderers.OVRTXRendererCfg.use_ovrtx_cloning` path to support | ||
| heterogeneous scenes as well as homogeneous ones. :meth:`~isaaclab_ov.renderers.OVRTXRenderer.prepare_stage` | ||
| now exports only :class:`~isaaclab.cloner.ClonePlan` source prototypes plus global stage metadata, and | ||
| replication uses per-row ``clone_usd`` calls from the published plan instead of cloning only | ||
| ``/World/envs/env_0``. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,12 @@ | |
|
|
||
| """USD manipulation for OVRTX: Render scope building, camera injection, and stage prim activation.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| import math | ||
| import re | ||
| from collections.abc import Callable | ||
|
|
||
| from pxr import Sdf, Usd, UsdGeom | ||
|
|
||
|
|
@@ -178,24 +182,17 @@ def build_render_product_as_string( | |
| def create_scene_partition_attributes( | ||
| stage, | ||
| num_envs: int = 1, | ||
| use_ovrtx_cloning: bool = True, | ||
| ) -> None: | ||
| """Create scene partition attributes for env roots and cameras. | ||
|
|
||
| If use_ovrtx_cloning is True, only env_0 is exported for OVRTX; env_1..env_{n-1} are deactivated before export. | ||
| OVRTX clones env_0 internally and _update_scene_partitions_after_clone sets partition attributes on the clones. | ||
| So we only need to set attributes on env_0 here. | ||
|
|
||
| Camera prims are discovered by USD type (``UsdGeom.Camera``) rather than by name, so this works regardless of | ||
| where the camera is placed in the hierarchy. | ||
|
|
||
| Args: | ||
| stage: USD stage to modify. | ||
| num_envs: Number of environments. | ||
| use_ovrtx_cloning: Whether OVRTX cloning is enabled. | ||
| """ | ||
| env_indices = [0] if use_ovrtx_cloning else range(num_envs) | ||
| for env_idx in env_indices: | ||
| for env_idx in range(num_envs): | ||
| env_path = f"/World/envs/env_{env_idx}" | ||
| env_prim = stage.GetPrimAtPath(env_path) | ||
| if not env_prim.IsValid(): | ||
|
|
@@ -217,38 +214,106 @@ def create_scene_partition_attributes( | |
| logger.debug("Set scene partition '%s' on camera '%s'", scene_partition, prim.GetPath()) | ||
|
|
||
|
|
||
| def export_stage_to_string(stage, num_envs: int, use_ovrtx_cloning: bool = True) -> str: | ||
| """Export the stage to a string; when num_envs > 1, only env_0 is exported for OVRTX cloning. | ||
| def _deactivate_child_prims( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic gets progressively more complex. Should we add unit test for this feature that we properly deactivate child prims? |
||
| prim: Usd.Prim, | ||
| source_paths: frozenset[Sdf.Path], | ||
| deactivated_prims: list[Usd.Prim], | ||
| should_keep_prim: Callable[[Sdf.Path], bool] | None = None, | ||
| ) -> None: | ||
| """Deactivate child prims under ``prim`` that are outside all source prototype subtrees. | ||
|
|
||
| For each child: | ||
|
|
||
| * **Source:** keep the full subtree and stop descending. | ||
| * **Ancestor of a source:** recurse to deactivate non-source siblings deeper in the tree. | ||
| * **Otherwise:** deactivate the child prim (including descendants). | ||
|
|
||
| Only prims that were active before deactivation are recorded in ``deactivated_prims`` for | ||
| reactivation after export. | ||
|
|
||
| Args: | ||
| prim: Parent prim whose children are considered. | ||
| source_paths: The paths to the cloning sources. | ||
| deactivated_prims: Prims deactivated by this call; used to reactivate them after export. | ||
| should_keep_prim: Optional predicate on each child path. If provided, a child prim for | ||
| which this returns ``True`` is retained (not deactivated and not descended). If not | ||
| provided, every child is considered. | ||
| """ | ||
| for child in list(prim.GetChildren()): | ||
| child_path = child.GetPath() | ||
|
|
||
| # If the optional predicate is provided and returns True, keep the prim and stop walking down the tree. | ||
| if should_keep_prim is not None and should_keep_prim(child_path): | ||
| continue | ||
|
|
||
| # If the child is a source, keep it and stop walking down the tree. | ||
| if child_path in source_paths: | ||
| continue | ||
|
|
||
| # If the child is an ancestor of some source, recurse to deactivate non-source siblings deeper in the tree. | ||
| if any(source.HasPrefix(child_path) for source in source_paths): | ||
| _deactivate_child_prims(child, source_paths, deactivated_prims, should_keep_prim) | ||
| continue | ||
|
|
||
| # Deactivate the child and record it for reactivation after export. | ||
| if child.IsActive(): | ||
| child.SetActive(False) | ||
| deactivated_prims.append(child) | ||
| logger.debug("Deactivated prim: %s", child_path) | ||
|
|
||
|
|
||
| def export_stage_to_string( | ||
| stage, | ||
| num_envs: int, | ||
| use_ovrtx_cloning: bool = True, | ||
| source_paths: tuple[str, ...] = (), | ||
| ) -> str: | ||
| """Export the USD stage as a USDA string for OVRTX loading. | ||
|
|
||
| When num_envs > 1, deactivates env_1..env_{num_envs-1} before export and reactivates | ||
| them after, so the exported content contains only env_0. The stage is modified in place. | ||
| When ``use_ovrtx_cloning`` is disabled or ``num_envs`` is 1, the full stage is exported | ||
| unchanged. Otherwise the stage is trimmed so OVRTX receives only the prototype geometry | ||
| it will replicate with ``clone_usd``. | ||
|
|
||
| Args: | ||
| stage: USD stage to export. | ||
| num_envs: Number of environments. | ||
| use_ovrtx_cloning: Whether OVRTX cloning is enabled. | ||
| num_envs: Number of parallel environments on the stage. | ||
| use_ovrtx_cloning: When ``True`` and ``num_envs > 1``, export only clone-plan prototypes; | ||
| otherwise export the full stage. | ||
| source_paths: The paths to source prims to clone. Required when ``use_ovrtx_cloning`` is ``True``. | ||
|
|
||
| Returns: | ||
| The exported stage as a string. | ||
| USDA text of the (possibly trimmed) stage. | ||
| """ | ||
| deactivated_prims = [] | ||
| if use_ovrtx_cloning and num_envs > 1: | ||
| logger.info("Deactivating %d environment roots...", num_envs - 1) | ||
| for env_idx in range(1, num_envs): | ||
| env_path = f"/World/envs/env_{env_idx}" | ||
| prim = stage.GetPrimAtPath(env_path) | ||
| if prim.IsValid() and prim.IsActive(): | ||
| prim.SetActive(False) | ||
| deactivated_prims.append(prim) | ||
| logger.debug("Deactivated environment root: %s", env_path) | ||
|
|
||
| logger.info("Deactivated %d environment roots in total", len(deactivated_prims)) | ||
| if not use_ovrtx_cloning or num_envs <= 1: | ||
| return stage.ExportToString() | ||
|
|
||
| envs_path = Sdf.Path("/World/envs") | ||
| envs_prim = stage.GetPrimAtPath(envs_path) | ||
| if not envs_prim.IsValid(): | ||
| raise RuntimeError(f"Failed to get prim at path: {envs_path}") | ||
|
|
||
| env_name_pattern = re.compile(r"^env_(\d+)$") | ||
|
|
||
| def _is_env_root(prim_path: Sdf.Path) -> bool: | ||
| """Return True if ``prim_path`` is an env root (e.g. ``/World/envs/env_0``).""" | ||
| return prim_path.GetParentPath() == envs_path and env_name_pattern.fullmatch(prim_path.name) is not None | ||
|
|
||
| deactivated_prims: list[Usd.Prim] = [] | ||
|
|
||
| _deactivate_child_prims( | ||
| envs_prim, | ||
| frozenset(map(Sdf.Path, source_paths)), | ||
| deactivated_prims, | ||
| should_keep_prim=lambda prim_path: not _is_env_root(prim_path), | ||
| ) | ||
|
|
||
| logger.info("Deactivated %d prims in total", len(deactivated_prims)) | ||
|
|
||
| try: | ||
| return stage.ExportToString() | ||
| finally: | ||
| if deactivated_prims: | ||
| logger.info("Reactivating %d environment roots...", len(deactivated_prims)) | ||
| logger.info("Reactivating %d prims...", len(deactivated_prims)) | ||
| for prim in deactivated_prims: | ||
| if prim.IsValid(): | ||
| prim.SetActive(True) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this runtime error please? Throwing an exception is more Pythonic way of handling runtime errors. Placing assertion here you essentially delay error downstream.
Python skips assertions when run in optimized mode
-O.