Make sure we never apply the ArticulationRoot API more than once.

PiperOrigin-RevId: 779185190
Change-Id: If441d0e2ba24191883839977b46864db2b4b8204
This commit is contained in:
Robin Alazard
2025-07-04 07:34:39 -07:00
committed by Copybara-Service
parent 85ad1eec91
commit 21a852504c
2 changed files with 50 additions and 1 deletions
@@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include <string>
#include <vector>
@@ -41,6 +42,7 @@
#include <pxr/usd/sdf/assetPath.h>
#include <pxr/usd/sdf/declareHandles.h>
#include <pxr/usd/sdf/fileFormat.h>
#include <pxr/usd/sdf/listOp.h>
#include <pxr/usd/sdf/path.h>
#include <pxr/usd/sdf/schema.h>
#include <pxr/usd/usd/common.h>
@@ -49,6 +51,7 @@
#include <pxr/usd/usd/primRange.h> // IWYU pragma: keep, used for TraverseAll
#include <pxr/usd/usd/stage.h>
#include <pxr/usd/usd/timeCode.h>
#include <pxr/usd/usd/tokens.h>
#include <pxr/usd/usdGeom/capsule.h>
#include <pxr/usd/usdGeom/cube.h>
#include <pxr/usd/usdGeom/cylinder.h>
@@ -1131,6 +1134,45 @@ TEST_F(MjcfSdfFileFormatPluginTest, TestPhysicsToggleSdfFormatArg) {
pxr::UsdPhysicsRigidBodyAPI);
}
TEST_F(MjcfSdfFileFormatPluginTest, TestArticulationRootAppliedOnce) {
static constexpr char kXml[] = R"(
<mujoco model="physics_test">
<worldbody>
<body name="parent" pos="0 0 0">
<geom name="parent_geom" type="sphere" size="1"/>
<body name="child_1" pos="1 0 0">
<geom name="child_1_geom" type="sphere" size="1"/>
</body>
<body name="child_2" pos="2 0 0">
<geom name="child_2_geom" type="sphere" size="1"/>
</body>
</body>
</worldbody>
</mujoco>
)";
pxr::SdfFileFormat::FileFormatArguments args;
args["usdMjcfToggleUsdPhysics"] = "true";
pxr::SdfLayerRefPtr layer = LoadLayer(kXml, args);
// This test is particular in the sense that the authoring mistake, which is
// made on the SdfLayer level, would disappear when we access the COMPOSED
// stage because duplicates are removed. So we need to check the SdfLayer
// directly to see the problem.
auto primSpec = layer->GetPrimAtPath(pxr::SdfPath("/physics_test/parent"));
EXPECT_TRUE(primSpec);
pxr::VtValue apiSchemasValue = primSpec->GetInfo(pxr::UsdTokens->apiSchemas);
const pxr::SdfTokenListOp& listOp =
apiSchemasValue.UncheckedGet<pxr::SdfTokenListOp>();
const pxr::SdfTokenListOp::ItemVector& prependedItems =
listOp.GetPrependedItems();
int count = std::count(prependedItems.begin(), prependedItems.end(),
pxr::UsdPhysicsTokens->PhysicsArticulationRootAPI);
EXPECT_EQ(count, 1);
}
TEST_F(MjcfSdfFileFormatPluginTest, TestPhysicsRigidBody) {
static constexpr char kXml[] = R"(
<mujoco model="physics_test">