Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ void removeObject(Node& self, BaseObject* object)
self.removeObject(object);
}

py::object hasObject(Node &n, const std::string &name)
{
BaseObject *object = n.getObject(name);
if (object)
return py::cast(true);
return py::cast(false);
}

/// Implement the addObject function.
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
{
Expand Down Expand Up @@ -477,6 +485,7 @@ void moduleAddNode(py::module &m) {
p.def("addObject", &addObjectKwargs, sofapython3::doc::sofa::core::Node::addObjectKwargs);
p.def("addObject", &addObject, sofapython3::doc::sofa::core::Node::addObject, py::keep_alive<0, 2>());
p.def("createObject", &createObject, sofapython3::doc::sofa::core::Node::createObject);
p.def("hasObject", &hasObject, sofapython3::doc::sofa::core::Node::hasObject);
p.def("addChild", &addChildKwargs, sofapython3::doc::sofa::core::Node::addChildKwargs);
p.def("addChild", &addChild, sofapython3::doc::sofa::core::Node::addChild);
p.def("createChild", &createChild, sofapython3::doc::sofa::core::Node::createChild);
Expand Down
14 changes: 12 additions & 2 deletions bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Node_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,22 @@ static auto addObject =
:type object: Sofa.Simulation.BaseObject*
)";


static auto createObject =
R"(
Deprecated, see addObject
)";

static auto hasObject =
R"(
Check if there is a component with provided name.

:param n
:param name
:type n: Sofa.Simulation.Node
:type name: string
:return: True if the node has an object with correspdonding name.
)";

static auto addChildKwargs =
R"(
Add a child node
Expand Down Expand Up @@ -188,7 +198,7 @@ static auto getChild =
:param name
:type n: Sofa.Simulation.Node
:type name: string
:return: the child of the same name
:return: the child with 'name', None otherwise
)";

static auto removeChild =
Expand Down
13 changes: 13 additions & 0 deletions bindings/Sofa/tests/Simulation/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,19 @@ def test_getLink(self):
self.assertEqual(node.getLinkPath(),"@/node")
self.assertEqual(node2.getLinkPath(), "@/node/node2")

def test_hasObjectWithFastPath(self):
root = Sofa.Core.Node("root")
root.addObject("RequiredPlugin", name="SofaBaseMechanics")
self.assertTrue(root.hasObject("SofaBaseMechanics"))
self.assertFalse(root.hasObject("NonExistingObjectName"))

def test_hasObjectWithDefaultPythonFunction(self):
root = Sofa.Core.Node("root")
root.addObject("RequiredPlugin", name="SofaBaseMechanics")

self.assertTrue(hasattr(root, "SofaBaseMechanics"))
self.assertFalse(hasattr(root, "NonExistingObjectName"))

def test_removeObject(self):
root = Sofa.Core.Node("root")
root.addObject("RequiredPlugin", name="SofaBaseMechanics")
Expand Down