Clean up MJCF attach code.

PiperOrigin-RevId: 941603845
Change-Id: I94f275e2073b1eb81cfabdc0bed80021c6f15b1c
This commit is contained in:
Yuval Tassa
2026-07-02 03:44:54 -07:00
committed by Copybara-Service
parent f8462a156d
commit 0e58c48504
2 changed files with 22 additions and 23 deletions
+21 -22
View File
@@ -4181,32 +4181,31 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame,
}
mjtObj type = has_body ? mjOBJ_BODY : mjOBJ_FRAME;
mjsElement* child = mjs_findElement(spec, type, (prefix+child_name).c_str());
mjsFrame* pframe = frame ? frame : mjs_addFrame(body, nullptr);
string full_name = prefix+child_name;
if (mjs_findElement(spec, type, full_name.c_str())) {
throw mjXError(elem, "cannot attach: element %s already exists", full_name.c_str());
}
if (!child) {
mjSpec* asset = mjs_findSpec(spec, model_name.c_str());
if (!asset) {
throw mjXError(elem, "could not find model '%s'", model_name.c_str());
}
if (child_name.empty()) {
child = asset->element;
} else {
child = mjs_findElement(asset, type, child_name.c_str());
if (!child) {
throw mjXError(elem, "could not find %s",
(string(mju_type2Str(type)) + " '" + child_name + "'").c_str());
}
}
if (!mjs_attach(pframe->element, child, prefix.c_str(), "")) {
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
}
mjSpec* asset = mjs_findSpec(spec, model_name.c_str());
if (!asset) {
throw mjXError(elem, "could not find model '%s'", model_name.c_str());
}
mjsElement* child;
if (child_name.empty()) {
child = asset->element;
} else {
// only set frame to existing body
if (mjs_setFrame(child, pframe)) {
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
child = mjs_findElement(asset, type, child_name.c_str());
if (!child) {
throw mjXError(elem, "could not find %s",
(string(mju_type2Str(type)) + " '" + child_name + "'").c_str());
}
}
mjsFrame* pframe = frame ? frame : mjs_addFrame(body, nullptr);
if (!mjs_attach(pframe->element, child, prefix.c_str(), "")) {
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
}
}
// no match
+1 -1
View File
@@ -1632,7 +1632,7 @@ TEST_F(XMLReaderTest, RepeatedPrefix) {
mjSpec* spec = mj_parseXMLString(parent, vfs.get(), err.data(), err.size());
EXPECT_THAT(spec, IsNull());
EXPECT_THAT(err.data(), HasSubstr("mismatched parents"));
EXPECT_THAT(err.data(), HasSubstr("cannot attach"));
mj_deleteSpec(spec);
mj_deleteVFS(vfs.get());
}