Skip to content

Commit 2b2772f

Browse files
hapatel-bdaijtigue-bdaikellyguo11
authored andcommitted
Adds new Collision Mesh Schema properties (isaac-sim#2249)
# Description Adding new collision mesh property options allowing users to configure meshes to add different collision types ## Type of change - New feature (non-breaking change which adds functionality) ## Checklist - [x] I have run the [`pre-commit` checks](https://pre-commit.com/) with `./isaaclab.sh --format` - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have updated the changelog and the corresponding version in the extension's `config/extension.toml` file - [x] I have added my name to the `CONTRIBUTORS.md` or my name already exists there --------- Signed-off-by: Harsh Patel <[email protected]> Co-authored-by: James Tigue <[email protected]> Co-authored-by: James Tigue <[email protected]> Co-authored-by: Kelly Guo <[email protected]>
1 parent 85fbe94 commit 2b2772f

File tree

8 files changed

+436
-26
lines changed

8 files changed

+436
-26
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Guidelines for modifications:
6868
* Gary Lvov
6969
* Giulio Romualdi
7070
* Haoran Zhou
71+
* Harsh Patel
7172
* HoJin Jeon
7273
* Hongwei Xiong
7374
* Hongyu Li

source/isaaclab/isaaclab/sim/converters/mesh_converter.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,15 @@ def _convert_asset(self, cfg: MeshConverterCfg):
122122
if child_mesh_prim.GetTypeName() == "Mesh":
123123
# Apply collider properties to mesh
124124
if cfg.collision_props is not None:
125-
# -- Collision approximation to mesh
126-
# TODO: Move this to a new Schema: https://github.com/isaac-orbit/IsaacLab/issues/163
127-
mesh_collision_api = UsdPhysics.MeshCollisionAPI.Apply(child_mesh_prim)
128-
mesh_collision_api.GetApproximationAttr().Set(cfg.collision_approximation)
129125
# -- Collider properties such as offset, scale, etc.
130126
schemas.define_collision_properties(
131127
prim_path=child_mesh_prim.GetPath(), cfg=cfg.collision_props, stage=stage
132128
)
129+
# Add collision mesh
130+
if cfg.mesh_collision_props is not None:
131+
schemas.define_mesh_collision_properties(
132+
prim_path=child_mesh_prim.GetPath(), cfg=cfg.mesh_collision_props, stage=stage
133+
)
133134
# Delete the old Xform and make the new Xform the default prim
134135
stage.SetDefaultPrim(xform_prim)
135136
# Apply default Xform rotation to mesh -> enable to set rotation and scale

source/isaaclab/isaaclab/sim/converters/mesh_converter_cfg.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,30 @@
1212
class MeshConverterCfg(AssetConverterBaseCfg):
1313
"""The configuration class for MeshConverter."""
1414

15-
mass_props: schemas_cfg.MassPropertiesCfg | None = None
15+
mass_props: schemas_cfg.MassPropertiesCfg = None
1616
"""Mass properties to apply to the USD. Defaults to None.
1717
1818
Note:
1919
If None, then no mass properties will be added.
2020
"""
2121

22-
rigid_props: schemas_cfg.RigidBodyPropertiesCfg | None = None
22+
rigid_props: schemas_cfg.RigidBodyPropertiesCfg = None
2323
"""Rigid body properties to apply to the USD. Defaults to None.
2424
2525
Note:
2626
If None, then no rigid body properties will be added.
2727
"""
2828

29-
collision_props: schemas_cfg.CollisionPropertiesCfg | None = None
29+
collision_props: schemas_cfg.CollisionPropertiesCfg = None
3030
"""Collision properties to apply to the USD. Defaults to None.
3131
3232
Note:
3333
If None, then no collision properties will be added.
3434
"""
35-
36-
collision_approximation: str = "convexDecomposition"
37-
"""Collision approximation method to use. Defaults to "convexDecomposition".
38-
39-
Valid options are:
40-
"convexDecomposition", "convexHull", "boundingCube",
41-
"boundingSphere", "meshSimplification", or "none"
42-
43-
"none" causes no collision mesh to be added.
35+
mesh_collision_props: schemas_cfg.MeshCollisionPropertiesCfg = None
36+
"""Mesh approximation properties to apply to all collision meshes in the USD.
37+
Note:
38+
If None, then no mesh approximation properties will be added.
4439
"""
4540

4641
translation: tuple[float, float, float] = (0.0, 0.0, 0.0)

source/isaaclab/isaaclab/sim/schemas/__init__.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,93 @@
3333
"""
3434

3535
from .schemas import (
36+
PHYSX_MESH_COLLISION_CFGS,
37+
USD_MESH_COLLISION_CFGS,
3638
activate_contact_sensors,
3739
define_articulation_root_properties,
3840
define_collision_properties,
3941
define_deformable_body_properties,
4042
define_mass_properties,
43+
define_mesh_collision_properties,
4144
define_rigid_body_properties,
4245
modify_articulation_root_properties,
4346
modify_collision_properties,
4447
modify_deformable_body_properties,
4548
modify_fixed_tendon_properties,
4649
modify_joint_drive_properties,
4750
modify_mass_properties,
51+
modify_mesh_collision_properties,
4852
modify_rigid_body_properties,
4953
modify_spatial_tendon_properties,
5054
)
5155
from .schemas_cfg import (
5256
ArticulationRootPropertiesCfg,
57+
BoundingCubePropertiesCfg,
58+
BoundingSpherePropertiesCfg,
5359
CollisionPropertiesCfg,
60+
ConvexDecompositionPropertiesCfg,
61+
ConvexHullPropertiesCfg,
5462
DeformableBodyPropertiesCfg,
5563
FixedTendonPropertiesCfg,
5664
JointDrivePropertiesCfg,
5765
MassPropertiesCfg,
66+
MeshCollisionPropertiesCfg,
5867
RigidBodyPropertiesCfg,
68+
SDFMeshPropertiesCfg,
5969
SpatialTendonPropertiesCfg,
70+
TriangleMeshPropertiesCfg,
71+
TriangleMeshSimplificationPropertiesCfg,
6072
)
73+
74+
__all__ = [
75+
# articulation root
76+
"ArticulationRootPropertiesCfg",
77+
"define_articulation_root_properties",
78+
"modify_articulation_root_properties",
79+
# rigid bodies
80+
"RigidBodyPropertiesCfg",
81+
"define_rigid_body_properties",
82+
"modify_rigid_body_properties",
83+
"activate_contact_sensors",
84+
# colliders
85+
"CollisionPropertiesCfg",
86+
"define_collision_properties",
87+
"modify_collision_properties",
88+
# deformables
89+
"DeformableBodyPropertiesCfg",
90+
"define_deformable_body_properties",
91+
"modify_deformable_body_properties",
92+
# joints
93+
"JointDrivePropertiesCfg",
94+
"modify_joint_drive_properties",
95+
# mass
96+
"MassPropertiesCfg",
97+
"define_mass_properties",
98+
"modify_mass_properties",
99+
# mesh colliders
100+
"MeshCollisionPropertiesCfg",
101+
"define_mesh_collision_properties",
102+
"modify_mesh_collision_properties",
103+
# bounding cube
104+
"BoundingCubePropertiesCfg",
105+
# bounding sphere
106+
"BoundingSpherePropertiesCfg",
107+
# convex decomposition
108+
"ConvexDecompositionPropertiesCfg",
109+
# convex hull
110+
"ConvexHullPropertiesCfg",
111+
# sdf mesh
112+
"SDFMeshPropertiesCfg",
113+
# triangle mesh
114+
"TriangleMeshPropertiesCfg",
115+
# triangle mesh simplification
116+
"TriangleMeshSimplificationPropertiesCfg",
117+
# tendons
118+
"FixedTendonPropertiesCfg",
119+
"SpatialTendonPropertiesCfg",
120+
"modify_fixed_tendon_properties",
121+
"modify_spatial_tendon_properties",
122+
# Constants for configs that use PhysX vs USD API
123+
"PHYSX_MESH_COLLISION_CFGS",
124+
"USD_MESH_COLLISION_CFGS",
125+
]

source/isaaclab/isaaclab/sim/schemas/schemas.py

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@
2626
Articulation root properties.
2727
"""
2828

29+
PHYSX_MESH_COLLISION_CFGS = [
30+
schemas_cfg.ConvexDecompositionPropertiesCfg,
31+
schemas_cfg.ConvexHullPropertiesCfg,
32+
schemas_cfg.TriangleMeshPropertiesCfg,
33+
schemas_cfg.TriangleMeshSimplificationPropertiesCfg,
34+
schemas_cfg.SDFMeshPropertiesCfg,
35+
]
36+
37+
USD_MESH_COLLISION_CFGS = [
38+
schemas_cfg.BoundingCubePropertiesCfg,
39+
schemas_cfg.BoundingSpherePropertiesCfg,
40+
schemas_cfg.ConvexDecompositionPropertiesCfg,
41+
schemas_cfg.ConvexHullPropertiesCfg,
42+
schemas_cfg.TriangleMeshSimplificationPropertiesCfg,
43+
]
44+
2945

3046
def define_articulation_root_properties(
3147
prim_path: str, cfg: schemas_cfg.ArticulationRootPropertiesCfg, stage: Usd.Stage | None = None
@@ -934,3 +950,121 @@ def modify_deformable_body_properties(
934950

935951
# success
936952
return True
953+
954+
955+
"""
956+
Collision mesh properties.
957+
"""
958+
959+
960+
def extract_mesh_collision_api_and_attrs(cfg):
961+
# We use the number of user set attributes outside of the API function
962+
# to determine which API to use in ambiguous cases, so collect them here
963+
custom_attrs = {
964+
key: value
965+
for key, value in cfg.to_dict().items()
966+
if value is not None and key not in ["usd_func", "physx_func"]
967+
}
968+
969+
use_usd_api = False
970+
use_phsyx_api = False
971+
972+
# We have some custom attributes and allow them
973+
if len(custom_attrs) > 0 and type(cfg) in PHYSX_MESH_COLLISION_CFGS:
974+
use_phsyx_api = True
975+
# We have no custom attributes
976+
elif len(custom_attrs) == 0:
977+
if type(cfg) in USD_MESH_COLLISION_CFGS:
978+
# Use the USD API
979+
use_usd_api = True
980+
else:
981+
# Use the PhysX API
982+
use_phsyx_api = True
983+
984+
elif len(custom_attrs > 0) and type(cfg) in USD_MESH_COLLISION_CFGS:
985+
raise ValueError("Args are specified but the USD Mesh API doesn't support them!")
986+
987+
mesh_collision_appx_type = type(cfg).__name__.partition("PropertiesCfg")[0]
988+
989+
if use_usd_api:
990+
# Add approximation to the attributes as this is how USD collision mesh API is configured
991+
api_func = cfg.usd_func
992+
# Approximation needs to be formatted with camelCase
993+
custom_attrs["Approximation"] = mesh_collision_appx_type[0].lower() + mesh_collision_appx_type[1:]
994+
elif use_phsyx_api:
995+
api_func = cfg.physx_func
996+
else:
997+
raise ValueError("Either USD or PhysX API should be used for mesh collision approximation!")
998+
999+
return api_func, custom_attrs
1000+
1001+
1002+
def define_mesh_collision_properties(
1003+
prim_path: str, cfg: schemas_cfg.MeshCollisionPropertiesCfg, stage: Usd.Stage | None = None
1004+
):
1005+
"""Apply the mesh collision schema on the input prim and set its properties.
1006+
See :func:`modify_collision_mesh_properties` for more details on how the properties are set.
1007+
Args:
1008+
prim_path : The prim path where to apply the mesh collision schema.
1009+
cfg : The configuration for the mesh collision properties.
1010+
stage : The stage where to find the prim. Defaults to None, in which case the
1011+
current stage is used.
1012+
Raises:
1013+
ValueError: When the prim path is not valid.
1014+
"""
1015+
# obtain stage
1016+
if stage is None:
1017+
stage = get_current_stage()
1018+
# get USD prim
1019+
prim = stage.GetPrimAtPath(prim_path)
1020+
# check if prim path is valid
1021+
if not prim.IsValid():
1022+
raise ValueError(f"Prim path '{prim_path}' is not valid.")
1023+
1024+
api_func, _ = extract_mesh_collision_api_and_attrs(cfg=cfg)
1025+
1026+
# Only enable if not already enabled
1027+
if not api_func(prim):
1028+
api_func.Apply(prim)
1029+
1030+
modify_mesh_collision_properties(prim_path=prim_path, cfg=cfg, stage=stage)
1031+
1032+
1033+
@apply_nested
1034+
def modify_mesh_collision_properties(
1035+
prim_path: str, cfg: schemas_cfg.MeshCollisionPropertiesCfg, stage: Usd.Stage | None = None
1036+
):
1037+
"""Set properties for the mesh collision of a prim.
1038+
These properties are based on either the `Phsyx the `UsdPhysics.MeshCollisionAPI` schema.
1039+
.. note::
1040+
This function is decorated with :func:`apply_nested` that sets the properties to all the prims
1041+
(that have the schema applied on them) under the input prim path.
1042+
.. UsdPhysics.MeshCollisionAPI: https://openusd.org/release/api/class_usd_physics_mesh_collision_a_p_i.html
1043+
Args:
1044+
prim_path : The prim path of the rigid body. This prim should be a Mesh prim.
1045+
cfg : The configuration for the mesh collision properties.
1046+
stage : The stage where to find the prim. Defaults to None, in which case the
1047+
current stage is used.
1048+
"""
1049+
# obtain stage
1050+
if stage is None:
1051+
stage = get_current_stage()
1052+
# get USD prim
1053+
prim = stage.GetPrimAtPath(prim_path)
1054+
1055+
api_func, custom_attrs = extract_mesh_collision_api_and_attrs(cfg=cfg)
1056+
1057+
# retrieve the mesh collision API
1058+
mesh_collision_api = api_func(prim)
1059+
1060+
# set custom attributes into mesh collision API
1061+
for attr_name, value in custom_attrs.items():
1062+
# Only "Attribute" attr should be in format "boundingSphere", so set camel_case to be False
1063+
if attr_name == "Attribute":
1064+
camel_case = False
1065+
else:
1066+
camel_case = True
1067+
safe_set_attribute_on_usd_schema(mesh_collision_api, attr_name, value, camel_case=camel_case)
1068+
1069+
# success
1070+
return True

0 commit comments

Comments
 (0)