Skip to content

Commit 0e58c48

Browse files
yuvaltassacopybara-github
authored andcommitted
Clean up MJCF attach code.
PiperOrigin-RevId: 941603845 Change-Id: I94f275e2073b1eb81cfabdc0bed80021c6f15b1c
1 parent f8462a1 commit 0e58c48

2 files changed

Lines changed: 22 additions & 23 deletions

File tree

src/xml/xml_native_reader.cc

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4181,32 +4181,31 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame,
41814181
}
41824182
mjtObj type = has_body ? mjOBJ_BODY : mjOBJ_FRAME;
41834183

4184-
mjsElement* child = mjs_findElement(spec, type, (prefix+child_name).c_str());
4185-
mjsFrame* pframe = frame ? frame : mjs_addFrame(body, nullptr);
4184+
string full_name = prefix+child_name;
4185+
if (mjs_findElement(spec, type, full_name.c_str())) {
4186+
throw mjXError(elem, "cannot attach: element %s already exists", full_name.c_str());
4187+
}
41864188

4187-
if (!child) {
4188-
mjSpec* asset = mjs_findSpec(spec, model_name.c_str());
4189-
if (!asset) {
4190-
throw mjXError(elem, "could not find model '%s'", model_name.c_str());
4191-
}
4192-
if (child_name.empty()) {
4193-
child = asset->element;
4194-
} else {
4195-
child = mjs_findElement(asset, type, child_name.c_str());
4196-
if (!child) {
4197-
throw mjXError(elem, "could not find %s",
4198-
(string(mju_type2Str(type)) + " '" + child_name + "'").c_str());
4199-
}
4200-
}
4201-
if (!mjs_attach(pframe->element, child, prefix.c_str(), "")) {
4202-
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
4203-
}
4189+
mjSpec* asset = mjs_findSpec(spec, model_name.c_str());
4190+
if (!asset) {
4191+
throw mjXError(elem, "could not find model '%s'", model_name.c_str());
4192+
}
4193+
4194+
mjsElement* child;
4195+
if (child_name.empty()) {
4196+
child = asset->element;
42044197
} else {
4205-
// only set frame to existing body
4206-
if (mjs_setFrame(child, pframe)) {
4207-
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
4198+
child = mjs_findElement(asset, type, child_name.c_str());
4199+
if (!child) {
4200+
throw mjXError(elem, "could not find %s",
4201+
(string(mju_type2Str(type)) + " '" + child_name + "'").c_str());
42084202
}
42094203
}
4204+
4205+
mjsFrame* pframe = frame ? frame : mjs_addFrame(body, nullptr);
4206+
if (!mjs_attach(pframe->element, child, prefix.c_str(), "")) {
4207+
throw mjXError(elem, "%s", stripError(mjs_getError(spec)));
4208+
}
42104209
}
42114210

42124211
// no match

test/xml/xml_native_reader_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,7 +1632,7 @@ TEST_F(XMLReaderTest, RepeatedPrefix) {
16321632

16331633
mjSpec* spec = mj_parseXMLString(parent, vfs.get(), err.data(), err.size());
16341634
EXPECT_THAT(spec, IsNull());
1635-
EXPECT_THAT(err.data(), HasSubstr("mismatched parents"));
1635+
EXPECT_THAT(err.data(), HasSubstr("cannot attach"));
16361636
mj_deleteSpec(spec);
16371637
mj_deleteVFS(vfs.get());
16381638
}

0 commit comments

Comments
 (0)