diff --git a/doc/includes/references.h b/doc/includes/references.h index c6c06f5c..15345e99 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -3542,7 +3542,7 @@ mjsText* mjs_addText(mjSpec* s); mjsTuple* mjs_addTuple(mjSpec* s); mjsKey* mjs_addKey(mjSpec* s); mjsPlugin* mjs_addPlugin(mjSpec* s); -mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id); +mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent); mjsMesh* mjs_addMesh(mjSpec* s, mjsDefault* def); mjsHField* mjs_addHField(mjSpec* s); mjsSkin* mjs_addSkin(mjSpec* s); diff --git a/include/mujoco/mujoco.h b/include/mujoco/mujoco.h index 6cb6f7e5..4a45bc72 100644 --- a/include/mujoco/mujoco.h +++ b/include/mujoco/mujoco.h @@ -1492,7 +1492,7 @@ MJAPI mjsKey* mjs_addKey(mjSpec* s); MJAPI mjsPlugin* mjs_addPlugin(mjSpec* s); // Add default. -MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id); +MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent); //---------------------------------- Assets -------------------------------------------------------- diff --git a/introspect/functions.py b/introspect/functions.py index 58985da5..a4808e53 100644 --- a/introspect/functions.py +++ b/introspect/functions.py @@ -9499,13 +9499,9 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ), ), FunctionParameterDecl( - name='parentid', - type=ValueType(name='int'), - ), - FunctionParameterDecl( - name='id', + name='parent', type=PointerType( - inner_type=ValueType(name='int'), + inner_type=ValueType(name='mjsDefault', is_const=True), ), ), ), diff --git a/src/user/user_api.cc b/src/user/user_api.cc index de3e58af..f71fa176 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -437,10 +437,11 @@ mjsPlugin* mjs_addPlugin(mjSpec* s) { // add default to model -mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id) { +mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent) { mjCModel* modelC = static_cast(s->element); - *id = (int)modelC->NumDefaults(); - mjCDef* def = modelC->AddDefault(classname, parentid); + mjCDef* parentC = parent ? static_cast(parent->element) : + static_cast(s->element)->Defaults(0); + mjCDef* def = modelC->AddDefault(classname, parentC); if (def) { return &def->spec; } else { diff --git a/src/user/user_api.h b/src/user/user_api.h index 168ae6d2..9e30b105 100644 --- a/src/user/user_api.h +++ b/src/user/user_api.h @@ -156,7 +156,7 @@ MJAPI mjsKey* mjs_addKey(mjSpec* s); MJAPI mjsPlugin* mjs_addPlugin(mjSpec* s); // Add default. -MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, int parentid, int* id); +MJAPI mjsDefault* mjs_addDefault(mjSpec* s, const char* classname, const mjsDefault* parent); //---------------------------------- Add assets ---------------------------------------------------- diff --git a/src/user/user_model.cc b/src/user/user_model.cc index 23f94634..0fadddd0 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -741,7 +741,9 @@ mjCDef* mjCModel::FindDefault(string name) { // add default class to array -mjCDef* mjCModel::AddDefault(string name, int parentid) { +mjCDef* mjCModel::AddDefault(string name, mjCDef* parent) { + int parentid = parent ? parent->id : 0; + // check for repeated name int thisid = (int)defaults_.size(); for (int i=0; iid = thisid; // initialize contents if (parentid>=0 && parentid>&& active_plugins) { active_plugins_ = std::move(active_plugins); } diff --git a/src/user/user_objects.cc b/src/user/user_objects.cc index 68fb0a92..4f10402a 100644 --- a/src/user/user_objects.cc +++ b/src/user/user_objects.cc @@ -517,6 +517,7 @@ int mjCBoundingVolumeHierarchy::MakeBVH( // constructor mjCDef::mjCDef() { name.clear(); + id = 0; parentid = -1; childid.clear(); mjs_defaultJoint(&joint_.spec); diff --git a/src/user/user_objects.h b/src/user/user_objects.h index 4e1b8a9f..9c50b483 100644 --- a/src/user/user_objects.h +++ b/src/user/user_objects.h @@ -1651,6 +1651,7 @@ class mjCDef : public mjsElement { // identifiers std::string name; // class name + int id; // id of this default int parentid; // id of parent class std::vector childid; // ids of child classes diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index 250f2bfe..6e685c76 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -29,6 +29,7 @@ #include "tinyxml2.h" +#include #include #include #include @@ -862,7 +863,7 @@ void mjXReader::Parse(XMLElement* root) { readingdefaults = true; for (XMLElement* section = FirstChildElement(root, "default"); section; section = NextSiblingElement(section, "default")) { - Default(section, -1); + Default(section, nullptr); } readingdefaults = false; @@ -2667,27 +2668,24 @@ void mjXReader::OnePlugin(XMLElement* elem, mjsPlugin* plugin) { //------------------ MJCF-specific sections -------------------------------------------------------- // default section parser -void mjXReader::Default(XMLElement* section, int parentid) { +void mjXReader::Default(XMLElement* section, const mjsDefault* def) { XMLElement* elem; string text, name; - mjsDefault* def; - int thisid; // create new default, except at top level (already added in mjCModel constructor) text.clear(); ReadAttrTxt(section, "class", text); if (text.empty()) { - if (parentid >= 0) { + if (def) { throw mjXError(section, "empty class name"); } } - if (parentid >= 0) { - def = mjs_addDefault(model, text.c_str(), parentid, &thisid); + if (def) { + def = mjs_addDefault(model, text.c_str(), def); if (!def) { throw mjXError(section, "repeated default class name"); } } else { - thisid = 0; def = mjs_getSpecDefault(model); if (!text.empty() && text != "main") { throw mjXError(section, "top-level default class 'main' cannot be renamed"); @@ -2755,7 +2753,7 @@ void mjXReader::Default(XMLElement* section, int parentid) { // read default if (name=="default") { - Default(elem, thisid); + Default(elem, def); } // advance diff --git a/src/xml/xml_native_reader.h b/src/xml/xml_native_reader.h index aa20841a..f2049bec 100644 --- a/src/xml/xml_native_reader.h +++ b/src/xml/xml_native_reader.h @@ -48,7 +48,7 @@ class mjXReader : public mjXBase { private: // XML section specific to MJCF - void Default(tinyxml2::XMLElement* section, int parentid); // default section + void Default(tinyxml2::XMLElement* section, const mjsDefault* def); // default section void Extension(tinyxml2::XMLElement* section); // extension section void Custom(tinyxml2::XMLElement* section); // custom section void Visual(tinyxml2::XMLElement* section); // visual section