Apply articulation API to the correct body when converting to USD.

PiperOrigin-RevId: 776206773
Change-Id: I84b5687a9900b42fd208124414ac98feaaa9f8c4
This commit is contained in:
Sam Haves
2025-06-26 11:06:07 -07:00
committed by Copybara-Service
parent 1e319aebed
commit 24789f8fe5
2 changed files with 29 additions and 14 deletions
@@ -1821,6 +1821,17 @@ class ModelWriter {
: pxr::KindTokens->subcomponent;
SetPrimKind(data_, body_path, kind);
// If the parent is not the world body, but is child of the world body
// then we need to apply the articulation root API.
if (parent_id != kWorldIndex) {
int parent_parent_id =
mjs_getId(mjs_getParent(parent->element)->element);
if (parent_parent_id == kWorldIndex) {
ApplyApiSchema(data_, parent_path,
pxr::UsdPhysicsTokens->PhysicsArticulationRootAPI);
}
}
// Apply the PhysicsRigidBodyAPI schema if we are writing physics.
if (write_physics_) {
// If the body had a mass specified then it must have either inertia or
@@ -1926,13 +1937,6 @@ class ModelWriter {
pxr::UsdGeomTokens->Xform);
SetPrimKind(data_, world_group_path, pxr::KindTokens->group);
if (write_physics_) {
// Apply the PhysicsArticulationRootAPI to the world body so that
// everything under it is automatically considered an articulation.
ApplyApiSchema(data_, world_group_path,
pxr::UsdPhysicsTokens->PhysicsArticulationRootAPI);
}
return world_group_path;
}
};
@@ -1139,6 +1139,9 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestPhysicsRigidBody) {
<geom name="test_geom_2" type="sphere" size="1"/>
</body>
</body>
<body name="test_body_3" pos="0 0 0">
<geom name="test_geom_3" type="sphere" size="1"/>
</body>
</worldbody>
</mujoco>
)";
@@ -1153,18 +1156,26 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestPhysicsRigidBody) {
EXPECT_PRIM_VALID(stage, "/physics_test/test_body");
EXPECT_PRIM_VALID(stage, "/physics_test/test_body/test_body_2");
// Articulation root is applied to the root of the physics scene (worldbody).
EXPECT_PRIM_API_APPLIED(stage, "/physics_test",
pxr::UsdPhysicsArticulationRootAPI);
EXPECT_PRIM_API_APPLIED(stage, "/physics_test/test_body",
pxr::UsdPhysicsRigidBodyAPI);
EXPECT_PRIM_API_NOT_APPLIED(stage, "/physics_test/test_body",
pxr::UsdPhysicsArticulationRootAPI);
EXPECT_PRIM_API_APPLIED(stage, "/physics_test/test_body/test_body_2",
pxr::UsdPhysicsRigidBodyAPI);
EXPECT_PRIM_API_APPLIED(stage, "/physics_test/test_body_3",
pxr::UsdPhysicsRigidBodyAPI);
// Articulation root is applied to the children of the world body.
EXPECT_PRIM_API_APPLIED(stage, "/physics_test/test_body",
pxr::UsdPhysicsArticulationRootAPI);
// test_body_3 is a child of the world but has no children so should not be
// an articulation root.
EXPECT_PRIM_API_NOT_APPLIED(stage, "/physics_test/test_body_3",
pxr::UsdPhysicsArticulationRootAPI);
// Articulation root is not applied to other bodies or world body.
EXPECT_PRIM_API_NOT_APPLIED(stage, "/physics_test",
pxr::UsdPhysicsArticulationRootAPI);
EXPECT_PRIM_API_NOT_APPLIED(stage, "/physics_test/test_body/test_body_2",
pxr::UsdPhysicsArticulationRootAPI);
pxr::UsdPhysicsArticulationRootAPI);
// Geoms should not have RigidBodyAPI applied either.
EXPECT_PRIM_API_NOT_APPLIED(stage, "/physics_test/test_body/test_geom",