Add plugins list to mjSpec Python API.
Fixes #2061. PiperOrigin-RevId: 677762985 Change-Id: I8e407373e7b505d9b0f1c023e97c5ea92169bbf6
This commit is contained in:
committed by
Copybara-Service
parent
91cedfd65c
commit
0ba11b9623
+15
-3
@@ -274,6 +274,18 @@ PYBIND11_MODULE(_specs, m) {
|
||||
mjSpec.def("detach_body", [](MjSpec& self, raw::MjsBody& body) {
|
||||
mjs_detachBody(self.ptr, &body);
|
||||
});
|
||||
mjSpec.def_property_readonly(
|
||||
"plugins",
|
||||
[](MjSpec& self) -> py::list {
|
||||
py::list list;
|
||||
raw::MjsElement* el = mjs_firstElement(self.ptr, mjOBJ_PLUGIN);
|
||||
while (el) {
|
||||
list.append(mjs_asPlugin(el));
|
||||
el = mjs_nextElement(self.ptr, el);
|
||||
}
|
||||
return list;
|
||||
},
|
||||
py::return_value_policy::reference_internal);
|
||||
mjSpec.def_property_readonly(
|
||||
"actuators",
|
||||
[](MjSpec& self) -> py::list {
|
||||
@@ -900,12 +912,12 @@ PYBIND11_MODULE(_specs, m) {
|
||||
// ============================= MJSPLUGIN ===================================
|
||||
mjsPlugin.def_property(
|
||||
"id",
|
||||
[](raw::MjsPlugin& self) -> int { return mjs_getId(self.instance); },
|
||||
[](raw::MjsPlugin& self) -> int { return mjs_getId(self.element); },
|
||||
[](raw::MjsPlugin& self, raw::MjsPlugin* other) {
|
||||
self.instance = other->instance;
|
||||
self.element = other->element;
|
||||
});
|
||||
mjsPlugin.def("delete",
|
||||
[](raw::MjsPlugin& self) { mjs_delete(self.instance); });
|
||||
[](raw::MjsPlugin& self) { mjs_delete(self.element); });
|
||||
|
||||
#include "specs.cc.inc"
|
||||
} // PYBIND11_MODULE // NOLINT
|
||||
|
||||
@@ -690,5 +690,32 @@ class SpecsTest(absltest.TestCase):
|
||||
):
|
||||
spec.recompile(model, data)
|
||||
|
||||
def test_delete_unused_plugin(self):
|
||||
spec = mujoco.MjSpec()
|
||||
spec.from_string(textwrap.dedent("""
|
||||
<mujoco model="MuJoCo Model">
|
||||
<extension>
|
||||
<plugin plugin="mujoco.pid">
|
||||
<instance name="pid1">
|
||||
<config key="kp" value="4.0"/>
|
||||
</instance>
|
||||
</plugin>
|
||||
</extension>
|
||||
|
||||
<worldbody>
|
||||
<body>
|
||||
<geom size="1"/>
|
||||
</body>
|
||||
</worldbody>
|
||||
</mujoco>
|
||||
"""))
|
||||
plugin = spec.plugins[0]
|
||||
self.assertIsNotNone(plugin)
|
||||
plugin.delete()
|
||||
|
||||
model = spec.compile()
|
||||
self.assertIsNotNone(model)
|
||||
self.assertEqual(model.nplugin, 0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
absltest.main()
|
||||
|
||||
Reference in New Issue
Block a user