From 6832df3091bd3acaffebba6493a18864401a37c6 Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Mon, 30 Sep 2024 09:45:25 -0700 Subject: [PATCH] Replace mjs_setActivePlugins with mjs_activatePlugin. PiperOrigin-RevId: 680613007 Change-Id: Ic03e0dc42a1f5f083e313672bc6e3ccebadc58b3 --- doc/APIreference/functions.rst | 18 +++++++-------- doc/changelog.rst | 1 + doc/includes/references.h | 2 +- include/mujoco/mujoco.h | 6 ++--- introspect/functions.py | 40 +++++++++++++++++----------------- src/user/user_api.cc | 25 ++++++++++++--------- src/user/user_api.h | 6 ++--- src/user/user_model.cc | 18 +++++++++++++++ src/user/user_model.h | 4 +--- src/xml/xml_native_reader.cc | 20 +---------------- test/user/user_api_test.cc | 28 ++++-------------------- 11 files changed, 76 insertions(+), 92 deletions(-) diff --git a/doc/APIreference/functions.rst b/doc/APIreference/functions.rst index 1d2bd252..ed317aea 100644 --- a/doc/APIreference/functions.rst +++ b/doc/APIreference/functions.rst @@ -1509,6 +1509,15 @@ mj_deleteSpec Free memory allocation in mjSpec. +.. _mjs_activatePlugin: + +mjs_activatePlugin +~~~~~~~~~~~~~~~~~~ + +.. mujoco-include:: mjs_activatePlugin + +Activate plugin, return slot number. + .. _Errorandmemory: Error and memory @@ -4354,15 +4363,6 @@ Get double array contents and optionally its size. Spec utilities ^^^^^^^^^^^^^^ -.. _mjs_setActivePlugins: - -mjs_setActivePlugins -~~~~~~~~~~~~~~~~~~~~ - -.. mujoco-include:: mjs_setActivePlugins - -Set active plugins. - .. _mjs_setDefault: mjs_setDefault diff --git a/doc/changelog.rst b/doc/changelog.rst index c97f97e6..f2cf0f62 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -12,6 +12,7 @@ General example model `__ and `2D example model `__ for examples of flex objects that previously required these plugins. +- Replaced the function ``mjs_setActivePlugins`` with :ref:`mjs_activatePlugin`. Bug fixes ^^^^^^^^^ diff --git a/doc/includes/references.h b/doc/includes/references.h index 07ea57e2..5f5ecafd 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -3171,6 +3171,7 @@ int mj_setLengthRange(mjModel* m, mjData* d, int index, mjSpec* mj_makeSpec(void); mjSpec* mj_copySpec(const mjSpec* s); void mj_deleteSpec(mjSpec* s); +int mjs_activatePlugin(mjSpec* s, const char* name); void mj_printFormattedModel(const mjModel* m, const char* filename, const char* float_format); void mj_printModel(const mjModel* m, const char* filename); void mj_printFormattedData(const mjModel* m, mjData* d, const char* filename, @@ -3614,7 +3615,6 @@ void mjs_setDouble(mjDoubleVec* dest, const double* array, int size); void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes); const char* mjs_getString(const mjString* source); const double* mjs_getDouble(const mjDoubleVec* source, int* size); -void mjs_setActivePlugins(mjSpec* s, void* activeplugins); void mjs_setDefault(mjsElement* element, mjsDefault* def); void mjs_setFrame(mjsElement* dest, mjsFrame* frame); const char* mjs_resolveOrientation(double quat[4], mjtByte degree, const char* sequence, diff --git a/include/mujoco/mujoco.h b/include/mujoco/mujoco.h index abdfa5cc..e9a41e0d 100644 --- a/include/mujoco/mujoco.h +++ b/include/mujoco/mujoco.h @@ -241,6 +241,9 @@ MJAPI mjSpec* mj_copySpec(const mjSpec* s); // Free memory allocation in mjSpec. MJAPI void mj_deleteSpec(mjSpec* s); +// Activate plugin, return slot number. +MJAPI int mjs_activatePlugin(mjSpec* s, const char* name); + //---------------------------------- Printing ------------------------------------------------------ @@ -1607,9 +1610,6 @@ MJAPI const double* mjs_getDouble(const mjDoubleVec* source, int* size); //---------------------------------- Spec utilities ------------------------------------------------ -// Set active plugins. -MJAPI void mjs_setActivePlugins(mjSpec* s, void* activeplugins); - // Set element's default. MJAPI void mjs_setDefault(mjsElement* element, mjsDefault* def); diff --git a/introspect/functions.py b/introspect/functions.py index 6d796d9e..08f3d44c 100644 --- a/introspect/functions.py +++ b/introspect/functions.py @@ -1045,6 +1045,26 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ), doc='Free memory allocation in mjSpec.', )), + ('mjs_activatePlugin', + FunctionDecl( + name='mjs_activatePlugin', + return_type=ValueType(name='int'), + parameters=( + FunctionParameterDecl( + name='s', + type=PointerType( + inner_type=ValueType(name='mjSpec'), + ), + ), + FunctionParameterDecl( + name='name', + type=PointerType( + inner_type=ValueType(name='char', is_const=True), + ), + ), + ), + doc='Activate plugin, return slot number.', + )), ('mj_printFormattedModel', FunctionDecl( name='mj_printFormattedModel', @@ -10192,26 +10212,6 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ), doc='Get double array contents and optionally its size.', )), - ('mjs_setActivePlugins', - FunctionDecl( - name='mjs_setActivePlugins', - return_type=ValueType(name='void'), - parameters=( - FunctionParameterDecl( - name='s', - type=PointerType( - inner_type=ValueType(name='mjSpec'), - ), - ), - FunctionParameterDecl( - name='activeplugins', - type=PointerType( - inner_type=ValueType(name='void'), - ), - ), - ), - doc='Set active plugins.', - )), ('mjs_setDefault', FunctionDecl( name='mjs_setDefault', diff --git a/src/user/user_api.cc b/src/user/user_api.cc index be7605d6..a401f131 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -205,6 +205,21 @@ void mjs_addSpec(mjSpec* s, mjSpec* child) { +// activate plugin +int mjs_activatePlugin(mjSpec* s, const char* name) { + int plugin_slot = -1; + const mjpPlugin* plugin = mjp_getPlugin(name, &plugin_slot); + if (!plugin) { + mju_error("unknown plugin '%s'", name); + return -1; + } + mjCModel* model = static_cast(s->element); + model->ActivatePlugin(plugin, plugin_slot); + return plugin_slot; +} + + + // delete object, it will call the appropriate destructor since ~mjCBase is virtual void mjs_delete(mjsElement* element) { mjCBase* object = static_cast(element); @@ -1027,16 +1042,6 @@ void mjs_setPluginAttributes(mjsPlugin* plugin, void* attributes) { -// Set active plugins. -void mjs_setActivePlugins(mjSpec* s, void* activeplugins) { - mjCModel* modelC = static_cast(s->element); - std::vector>* active_plugins = - reinterpret_cast>*>(activeplugins); - modelC->SetActivePlugins(std::move(*active_plugins)); -} - - - // -------------------------- GLOBAL ASSET CACHE ------------------------------- void mj_setCacheSize(mjCache cache, std::size_t size) { diff --git a/src/user/user_api.h b/src/user/user_api.h index c2855b54..43cff0c5 100644 --- a/src/user/user_api.h +++ b/src/user/user_api.h @@ -63,6 +63,9 @@ MJAPI void mj_deleteSpec(mjSpec* s); // Add spec (model asset) to spec. MJAPI void mjs_addSpec(mjSpec* s, mjSpec* child); +// Activate plugin, return slot number. +MJAPI int mjs_activatePlugin(mjSpec* s, const char* name); + //---------------------------------- Attachment ---------------------------------------------------- @@ -347,9 +350,6 @@ MJAPI const double* mjs_getDouble(const mjDoubleVec* source, int* size); //---------------------------------- Other utilities ----------------------------------------------- -// Set active plugins. -MJAPI void mjs_setActivePlugins(mjSpec* s, void* activeplugins); - // Set element's default. MJAPI void mjs_setDefault(mjsElement* element, mjsDefault* def); diff --git a/src/user/user_model.cc b/src/user/user_model.cc index ec8ae6aa..a69dfbc4 100644 --- a/src/user/user_model.cc +++ b/src/user/user_model.cc @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -4450,6 +4451,23 @@ bool mjCModel::CopyBack(const mjModel* m) { return true; } + + +void mjCModel::ActivatePlugin(const mjpPlugin* plugin, int slot) { + bool already_declared = false; + for (const auto& [existing_plugin, existing_slot] : active_plugins_) { + if (plugin == existing_plugin) { + already_declared = true; + break; + } + } + if (!already_declared) { + active_plugins_.emplace_back(std::make_pair(plugin, slot)); + } +} + + + void mjCModel::ResolvePlugin(mjCBase* obj, const std::string& plugin_name, const std::string& plugin_instance_name, mjCPlugin** plugin_instance) { // if plugin_name is specified, check if it is in the list of active plugins diff --git a/src/user/user_model.h b/src/user/user_model.h index 06076404..6543ee04 100644 --- a/src/user/user_model.h +++ b/src/user/user_model.h @@ -233,9 +233,7 @@ class mjCModel : public mjCModel_, private mjSpec { mjCBase* FindObject(mjtObj type, std::string name) const; // find object given type and name mjCBase* FindTree(mjCBody* body, mjtObj type, std::string name); // find tree object given name mjSpec* FindSpec(std::string name) const; // find spec given name - void SetActivePlugins(const std::vector>&& active_plugins) { - active_plugins_ = std::move(active_plugins); - } + void ActivatePlugin(const mjpPlugin* plugin, int slot); // activate plugin // accessors std::string get_meshdir() const { return meshdir_; } diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index c6587c2c..daab1331 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -2882,7 +2882,6 @@ void mjXReader::Default(XMLElement* section, const mjsDefault* def, const mjVFS* // extension section parser void mjXReader::Extension(XMLElement* section) { XMLElement* elem = FirstChildElement(section); - std::vector> active_plugins; while (elem) { // get sub-element name @@ -2890,23 +2889,8 @@ void mjXReader::Extension(XMLElement* section) { if (name == "plugin") { string plugin_name; - int plugin_slot = -1; ReadAttrTxt(elem, "plugin", plugin_name, /* required = */ true); - const mjpPlugin* plugin = mjp_getPlugin(plugin_name.c_str(), &plugin_slot); - if (!plugin) { - throw mjXError(elem, "unknown plugin '%s'", plugin_name.c_str()); - } - - bool already_declared = false; - for (const auto& [existing_plugin, existing_slot] : active_plugins) { - if (plugin == existing_plugin) { - already_declared = true; - break; - } - } - if (!already_declared) { - active_plugins.emplace_back(std::make_pair(plugin, plugin_slot)); - } + int plugin_slot = mjs_activatePlugin(spec, plugin_name.c_str()); XMLElement* child = FirstChildElement(elem); while (child) { @@ -2933,8 +2917,6 @@ void mjXReader::Extension(XMLElement* section) { // advance to next element elem = NextSiblingElement(elem); } - - mjs_setActivePlugins(spec, &active_plugins); } diff --git a/test/user/user_api_test.cc b/test/user/user_api_test.cc index 9cfc823d..6e7fd45f 100644 --- a/test/user/user_api_test.cc +++ b/test/user/user_api_test.cc @@ -131,22 +131,12 @@ TEST_F(MujocoTest, TreeTraversal) { } TEST_F(PluginTest, ActivatePlugin) { - std::string plugin_name = "mujoco.elasticity.cable"; mjSpec* spec = mj_makeSpec(); - - // get slot of requested plugin - int plugin_slot = -1; - const mjpPlugin* plugin = mjp_getPlugin(plugin_name.c_str(), &plugin_slot); - EXPECT_THAT(plugin, NotNull()); - - // activated plugin in the slot - std::vector> active_plugins; - active_plugins.emplace_back(std::make_pair(plugin, plugin_slot)); - mjs_setActivePlugins(spec, &active_plugins); + mjs_activatePlugin(spec, "mujoco.elasticity.cable"); // associate plugin to body mjsBody* body = mjs_addBody(mjs_findBody(spec, "world"), 0); - mjs_setString(body->plugin.name, plugin_name.c_str()); + mjs_setString(body->plugin.name, "mujoco.elasticity.cable"); body->plugin.element = mjs_addPlugin(spec)->element; body->plugin.active = true; mjsGeom* geom = mjs_addGeom(body, 0); @@ -166,18 +156,8 @@ TEST_F(PluginTest, ActivatePlugin) { } TEST_F(PluginTest, DeletePlugin) { - std::string plugin_name = "mujoco.pid"; mjSpec* spec = mj_makeSpec(); - - // get slot of requested plugin - int plugin_slot = -1; - const mjpPlugin* plugin = mjp_getPlugin(plugin_name.c_str(), &plugin_slot); - ASSERT_THAT(plugin, NotNull()); - - // activated plugin in the slot - std::vector> active_plugins; - active_plugins.emplace_back(std::make_pair(plugin, plugin_slot)); - mjs_setActivePlugins(spec, &active_plugins); + mjs_activatePlugin(spec, "mujoco.pid"); // create body mjsBody* body = mjs_addBody(mjs_findBody(spec, "world"), 0); @@ -190,7 +170,7 @@ TEST_F(PluginTest, DeletePlugin) { // add actuator mjsActuator* actuator = mjs_addActuator(spec, 0); mjs_setString(actuator->target, "j1"); - mjs_setString(actuator->plugin.name, plugin_name.c_str()); + mjs_setString(actuator->plugin.name, "mujoco.pid"); actuator->plugin.element = mjs_addPlugin(spec)->element; actuator->plugin.active = true; actuator->trntype = mjTRN_JOINT;