From a26ba2012e5715103e81b3872129dbb3a7e5aa4f Mon Sep 17 00:00:00 2001 From: Alessio Quaglino Date: Wed, 9 Oct 2024 11:08:38 -0700 Subject: [PATCH] Return 0 on success from mjs_activatePlugin instead of using mju_error. PiperOrigin-RevId: 684102823 Change-Id: I86b7702094ae886539e1dfb6294381952d507bc1 --- doc/APIreference/functions.rst | 2 +- doc/includes/references.h | 2 +- include/mujoco/mujoco.h | 4 ++-- introspect/functions.py | 4 ++-- src/user/user_api.cc | 5 +++-- src/user/user_api.h | 4 ++-- src/xml/xml_native_reader.cc | 4 +++- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/doc/APIreference/functions.rst b/doc/APIreference/functions.rst index 43d4f84b..56bc272e 100644 --- a/doc/APIreference/functions.rst +++ b/doc/APIreference/functions.rst @@ -1516,7 +1516,7 @@ mjs_activatePlugin .. mujoco-include:: mjs_activatePlugin -Activate plugin. +Activate plugin. Returns 0 on success. .. _Errorandmemory: diff --git a/doc/includes/references.h b/doc/includes/references.h index 8b4ad04e..6e193719 100644 --- a/doc/includes/references.h +++ b/doc/includes/references.h @@ -3178,7 +3178,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); -void mjs_activatePlugin(mjSpec* s, const char* name); +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, diff --git a/include/mujoco/mujoco.h b/include/mujoco/mujoco.h index 879ae85a..c146054c 100644 --- a/include/mujoco/mujoco.h +++ b/include/mujoco/mujoco.h @@ -241,8 +241,8 @@ MJAPI mjSpec* mj_copySpec(const mjSpec* s); // Free memory allocation in mjSpec. MJAPI void mj_deleteSpec(mjSpec* s); -// Activate plugin. -MJAPI void mjs_activatePlugin(mjSpec* s, const char* name); +// Activate plugin. Returns 0 on success. +MJAPI int mjs_activatePlugin(mjSpec* s, const char* name); //---------------------------------- Printing ------------------------------------------------------ diff --git a/introspect/functions.py b/introspect/functions.py index 0d576b8e..447b2e55 100644 --- a/introspect/functions.py +++ b/introspect/functions.py @@ -1048,7 +1048,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ('mjs_activatePlugin', FunctionDecl( name='mjs_activatePlugin', - return_type=ValueType(name='void'), + return_type=ValueType(name='int'), parameters=( FunctionParameterDecl( name='s', @@ -1063,7 +1063,7 @@ FUNCTIONS: Mapping[str, FunctionDecl] = dict([ ), ), ), - doc='Activate plugin.', + doc='Activate plugin. Returns 0 on success.', )), ('mj_printFormattedModel', FunctionDecl( diff --git a/src/user/user_api.cc b/src/user/user_api.cc index 9dba3c81..12b7a1d2 100644 --- a/src/user/user_api.cc +++ b/src/user/user_api.cc @@ -206,14 +206,15 @@ void mjs_addSpec(mjSpec* s, mjSpec* child) { // activate plugin -void mjs_activatePlugin(mjSpec* s, const char* name) { +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 0; } diff --git a/src/user/user_api.h b/src/user/user_api.h index f3f25ebc..853c9363 100644 --- a/src/user/user_api.h +++ b/src/user/user_api.h @@ -63,8 +63,8 @@ MJAPI void mj_deleteSpec(mjSpec* s); // Add spec (model asset) to spec. MJAPI void mjs_addSpec(mjSpec* s, mjSpec* child); -// Activate plugin. -MJAPI void mjs_activatePlugin(mjSpec* s, const char* name); +// Activate plugin, return 0 on success. +MJAPI int mjs_activatePlugin(mjSpec* s, const char* name); //---------------------------------- Attachment ---------------------------------------------------- diff --git a/src/xml/xml_native_reader.cc b/src/xml/xml_native_reader.cc index 4bcd7cd1..404420cc 100644 --- a/src/xml/xml_native_reader.cc +++ b/src/xml/xml_native_reader.cc @@ -2890,7 +2890,9 @@ void mjXReader::Extension(XMLElement* section) { if (name == "plugin") { string plugin_name; ReadAttrTxt(elem, "plugin", plugin_name, /* required = */ true); - mjs_activatePlugin(spec, plugin_name.c_str()); + if (mjs_activatePlugin(spec, plugin_name.c_str())) { + throw mjXError(elem, "plugin %s not found", plugin_name.c_str()); + } XMLElement* child = FirstChildElement(elem); while (child) {