Skip to content

Commit db072c9

Browse files
Merge branch 'master' into pr-remove-dagnode
2 parents 459e049 + 2a6f73e commit db072c9

File tree

8 files changed

+921
-293
lines changed

8 files changed

+921
-293
lines changed

Plugin/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ sofa_create_component_in_package_with_targets(
7171
INCLUDE_SOURCE_DIR "src"
7272
INCLUDE_INSTALL_DIR "."
7373
OPTIMIZE_BUILD_DIR FALSE
74-
RELOCATABLE "../../plugins"
74+
RELOCATABLE_PATH "../../plugins"
7575
)

Plugin/src/SofaPython3/DataHelper.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,28 @@ BaseLink* addLink(py::object py_self, const std::string& name, py::object value,
659659
return link;
660660
}
661661

662+
663+
void setDataFromKwargs(Base* obj, const pybind11::kwargs& kwargs)
664+
{
665+
const auto typeHandleBaseData = py::detail::get_type_handle(typeid(BaseData), false);
666+
const auto typeHandleLinkPath = py::detail::get_type_handle(typeid(LinkPath), false);
667+
668+
for (auto & kv : kwargs)
669+
{
670+
BaseData* d = obj->findData(py::cast<std::string>(kv.first));
671+
if(d)
672+
{
673+
if (py::isinstance(kv.second, typeHandleBaseData))
674+
d->setParent(kv.second.cast<BaseData*>());
675+
else if (py::isinstance(kv.second, typeHandleLinkPath))
676+
d->setParent(py::str(kv.second));
677+
else if (py::isinstance<py::str>(kv.second))
678+
d->read(py::str(kv.second));
679+
else
680+
PythonFactory::fromPython(d, py::cast<py::object>(kv.second));
681+
}
682+
}
683+
}
684+
685+
662686
} // namespace sofapython3

Plugin/src/SofaPython3/DataHelper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sofa/core/objectmodel/BaseNode.h>
3333
#include <SofaPython3/config.h>
3434

35+
3536
////////////////////////// FORWARD DECLARATION ///////////////////////////
3637
namespace sofa {
3738
namespace defaulttype {
@@ -292,6 +293,9 @@ SOFAPYTHON3_API BaseData* addData(pybind11::object py_self, const std::string& n
292293
SOFAPYTHON3_API BaseLink* addLink(pybind11::object py_self, const std::string& name, pybind11::object value, const std::string& help);
293294
SOFAPYTHON3_API bool isProtectedKeyword(const std::string& name);
294295

296+
297+
SOFAPYTHON3_API void setDataFromKwargs(Base* obj, const pybind11::kwargs& kwargs);
298+
295299
} // namespace sofapython3
296300

297301

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ py_shared_ptr<Node> __init__(const std::string& name) {
173173
return std::move(dag_node);
174174
}
175175

176+
py_shared_ptr<Node> __init__(const std::string& name, const py::kwargs& kwargs) {
177+
auto dag_node = sofa::core::objectmodel::New<sofa::simulation::graph::DAGNode>(name);
178+
179+
setDataFromKwargs(dag_node.get(), kwargs);
180+
181+
return std::move(dag_node);
182+
}
183+
176184
/// Method: init (beware this is not the python __init__, this is sofa's init())
177185
void init(Node& self) { self.init(sofa::core::execparams::defaultInstance()); }
178186

@@ -680,7 +688,8 @@ void moduleAddNode(py::module &m) {
680688
});
681689

682690
p.def(py::init(&__init__noname), sofapython3::doc::sofa::core::Node::init);
683-
p.def(py::init(&__init__), sofapython3::doc::sofa::core::Node::init1Arg, py::arg("name"));
691+
p.def(py::init(&__init__no_kwargs__), sofapython3::doc::sofa::core::Node::init1Arg, py::arg("name"));
692+
p.def(py::init(&__init__), sofapython3::doc::sofa::core::Node::init1Arg);
684693
p.def("init", &init, sofapython3::doc::sofa::core::Node::initSofa );
685694
p.def("add", &addKwargs, sofapython3::doc::sofa::core::Node::addKwargs);
686695
p.def("addObject", &addObjectKwargs, sofapython3::doc::sofa::core::Node::addObjectKwargs);

0 commit comments

Comments
 (0)