Prevent deletion from an attached mjSpec.

This change disallows calling `mjs_delete` on an mjSpec that has been attached to another mjSpec via `mjs_attach`. Attempting to delete an element from an attached spec will now result in an error. The Python bindings for `mjSpec.delete` have been updated to raise a ValueError when this occurs.

PiperOrigin-RevId: 897266695
Change-Id: Ic0670125a3028191ec50eca890f02b5910ec8b03
This commit is contained in:
Alessio Quaglino
2026-04-09 13:12:40 -07:00
committed by Copybara-Service
parent a744b366fb
commit 6b724616c0
5 changed files with 107 additions and 20 deletions
+5 -1
View File
@@ -474,8 +474,12 @@ int mj_copyBack(mjSpec* s, const mjModel* m) {
// remove body from mjSpec, return 0 on success
int mjs_delete(mjSpec* s, mjsElement* element) {
mjCModel* model = static_cast<mjCModel*>(s->element);
if (model->IsAttached()) {
model->SetError(mjCError(nullptr, "Cannot delete element from an attached mjSpec."));
return -1;
}
if (!element) {
model->SetError(mjCError(0, "Element is null."));
model->SetError(mjCError(nullptr, "Element is null."));
return -1;
}
try {
+3
View File
@@ -327,6 +327,9 @@ class mjCModel : public mjCModel_, private mjSpec {
// set attached flag
void SetAttached(bool deepcopy) { attached_ |= !deepcopy; }
// check if model is attached
bool IsAttached() const { return attached_; }
// check for repeated names in list
void CheckRepeat(mjtObj type);