From c62e731f4d55634285a9e9c76a39d6a31eed0645 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Sat, 28 Nov 2026 11:43:10 +0100 Subject: [PATCH] fix the getMechanicalState binding, add Node.getMechanicalStates(), update the BaseLink test --- .../src/SofaPython3/Sofa/Core/Binding_Node.cpp | 17 ++++++++++++++++- bindings/Sofa/tests/Core/BaseLink.py | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp index e56af868..87db367d 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp @@ -621,13 +621,27 @@ py::object getForceField(Node *self, unsigned int index) py::object getMechanicalState(Node *self) { - sofa::core::behavior::BaseMechanicalState* state = self->mechanicalState.get(); + if (self->mechanicalState.empty()) + { + return py::none(); + } + sofa::core::behavior::BaseMechanicalState* state = self->mechanicalState[0]; if (state) { return PythonFactory::toPython(sofa::core::castToBase(state)); } return py::none(); } +py::list getMechanicalStates(Node *self) +{ + py::list states; + for (const auto& state : self->mechanicalState) + { + states.append(PythonFactory::toPython(sofa::core::castToBase(state))); + } + return states; +} + py::object hasIntegrationScheme(Node *self) { @@ -722,6 +736,7 @@ void moduleAddNode(py::module &m) { p.def("hasIntegrationScheme", &hasIntegrationScheme, sofapython3::doc::sofa::core::Node::hasIntegrationScheme); p.def("getForceField", &getForceField, sofapython3::doc::sofa::core::Node::getForceField); p.def("getMechanicalState", &getMechanicalState, sofapython3::doc::sofa::core::Node::getMechanicalState); + p.def("getMechanicalStates", &getMechanicalStates, "Get the list of all the mechanical states of the node."); p.def("getMechanicalMapping", &getMechanicalMapping, sofapython3::doc::sofa::core::Node::getMechanicalMapping); p.def("sendEvent", &sendEvent, sofapython3::doc::sofa::core::Node::sendEvent); p.def("computeEnergy", &computeEnergy, sofapython3::doc::sofa::core::Node::computeEnergy); diff --git a/bindings/Sofa/tests/Core/BaseLink.py b/bindings/Sofa/tests/Core/BaseLink.py index df485408..e42b36f2 100644 --- a/bindings/Sofa/tests/Core/BaseLink.py +++ b/bindings/Sofa/tests/Core/BaseLink.py @@ -35,7 +35,7 @@ def test_isMultiLink(self): root = create_scene("root") root.addObject("MechanicalObject", name="t") link = root.findLink("mechanicalState") - self.assertFalse(link.isMultiLink()) + self.assertTrue(link.isMultiLink()) root.addChild("aChild") link = root.findLink("child") self.assertTrue(link.isMultiLink())