@@ -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
0 commit comments