google-deepmind / google-deepmind/mujoco
Allow moving bodies to another parent in mjSpec
- Dominant language
- C++
- Stars
- 15.2k
- Forks
- 1.8k
- Avg merge
- 10d 16h
- Merged PRs (30d)
- 25
Description
### The feature, motivation and pitch
I'm experimenting with modifying the kinematic tree during simulation. I understand that, for a single `mjModel`, the kinematic tree and the number of DOFs have to be fixed. So I tried out the recent `mjSpec` API, but found that it might not be flexible enough to achieve this seemingly simple functionality (discussed in https://github.com/google-deepmind/mujoco/issues/2327#issuecomment-2573230598).
Except for keyframe management, I could implement this feature when building from source as follows:
```c++
class mjCBody : public mjCBody_, private mjsBody {
// ...
void MoveTo(mjCBody* new_parent) {
mjCBody* old_parent = parent;
for (auto it = old_parent->bodies.begin(); it != old_parent->bodies.end(); ++it) {
if (*it == this) {
old_parent->bodies.erase(it);
break;
}
}
new_parent->bodies.push_back(this);
parent = new_parent;
}
// ...
}
void mjs_moveBody(mjsBody* parent, mjsBody* child) {
mjCBody* child_body = static_cast(child->element);
mjCBody* new_parent = static_cast(parent->element);
try {
child_body->MoveTo(new_parent);
} catch (mjCError& e) {
new_parent->model->SetError(e);
}
}
```
### Alternatives
_No response_
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.