Skip to content

Commit 012b86d

Browse files
[SofaPython3] Add an hasObject method in Binding_Node to detect if a node has an object. (#154)
With the current binding it is possible to detect if a node has an object using the default python hasattr() function. But because of how hasattr() is implement it imply the generation of the bindinged object regardeless this is usefull or not from the callee's point of view. So we add a fast path version, with the corresponding documentation and tests.
1 parent 8d83a97 commit 012b86d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ void removeObject(Node& self, BaseObject* object)
159159
self.removeObject(object);
160160
}
161161

162+
py::object hasObject(Node &n, const std::string &name)
163+
{
164+
BaseObject *object = n.getObject(name);
165+
if (object)
166+
return py::cast(true);
167+
return py::cast(false);
168+
}
169+
162170
/// Implement the addObject function.
163171
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
164172
{
@@ -477,6 +485,7 @@ void moduleAddNode(py::module &m) {
477485
p.def("addObject", &addObjectKwargs, sofapython3::doc::sofa::core::Node::addObjectKwargs);
478486
p.def("addObject", &addObject, sofapython3::doc::sofa::core::Node::addObject, py::keep_alive<0, 2>());
479487
p.def("createObject", &createObject, sofapython3::doc::sofa::core::Node::createObject);
488+
p.def("hasObject", &hasObject, sofapython3::doc::sofa::core::Node::hasObject);
480489
p.def("addChild", &addChildKwargs, sofapython3::doc::sofa::core::Node::addChildKwargs);
481490
p.def("addChild", &addChild, sofapython3::doc::sofa::core::Node::addChild);
482491
p.def("createChild", &createChild, sofapython3::doc::sofa::core::Node::createChild);

bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node_doc.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,22 @@ static auto addObject =
146146
:type object: Sofa.Simulation.BaseObject*
147147
)";
148148

149-
150149
static auto createObject =
151150
R"(
152151
Deprecated, see addObject
153152
)";
154153

154+
static auto hasObject =
155+
R"(
156+
Check if there is a component with provided name.
157+
158+
:param n
159+
:param name
160+
:type n: Sofa.Simulation.Node
161+
:type name: string
162+
:return: True if the node has an object with correspdonding name.
163+
)";
164+
155165
static auto addChildKwargs =
156166
R"(
157167
Add a child node
@@ -188,7 +198,7 @@ static auto getChild =
188198
:param name
189199
:type n: Sofa.Simulation.Node
190200
:type name: string
191-
:return: the child of the same name
201+
:return: the child with 'name', None otherwise
192202
)";
193203

194204
static auto removeChild =

bindings/Sofa/tests/Simulation/Node.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ def test_getLink(self):
174174
self.assertEqual(node.getLinkPath(),"@/node")
175175
self.assertEqual(node2.getLinkPath(), "@/node/node2")
176176

177+
def test_hasObjectWithFastPath(self):
178+
root = Sofa.Core.Node("root")
179+
root.addObject("RequiredPlugin", name="SofaBaseMechanics")
180+
self.assertTrue(root.hasObject("SofaBaseMechanics"))
181+
self.assertFalse(root.hasObject("NonExistingObjectName"))
182+
183+
def test_hasObjectWithDefaultPythonFunction(self):
184+
root = Sofa.Core.Node("root")
185+
root.addObject("RequiredPlugin", name="SofaBaseMechanics")
186+
187+
self.assertTrue(hasattr(root, "SofaBaseMechanics"))
188+
self.assertFalse(hasattr(root, "NonExistingObjectName"))
189+
177190
def test_removeObject(self):
178191
root = Sofa.Core.Node("root")
179192
root.addObject("RequiredPlugin", name="SofaBaseMechanics")

0 commit comments

Comments
 (0)