Replace mjs_setActivePlugins with mjs_activatePlugin.
PiperOrigin-RevId: 680613007 Change-Id: Ic03e0dc42a1f5f083e313672bc6e3ccebadc58b3
This commit is contained in:
committed by
Copybara-Service
parent
cf13413c0b
commit
6832df3091
@@ -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
|
||||
|
||||
@@ -12,6 +12,7 @@ General
|
||||
example model <https://github.com/google-deepmind/mujoco/blob/main/model/flex/floppy.xml>`__ and `2D example model
|
||||
<https://github.com/google-deepmind/mujoco/blob/main/src/model/trampoline.xml>`__ for examples of flex objects
|
||||
that previously required these plugins.
|
||||
- Replaced the function ``mjs_setActivePlugins`` with :ref:`mjs_activatePlugin`.
|
||||
|
||||
Bug fixes
|
||||
^^^^^^^^^
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
+20
-20
@@ -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',
|
||||
|
||||
+15
-10
@@ -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<mjCModel*>(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<mjCBase*>(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<mjCModel*>(s->element);
|
||||
std::vector<std::pair<const mjpPlugin*, int>>* active_plugins =
|
||||
reinterpret_cast<std::vector<std::pair<const mjpPlugin*, int>>*>(activeplugins);
|
||||
modelC->SetActivePlugins(std::move(*active_plugins));
|
||||
}
|
||||
|
||||
|
||||
|
||||
// -------------------------- GLOBAL ASSET CACHE -------------------------------
|
||||
|
||||
void mj_setCacheSize(mjCache cache, std::size_t size) {
|
||||
|
||||
+3
-3
@@ -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);
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include <mujoco/mjdata.h>
|
||||
@@ -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
|
||||
|
||||
@@ -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<std::pair<const mjpPlugin*, int>>&& 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_; }
|
||||
|
||||
@@ -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<std::pair<const mjpPlugin*, int>> 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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<std::pair<const mjpPlugin*, int>> 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<std::pair<const mjpPlugin*, int>> 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;
|
||||
|
||||
Reference in New Issue
Block a user