|
26 | 26 | Articulation root properties. |
27 | 27 | """ |
28 | 28 |
|
| 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 | + |
29 | 45 |
|
30 | 46 | def define_articulation_root_properties( |
31 | 47 | prim_path: str, cfg: schemas_cfg.ArticulationRootPropertiesCfg, stage: Usd.Stage | None = None |
@@ -934,3 +950,121 @@ def modify_deformable_body_properties( |
934 | 950 |
|
935 | 951 | # success |
936 | 952 | 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