Allow to attach an entire mjSpec in mjs_attach.
PiperOrigin-RevId: 742737713 Change-Id: Ie172568b8c35232e1f13d75aebee20346e76e95b
This commit is contained in:
committed by
Copybara-Service
parent
e5912c3ce4
commit
b839fe79e1
+44
-1
@@ -119,6 +119,18 @@ mjModel* mj_compile(mjSpec* s, const mjVFS* vfs) {
|
||||
}
|
||||
|
||||
|
||||
// set frame for all elements of a body
|
||||
static void SetFrame(mjsBody* body, mjtObj objtype, mjsFrame* frame) {
|
||||
mjsElement* el = mjs_firstChild(body, objtype, 0);
|
||||
while (el) {
|
||||
if (frame->element != el && mjs_getFrame(el) == nullptr) {
|
||||
mjs_setFrame(el, frame);
|
||||
}
|
||||
el = mjs_nextChild(body, el, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// attach body to a frame of the parent
|
||||
static mjsElement* attachBody(mjCFrame* parent, const mjCBody* child,
|
||||
@@ -207,13 +219,44 @@ mjsElement* mjs_attach(mjsElement* parent, const mjsElement* child,
|
||||
return nullptr;
|
||||
}
|
||||
mjCModel* model = static_cast<mjCModel*>(mjs_getSpec(parent)->element);
|
||||
if (child->elemtype == mjOBJ_MODEL) {
|
||||
mjCModel* child_model = static_cast<mjCModel*>((mjsElement*)child);
|
||||
mjsBody* worldbody = mjs_findBody(&child_model->spec, "world");
|
||||
if (!worldbody) {
|
||||
model->SetError(mjCError(0, "Child does not have a world body."));
|
||||
return nullptr;
|
||||
}
|
||||
mjsFrame* worldframe = mjs_addFrame(worldbody, nullptr);
|
||||
SetFrame(worldbody, mjOBJ_BODY, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_SITE, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_FRAME, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_JOINT, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_GEOM, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_LIGHT, worldframe);
|
||||
SetFrame(worldbody, mjOBJ_CAMERA, worldframe);
|
||||
child = worldframe->element;
|
||||
}
|
||||
switch (parent->elemtype) {
|
||||
case mjOBJ_FRAME:
|
||||
if (child->elemtype == mjOBJ_BODY) {
|
||||
return attachBody(static_cast<mjCFrame*>(parent),
|
||||
static_cast<const mjCBody*>(child), prefix, suffix);
|
||||
} else if (child->elemtype == mjOBJ_FRAME) {
|
||||
mjsBody* parent_body = mjs_getParent(parent);
|
||||
if (!parent_body) {
|
||||
model->SetError(mjCError(0, "Frame does not have a parent body."));
|
||||
return nullptr;
|
||||
}
|
||||
mjCFrame* frame = static_cast<mjCFrame*>(parent);
|
||||
mjsElement* attached_frame =
|
||||
attachFrame(static_cast<mjCBody*>(parent_body->element),
|
||||
static_cast<const mjCFrame*>(child), prefix, suffix);
|
||||
if (mjs_setFrame(attached_frame, &frame->spec)) {
|
||||
return nullptr;
|
||||
}
|
||||
return attached_frame;
|
||||
} else {
|
||||
model->SetError(mjCError(0, "child element is not a body"));
|
||||
model->SetError(mjCError(0, "child element is not a body or frame"));
|
||||
return nullptr;
|
||||
}
|
||||
case mjOBJ_BODY:
|
||||
|
||||
@@ -3627,27 +3627,33 @@ void mjXReader::Body(XMLElement* section, mjsBody* body, mjsFrame* frame,
|
||||
else if (name == "attach") {
|
||||
string model_name, body_name, prefix;
|
||||
ReadAttrTxt(elem, "model", model_name, /*required=*/true);
|
||||
ReadAttrTxt(elem, "body", body_name, /*required=*/true);
|
||||
ReadAttrTxt(elem, "body", body_name, /*required=*/false);
|
||||
ReadAttrTxt(elem, "prefix", prefix, /*required=*/true);
|
||||
|
||||
mjsBody* child = mjs_findBody(spec, (prefix+body_name).c_str());
|
||||
mjsBody* child_body = mjs_findBody(spec, (prefix+body_name).c_str());
|
||||
mjsFrame* pframe = frame ? frame : mjs_addFrame(body, nullptr);
|
||||
|
||||
if (!child) {
|
||||
if (!child_body) {
|
||||
mjSpec* asset = mjs_findSpec(spec, model_name.c_str());
|
||||
if (!asset) {
|
||||
throw mjXError(elem, "could not find model '%s'", model_name.c_str());
|
||||
}
|
||||
child = mjs_findBody(asset, body_name.c_str());
|
||||
if (!child) {
|
||||
throw mjXError(elem, "could not find body '%s''%s'", body_name.c_str());
|
||||
mjsElement* child;
|
||||
if (body_name.empty()) {
|
||||
child = asset->element;
|
||||
} else {
|
||||
child_body = mjs_findBody(asset, body_name.c_str());
|
||||
if (!child_body) {
|
||||
throw mjXError(elem, "could not find body '%s''%s'", body_name.c_str());
|
||||
}
|
||||
child = child_body->element;
|
||||
}
|
||||
if (!mjs_attach(pframe->element, child->element, prefix.c_str(), "")) {
|
||||
if (!mjs_attach(pframe->element, child, prefix.c_str(), "")) {
|
||||
throw mjXError(elem, mjs_getError(spec));
|
||||
}
|
||||
} else {
|
||||
// only set frame to existing body
|
||||
if (mjs_setFrame(child->element, pframe)) {
|
||||
if (mjs_setFrame(child_body->element, pframe)) {
|
||||
throw mjXError(elem, mjs_getError(spec));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user