From 1e319aebed3ffe868b78b5c1d822c5f5c39a3ff9 Mon Sep 17 00:00:00 2001 From: Sam Haves Date: Thu, 26 Jun 2025 10:54:39 -0700 Subject: [PATCH] Support joint actuator authoring in Mjcf SdfFileFormat plugin. PiperOrigin-RevId: 776202325 Change-Id: I8503dd98c1b64502550b879e70039ae568517508 --- .../usd/plugins/mjcf/mujoco_to_usd.cc | 9 +++++++ .../usd/plugins/mjcf/mjcf_file_format_test.cc | 25 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc b/src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc index c17df766..a3a21d17 100644 --- a/src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc +++ b/src/experimental/usd/plugins/mjcf/mujoco_to_usd.cc @@ -171,6 +171,7 @@ class ModelWriter { : spec_(spec), model_(model), data_(data), class_path_("/Bad_Path") { body_paths_ = std::vector(model->nbody); site_paths_ = std::vector(model->nsite); + joint_paths_ = std::vector(model->njnt); } ~ModelWriter() { mj_deleteModel(model_); } @@ -221,6 +222,8 @@ class ModelWriter { std::vector body_paths_; // Mapping from Mujoco site id to SdfPath. std::vector site_paths_; + // Mapping from Mujoco joint id to SdfPath. + std::vector joint_paths_; // Mapping from mesh names to Mesh prim path. std::unordered_map mesh_paths_; // Whether to write physics data. @@ -935,6 +938,9 @@ class ModelWriter { actuator->trntype == mjtTrn::mjTRN_SLIDERCRANK) { int site_id = mj_name2id(model_, mjOBJ_SITE, actuator->target->c_str()); transmission_path = site_paths_[site_id]; + } else if (actuator->trntype == mjtTrn::mjTRN_JOINT) { + int joint_id = mj_name2id(model_, mjOBJ_JOINT, actuator->target->c_str()); + transmission_path = joint_paths_[joint_id]; } else { TF_WARN(UnsupportedActuatorTypeError, "Unsupported actuator type for actuator %d", @@ -1711,6 +1717,9 @@ class ModelWriter { } } } + if (joint_id >= 0) { + joint_paths_[joint_id] = joint_path; + } } void WriteCamera(mjsCamera *spec_cam, const mjsBody *body) { diff --git a/test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc b/test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc index ddb7a60c..a0698ef4 100644 --- a/test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc +++ b/test/experimental/usd/plugins/mjcf/mjcf_file_format_test.cc @@ -1503,6 +1503,31 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestMjcPhysicsActuatorGeneral) { pxr::VtDoubleArray{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}); } +TEST_F(MjcfSdfFileFormatPluginTest, TestMjcPhysicsJointActuator) { + static constexpr char xml[] = R"( + + + + + + + + + + + + + + )"; + auto stage = OpenStageWithPhysics(xml); + + EXPECT_PRIM_API_APPLIED(stage, "/test/axle/rod/rod_hinge", + pxr::MjcPhysicsActuatorAPI); +} + TEST_F(MjcfSdfFileFormatPluginTest, TestMjcPhysicsBodyActuator) { static constexpr char xml[] = R"(