|
22 | 22 | from .utils import ( |
23 | 23 | float3_to_quatf, |
24 | 24 | get_geometry_name, |
| 25 | + set_schema_attribute, |
25 | 26 | set_transform, |
26 | 27 | ) |
27 | 28 |
|
@@ -204,6 +205,11 @@ def physics_joints(parent: Usd.Prim, link: ElementLink, data: ConversionData): |
204 | 205 | joint_names = [joint.name for joint in joints] if joints else [] |
205 | 206 | joint_safe_names = data.name_cache.getPrimNames(parent, joint_names) |
206 | 207 |
|
| 208 | + # Usd.Prim dict for joint name. |
| 209 | + # This is used to reference mimic joints. |
| 210 | + physics_joints_dict: dict[str, Usd.Prim] = {} |
| 211 | + mimic_joints_names: list[str] = [] |
| 212 | + |
207 | 213 | # Create physics joints. |
208 | 214 | for joint, joint_safe_name in zip(joints, joint_safe_names): |
209 | 215 | body0_link_name = joint.parent.get_with_default("link") |
@@ -243,12 +249,30 @@ def physics_joints(parent: Usd.Prim, link: ElementLink, data: ConversionData): |
243 | 249 |
|
244 | 250 | # Unsupported attributes and elements within the joint are stored as custom attributes of the PhysicsJoint. |
245 | 251 | if physics_joint: |
| 252 | + # Stores information used by the mimic joint. |
| 253 | + physics_joints_dict[joint.name] = physics_joint.GetPrim() |
| 254 | + if joint.mimic and joint.mimic.joint: |
| 255 | + mimic_joints_names.append(joint.name) |
| 256 | + |
246 | 257 | convert_unsupported_attributes_and_elements(joint, physics_joint.GetPrim(), data) |
247 | 258 |
|
248 | 259 | # Store custom attributes and custom elements for the specified element. |
249 | 260 | if joint.undefined_attributes or joint.undefined_elements or joint.undefined_text: |
250 | 261 | convert_undefined_elements(joint, physics_joint.GetPrim(), data) |
251 | 262 |
|
| 263 | + # Set mimic joints. |
| 264 | + for mimic_joint_name in mimic_joints_names: |
| 265 | + # USD prim associated with mimic_joint_name. |
| 266 | + physics_joint_prim = physics_joints_dict[mimic_joint_name] |
| 267 | + |
| 268 | + # Search for the mimic_joint_name among the joints. |
| 269 | + joint = next((joint for joint in joints if joint.name == mimic_joint_name), None) |
| 270 | + if joint: |
| 271 | + # USD prim associated with the joint name referenced by mimic. |
| 272 | + ref_joint_prim = physics_joints_dict[joint.mimic.joint] |
| 273 | + |
| 274 | + set_physics_mimic_joint(joint, physics_joint_prim, ref_joint_prim) |
| 275 | + |
252 | 276 |
|
253 | 277 | def convert_unsupported_attributes_and_elements(element_joint: ElementJoint, prim: Usd.Prim, data: ConversionData): |
254 | 278 | """ |
@@ -300,13 +324,26 @@ def convert_unsupported_attributes_and_elements(element_joint: ElementJoint, pri |
300 | 324 | if soft_upper_limit is not None: |
301 | 325 | prim.CreateAttribute("urdf:safety_controller:soft_upper_limit", Sdf.ValueTypeNames.Float, custom=True).Set(soft_upper_limit) |
302 | 326 |
|
303 | | - if element_joint.mimic: |
304 | | - joint = element_joint.mimic.get_with_default("joint") |
305 | | - if joint is not None: |
306 | | - prim.CreateAttribute("urdf:mimic:joint", Sdf.ValueTypeNames.String, custom=True).Set(joint) |
307 | | - multiplier = element_joint.mimic.get_with_default("multiplier") |
308 | | - if multiplier is not None: |
309 | | - prim.CreateAttribute("urdf:mimic:multiplier", Sdf.ValueTypeNames.Float, custom=True).Set(multiplier) |
310 | | - offset = element_joint.mimic.get_with_default("offset") |
311 | | - if offset is not None: |
312 | | - prim.CreateAttribute("urdf:mimic:offset", Sdf.ValueTypeNames.Float, custom=True).Set(offset) |
| 327 | + |
| 328 | +def set_physics_mimic_joint(element_joint: ElementJoint, prim: Usd.Prim, ref_prim: Usd.Prim): |
| 329 | + """ |
| 330 | + Set the mimic joint. |
| 331 | +
|
| 332 | + Args: |
| 333 | + element_joint: ElementJoint in URDF |
| 334 | + prim: PhysicsJoint in USD |
| 335 | + ref_prim: PhysicsJoint in USD referenced by mimic |
| 336 | + """ |
| 337 | + # Both multiplier and offset have default values. |
| 338 | + multiplier = element_joint.mimic.get_with_default("multiplier") |
| 339 | + offset = element_joint.mimic.get_with_default("offset") |
| 340 | + |
| 341 | + # URDF: joint0 = multiplier * joint1 + offset |
| 342 | + # Newton USD Schemas: joint0 = coef1 * joint1 + coef0 |
| 343 | + prim.ApplyAPI("NewtonMimicAPI") |
| 344 | + set_schema_attribute(prim, "newton:mimicEnabled", True) |
| 345 | + set_schema_attribute(prim, "newton:mimicCoef0", offset) |
| 346 | + set_schema_attribute(prim, "newton:mimicCoef1", multiplier) |
| 347 | + |
| 348 | + rel = prim.CreateRelationship("newton:mimicJoint") |
| 349 | + rel.AddTarget(ref_prim.GetPath()) |
0 commit comments