Skip to content

Commit 2ca7d26

Browse files
[Binding/Sofa.Core] add getObject method to Sofa.Node. (#175)
1 parent 0d7e07f commit 2ca7d26

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,28 @@ py::object hasObject(Node &n, const std::string &name)
167167
return py::cast(false);
168168
}
169169

170+
py::object getObject(Node &n, const std::string &name, const py::kwargs& kwargs)
171+
{
172+
if(kwargs.size()!=0)
173+
{
174+
msg_deprecated(&n) << "Calling the method getObject() with extra arguments is not supported anymore."
175+
<< "To remove this message please refer to the documentation of the getObject method";
176+
}
177+
178+
BaseObject *object = n.getObject(name);
179+
if (object)
180+
return PythonFactory::toPython(object);
181+
return py::none();
182+
}
183+
170184
/// Implement the addObject function.
171185
py::object addObjectKwargs(Node* self, const std::string& type, const py::kwargs& kwargs)
172186
{
173187
if (kwargs.contains("name"))
174188
{
175189
std::string name = py::str(kwargs["name"]);
176190
if (sofapython3::isProtectedKeyword(name))
177-
throw py::value_error("addObject: Cannot call addObject with name " + name + ": Protected keyword");
191+
throw py::value_error("Cannot call addObject with name " + name + ": Protected keyword");
178192
}
179193
/// Prepare the description to hold the different python attributes as data field's
180194
/// arguments then create the object.
@@ -230,7 +244,6 @@ py::object addKwargs(Node* self, const py::object& callable, const py::kwargs& k
230244
self->addChild(node);
231245
return py::cast(node);
232246
}
233-
234247
if(py::isinstance<py::str>(callable))
235248
{
236249
py::str type = callable;
@@ -507,6 +520,7 @@ void moduleAddNode(py::module &m) {
507520
p.def("addObject", &addObject, sofapython3::doc::sofa::core::Node::addObject, py::keep_alive<0, 2>());
508521
p.def("createObject", &createObject, sofapython3::doc::sofa::core::Node::createObject);
509522
p.def("hasObject", &hasObject, sofapython3::doc::sofa::core::Node::hasObject);
523+
p.def("getObject", &getObject, sofapython3::doc::sofa::core::Node::getObject);
510524
p.def("addChild", &addChildKwargs, sofapython3::doc::sofa::core::Node::addChildKwargs);
511525
p.def("addChild", &addChild, sofapython3::doc::sofa::core::Node::addChild);
512526
p.def("createChild", &createChild, sofapython3::doc::sofa::core::Node::createChild);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,34 @@ static auto hasObject =
161161
:type name: string
162162
:return: True if the node has an object with correspdonding name.
163163
)";
164+
static auto getObject =
165+
R"(
166+
Get a sofa component hold by a node.
167+
168+
:param n
169+
:param name
170+
:type n: Sofa.Simulation.Node
171+
:type name: string
172+
:return: the component with 'name', None otherwise
173+
174+
.. note::
175+
The extra arguments allowed in the SofaPython (warning=True/False) binding are not supported SofaPython3.
176+
177+
.. code-block:: python
178+
# SofaPython2:
179+
if node.getObject("MyObject",warning=False):
180+
pass
181+
182+
# SofaPython3:
183+
if node.getObject("MyObject") != None:
184+
pass
185+
186+
if node.hasObject("MyObject"):
187+
pass
188+
189+
if "MyObject" in node.objects:
190+
pass
191+
)";
164192

165193
static auto addChildKwargs =
166194
R"(

0 commit comments

Comments
 (0)