Repository navigation
Adding Rigid Body Material USD data classes and writers #6287
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
Changes from all commits
182dcb9
17d7c0a
b8ba64e
bbf9c71
bd0f063
a8bc6aa
2d70ad4
bb3226f
85592af
435240e
e0e647d
31abf2c
c84801e
ffa19f3
1983a59
9553f38
21b2a5e
3c86ca6
e1bb60b
419d1c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| Added | ||
| ^^^^^ | ||
|
|
||
| * Added the rigid-body physics-material "fragment" classes | ||
| :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialFragment` (marker base) and | ||
| :class:`~isaaclab.sim.spawners.materials.UsdPhysicsRigidBodyMaterialCfg` (solver-common | ||
| ``physics:*`` friction/restitution/density), plus the family writer | ||
| :func:`~isaaclab.sim.spawners.materials.spawn_rigid_body_material_from_fragments` and the slot | ||
| dispatcher :func:`~isaaclab.sim.spawners.materials.spawn_physics_material`. Relevant | ||
| ``physics_material`` slots now accept a single fragment or list alongside their legacy cfg form. | ||
| Legacy material cfgs are current-stage-only: the dispatcher raises ``ValueError`` for an explicit | ||
| non-current stage, while fragment-based materials support explicit-stage authoring. | ||
|
|
||
| * Added :attr:`~isaaclab.sim.spawners.materials.UsdPhysicsRigidBodyMaterialCfg.density` (writes | ||
| ``physics:density``), completing the fragment's coverage of ``UsdPhysics.MaterialAPI``. | ||
| * Added :attr:`~isaaclab.sim.spawners.materials.RigidBodyMaterialBaseCfg.density`, so the legacy | ||
| rigid material base authors every attribute the fragment authors. | ||
|
|
||
| Changed | ||
| ^^^^^^^ | ||
|
|
||
| * Narrowed :attr:`~isaaclab.sim.spawners.ShapeCfg.physics_material` from the broad | ||
| :class:`~isaaclab.sim.spawners.materials.PhysicsMaterialCfg` to the rigid material base, a single | ||
| rigid-material fragment, or a list of fragments. | ||
| * Extended the already rigid-only :attr:`~isaaclab.sim.spawners.GroundPlaneCfg.physics_material` | ||
| and :attr:`~isaaclab.terrains.TerrainImporterCfg.physics_material` slots from the legacy | ||
| :class:`~isaaclab_physx.sim.spawners.materials.RigidBodyMaterialCfg` type to | ||
| :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialBaseCfg`, a single | ||
| :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialFragment`, or a list of fragments. | ||
| Deformable materials remain accepted where deformables can spawn | ||
| (:class:`~isaaclab.sim.spawners.FileCfg` and :class:`~isaaclab.sim.spawners.MeshCfg`). | ||
| * Changed the generated-terrain material path to use | ||
| :func:`~isaaclab.sim.spawners.materials.spawn_physics_material`, enabling fragment lists there. | ||
|
|
||
| Fixed | ||
| ^^^^^ | ||
|
|
||
| * Fixed mesh spawners rejecting valid rigid physics materials: the rigid-material check compared | ||
| against the deprecated leaf class, so canonical | ||
| :class:`~isaaclab_physx.sim.spawners.materials.PhysxRigidBodyMaterialCfg` and | ||
| :class:`~isaaclab_newton.sim.schemas.NewtonMaterialPropertiesCfg` instances raised | ||
| ``ValueError`` on rigid meshes. The check now accepts any | ||
| :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialBaseCfg`. | ||
| * Fixed malformed fragment inputs reaching an opaque legacy ``func`` call. Direct and dispatched | ||
| fragment calls now reject empty, mixed, and non-fragment inputs before creating a material prim. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,10 +12,138 @@ | |||||||||||||||||||||||||
| from isaaclab.sim.schemas.schemas import _apply_namespaced_schemas | ||||||||||||||||||||||||||
| from isaaclab.sim.utils import clone | ||||||||||||||||||||||||||
| from isaaclab.sim.utils.stage import get_current_stage | ||||||||||||||||||||||||||
| from isaaclab.utils.string import string_to_callable | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| from . import physics_materials_cfg | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def spawn_rigid_body_material_from_fragments( | ||||||||||||||||||||||||||
| prim_path: str, | ||||||||||||||||||||||||||
| fragments: physics_materials_cfg.RigidBodyMaterialFragment | ||||||||||||||||||||||||||
| | list[physics_materials_cfg.RigidBodyMaterialFragment] | ||||||||||||||||||||||||||
| | tuple[physics_materials_cfg.RigidBodyMaterialFragment, ...], | ||||||||||||||||||||||||||
| stage: Usd.Stage | None = None, | ||||||||||||||||||||||||||
| ) -> Usd.Prim: | ||||||||||||||||||||||||||
| """Spawn a rigid-body physics material from one or more single-namespace fragments. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Creates (or reuses) the ``UsdShade.Material`` prim at ``prim_path``, applies the standard | ||||||||||||||||||||||||||
| ``UsdPhysics.MaterialAPI`` anchor, then dispatches each fragment via its | ||||||||||||||||||||||||||
| :attr:`~isaaclab.sim.schemas.SchemaFragment.func` to author its namespace onto the material prim. | ||||||||||||||||||||||||||
| Backend fragments carry backend-specific namespaces (e.g. PhysX ``physxMaterial:*``) without core | ||||||||||||||||||||||||||
| importing a backend. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| .. note:: | ||||||||||||||||||||||||||
| Unlike the ``@clone``-decorated :func:`spawn_rigid_body_material`, this writer expects a | ||||||||||||||||||||||||||
| concrete ``prim_path`` and does not resolve regex prim-path patterns. Physics materials are | ||||||||||||||||||||||||||
| spawned at a single derived path by :func:`spawn_physics_material` and the spawner internals, | ||||||||||||||||||||||||||
| so regex resolution does not apply here. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| prim_path: The prim path to spawn the material at. | ||||||||||||||||||||||||||
| fragments: A single :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialFragment`, | ||||||||||||||||||||||||||
| or a list/tuple of them. | ||||||||||||||||||||||||||
| stage: The stage to spawn on. Defaults to None, in which case the current stage is used. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||
| The spawned rigid body material prim. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||||||
| ValueError: When ``fragments`` is empty. | ||||||||||||||||||||||||||
| ValueError: When a prim already exists at the path and is not a material. | ||||||||||||||||||||||||||
| TypeError: When an item is not a rigid-body material fragment. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| if not isinstance(fragments, (list, tuple)): | ||||||||||||||||||||||||||
| fragments = [fragments] | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| fragments = list(fragments) | ||||||||||||||||||||||||||
| if not fragments: | ||||||||||||||||||||||||||
| raise ValueError(f"Cannot spawn a physics material at '{prim_path}' from an empty fragment collection.") | ||||||||||||||||||||||||||
| invalid_types = [ | ||||||||||||||||||||||||||
| type(fragment).__name__ | ||||||||||||||||||||||||||
| for fragment in fragments | ||||||||||||||||||||||||||
| if not isinstance(fragment, physics_materials_cfg.RigidBodyMaterialFragment) | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
| if invalid_types: | ||||||||||||||||||||||||||
| raise TypeError( | ||||||||||||||||||||||||||
| "A physics-material fragment collection must contain only RigidBodyMaterialFragment instances; got" | ||||||||||||||||||||||||||
| f" {invalid_types}." | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| if stage is None: | ||||||||||||||||||||||||||
| stage = get_current_stage() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # create the material prim if none exists yet | ||||||||||||||||||||||||||
| if not stage.GetPrimAtPath(prim_path).IsValid(): | ||||||||||||||||||||||||||
| UsdShade.Material.Define(stage, prim_path) | ||||||||||||||||||||||||||
| prim = stage.GetPrimAtPath(prim_path) | ||||||||||||||||||||||||||
| if not prim.IsA(UsdShade.Material): | ||||||||||||||||||||||||||
| raise ValueError(f"A prim already exists at path: '{prim_path}' but is not a material.") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # apply the standard UsdPhysics MaterialAPI anchor (the defining schema for a physics material) | ||||||||||||||||||||||||||
| if not UsdPhysics.MaterialAPI(prim): | ||||||||||||||||||||||||||
| UsdPhysics.MaterialAPI.Apply(prim) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # dispatch each fragment's applier (writes its single namespace onto the material prim) | ||||||||||||||||||||||||||
| for cfg in fragments: | ||||||||||||||||||||||||||
| func = cfg.func if callable(cfg.func) else string_to_callable(cfg.func) | ||||||||||||||||||||||||||
| func(cfg, prim_path, stage) | ||||||||||||||||||||||||||
| return prim | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def spawn_physics_material( | ||||||||||||||||||||||||||
| prim_path: str, | ||||||||||||||||||||||||||
| material: physics_materials_cfg.PhysicsMaterialCfg | ||||||||||||||||||||||||||
| | physics_materials_cfg.RigidBodyMaterialFragment | ||||||||||||||||||||||||||
| | list[physics_materials_cfg.RigidBodyMaterialFragment] | ||||||||||||||||||||||||||
| | tuple[physics_materials_cfg.RigidBodyMaterialFragment, ...], | ||||||||||||||||||||||||||
| stage: Usd.Stage | None = None, | ||||||||||||||||||||||||||
| ) -> Usd.Prim: | ||||||||||||||||||||||||||
|
Comment on lines
+92
to
+99
Contributor
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.
Suggested change
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! |
||||||||||||||||||||||||||
| """Spawn a physics material from a spawner ``physics_material`` slot value. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Dispatches the two accepted slot forms: a single rigid-body fragment or list/tuple is spawned via | ||||||||||||||||||||||||||
| :func:`spawn_rigid_body_material_from_fragments`; otherwise the value is a legacy material cfg and is | ||||||||||||||||||||||||||
| spawned via its own :attr:`func`. Lets spawners accept both the fragment and the legacy interface | ||||||||||||||||||||||||||
| from one call site. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||
| prim_path: The prim path to spawn the material at. | ||||||||||||||||||||||||||
| material: A :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialFragment` (or list/tuple | ||||||||||||||||||||||||||
| of them), or a legacy material cfg carrying a :attr:`func`. | ||||||||||||||||||||||||||
| stage: The stage to spawn on. Defaults to None, in which case the current stage is used. A | ||||||||||||||||||||||||||
| legacy material cfg only supports the current stage (or None); passing a different | ||||||||||||||||||||||||||
| stage raises. | ||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||
| The spawned material prim. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Raises: | ||||||||||||||||||||||||||
| ValueError: When ``material`` is an empty fragment collection. | ||||||||||||||||||||||||||
| ValueError: When ``material`` is a legacy material cfg and ``stage`` is neither ``None`` | ||||||||||||||||||||||||||
| nor the current stage. | ||||||||||||||||||||||||||
| TypeError: When ``material`` is a fragment collection containing anything other than | ||||||||||||||||||||||||||
| :class:`~isaaclab.sim.spawners.materials.RigidBodyMaterialFragment` instances. | ||||||||||||||||||||||||||
| TypeError: When ``material`` is neither a physics-material cfg nor a rigid-body fragment. | ||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||
| # The family writer owns fragment validation so direct and dispatched calls have one contract. | ||||||||||||||||||||||||||
| if isinstance(material, (list, tuple, physics_materials_cfg.RigidBodyMaterialFragment)): | ||||||||||||||||||||||||||
| return spawn_rigid_body_material_from_fragments(prim_path, material, stage) | ||||||||||||||||||||||||||
| if not isinstance(material, physics_materials_cfg.PhysicsMaterialCfg): | ||||||||||||||||||||||||||
| raise TypeError( | ||||||||||||||||||||||||||
| "A physics material must be a PhysicsMaterialCfg or rigid-body material fragment; got" | ||||||||||||||||||||||||||
| f" '{type(material).__name__}'." | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| # legacy single-cfg path (rigid or deformable material cfg with its own spawner ``func``). | ||||||||||||||||||||||||||
| # Legacy material funcs take only ``(prim_path, cfg)`` and resolve the stage internally via | ||||||||||||||||||||||||||
| # ``get_current_stage()`` (their path matching is also current-stage-bound), so an explicit | ||||||||||||||||||||||||||
| # different stage cannot be honored on this path -- reject it loudly rather than authoring on | ||||||||||||||||||||||||||
| # the wrong stage. The fragment path above supports explicit stages. | ||||||||||||||||||||||||||
| if stage is not None and stage != get_current_stage(): | ||||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||||
| f"Legacy material cfg '{type(material).__name__}' can only be spawned on the current" | ||||||||||||||||||||||||||
| " stage. Pass the current stage (or None), or use the fragment-based API for" | ||||||||||||||||||||||||||
| " explicit-stage authoring." | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| return material.func(prim_path, material) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @clone | ||||||||||||||||||||||||||
| def spawn_rigid_body_material(prim_path: str, cfg: physics_materials_cfg.RigidBodyMaterialBaseCfg) -> Usd.Prim: | ||||||||||||||||||||||||||
| """Create material with rigid-body physics properties. | ||||||||||||||||||||||||||
|
|
@@ -26,10 +154,10 @@ def spawn_rigid_body_material(prim_path: str, cfg: physics_materials_cfg.RigidBo | |||||||||||||||||||||||||
| PxMaterial <https://nvidia-omniverse.github.io/PhysX/physx/5.4.1/_api_build/classPxBaseMaterial.html>`_. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| The writer is metadata-driven: it always applies the standard ``UsdPhysics.MaterialAPI`` and | ||||||||||||||||||||||||||
| writes the friction/restitution fields, then reads ``_usd_applied_schema``, ``_usd_namespace``, | ||||||||||||||||||||||||||
| and ``_usd_attr_name_map`` from the cfg to author solver-specific attributes. The applied | ||||||||||||||||||||||||||
| schema (e.g. ``PhysxMaterialAPI``) is added only when at least one solver-specific field has a | ||||||||||||||||||||||||||
| non-``None`` value at the instance level. | ||||||||||||||||||||||||||
| writes the friction/restitution/density fields, then reads ``_usd_applied_schema``, | ||||||||||||||||||||||||||
| ``_usd_namespace``, and ``_usd_attr_name_map`` from the cfg to author solver-specific attributes. | ||||||||||||||||||||||||||
| The applied schema (e.g. ``PhysxMaterialAPI``) is added only when at least one solver-specific | ||||||||||||||||||||||||||
| field has a non-``None`` value at the instance level. | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| .. note:: | ||||||||||||||||||||||||||
| This function is decorated with :func:`clone` that resolves prim path into list of paths | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
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.
@clonedecoratorspawn_rigid_body_materialandspawn_deformable_body_materialare both decorated with@clone, which resolves regex prim-path patterns (e.g./World/Robot_.*/body) to concrete paths before spawning.spawn_rigid_body_material_from_fragmentsskips the decorator, so callers who pass a regex path directly will silently get a single prim at the literal pattern string rather than one prim per match. The docstring does not note this limitation, which diverges from the documented behaviour of the rest of the spawner family.