Namespace plugin instances when attaching.

Fixes #2112.

PiperOrigin-RevId: 681045462
Change-Id: I2c945c5405fcb91e0f9a1b0b1e94f68d174c84ee
This commit is contained in:
Alessio Quaglino
2024-10-01 09:13:43 -07:00
committed by Copybara-Service
parent 89aea5e217
commit dfe8e45101
5 changed files with 36 additions and 3 deletions
+3
View File
@@ -230,6 +230,9 @@ void mjCMesh::NameSpace(const mjCModel* m) {
if (meshdir_.empty()) {
meshdir_ = FilePath(m->spec_meshdir_);
}
if (!plugin_instance_name.empty()) {
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
}
}
+1
View File
@@ -341,6 +341,7 @@ mjCModel& mjCModel::operator+=(const mjCModel& other) {
std::unordered_map<mjCPlugin*, mjCPlugin*> plugin_map;
for (const auto& plugin : other.plugins_) {
plugins_.push_back(new mjCPlugin(*plugin));
plugins_.back()->NameSpace(&other);
plugin_map[plugin] = plugins_.back();
}
mapplugin(plugin_map, bodies_);
+22
View File
@@ -1016,6 +1016,9 @@ void mjCBody::NameSpace_(const mjCModel* m, bool propagate) {
if (!classname.empty() && m != model) {
classname = m->prefix + classname + m->suffix;
}
if (!plugin_instance_name.empty()) {
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
}
for (auto& body : bodies) {
body->prefix = m->prefix;
@@ -2222,6 +2225,9 @@ void mjCGeom::NameSpace(const mjCModel* m) {
if (!spec_meshname_.empty() && model != m) {
spec_meshname_ = m->prefix + spec_meshname_ + m->suffix;
}
if (!plugin_instance_name.empty()) {
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
}
}
@@ -5649,6 +5655,9 @@ void mjCActuator::NameSpace(const mjCModel* m) {
if (!name.empty()) {
name = m->prefix + name + m->suffix;
}
if (!plugin_instance_name.empty()) {
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
}
spec_target_ = m->prefix + spec_target_ + m->suffix;
spec_refsite_ = m->prefix + spec_refsite_ + m->suffix;
spec_slidersite_ = m->prefix + spec_slidersite_ + m->suffix;
@@ -5979,6 +5988,9 @@ void mjCSensor::NameSpace(const mjCModel* m) {
if (!name.empty()) {
name = m->prefix + name + m->suffix;
}
if (!plugin_instance_name.empty()) {
plugin_instance_name = m->prefix + plugin_instance_name + m->suffix;
}
prefix = m->prefix;
suffix = m->suffix;
}
@@ -6919,6 +6931,7 @@ mjCPlugin::mjCPlugin(mjCModel* _model) {
mjCPlugin::mjCPlugin(const mjCPlugin& other) {
*this = other;
id = -1;
}
@@ -6934,6 +6947,15 @@ mjCPlugin& mjCPlugin::operator=(const mjCPlugin& other) {
void mjCPlugin::NameSpace(const mjCModel* m) {
mjCBase::NameSpace(m);
if (!instance_name.empty()) {
instance_name = m->prefix + instance_name + m->suffix;
}
}
// compiler
void mjCPlugin::Compile(void) {
const mjpPlugin* plugin = mjp_getPluginAtSlot(spec.plugin_slot);
+1
View File
@@ -1422,6 +1422,7 @@ class mjCPlugin : public mjCPlugin_ {
private:
void Compile(void); // compiler
void NameSpace(const mjCModel* m);
};
+9 -3
View File
@@ -243,10 +243,16 @@ TEST_F(PluginTest, AttachPlugin) {
EXPECT_THAT(attachment_frame, NotNull());
mjs_attachBody(attachment_frame, mjs_findBody(spec_2, "body"), "child-", "");
mjModel* model = mj_compile(spec_1, nullptr);
EXPECT_THAT(model, NotNull());
mjModel* model_1 = mj_compile(spec_1, nullptr);
EXPECT_THAT(model_1, NotNull());
mj_deleteModel(model);
// attach it a second time to test namespacing
mjs_attachBody(attachment_frame, mjs_findBody(spec_2, "body"), "copy-", "");
mjModel* model_2 = mj_compile(spec_1, nullptr);
EXPECT_THAT(model_2, NotNull());
mj_deleteModel(model_1);
mj_deleteModel(model_2);
mj_deleteSpec(spec_1);
mj_deleteSpec(spec_2);
}