From 840d1431bc53c2316b6803f3db7ad6108a889f51 Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Wed, 22 Jul 2020 23:44:14 +0200 Subject: [PATCH] [SofaPython3] Add a per-keyword setData function This is usefull to set a bunch of data fields. # Conflicts: # bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp --- .../src/SofaPython3/Sofa/Core/Binding_Base.cpp | 15 ++++++++++++++- .../Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h | 1 + .../src/SofaPython3/Sofa/Core/Binding_Base_doc.h | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp index cc51a990..02bc214a 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.cpp @@ -25,7 +25,6 @@ along with sofaqtquick. If not, see . - thierry.gaugry@inria.fr ********************************************************************/ - #include #include @@ -395,6 +394,19 @@ py::object BindingBase::getData(Base& self, const std::string& s) return py::none(); } +py::object BindingBase::setDataValues(Base& self, py::kwargs kwargs) +{ + for(auto key : kwargs) + { + BaseData* d = self.findData(py::cast(key.first)); + if(d!=nullptr) + PythonFactory::fromPython(d, py::cast(key.second)); + else + throw py::attribute_error("There is no data field named: "+py::cast(key.first)); + } + return py::none(); +} + void moduleAddBase(py::module &m) { py::class_ base(m, "Base", py::dynamic_attr(), doc::base::BaseClass); @@ -428,6 +440,7 @@ void moduleAddBase(py::module &m) base.def("getLoggedMessagesAsString", &BindingBase::getLoggedMessagesAsString, sofapython3::doc::base::getLoggedMessagesAsString); base.def("countLoggedMessages", &BindingBase::countLoggedMessages, sofapython3::doc::base::countLoggedMessages); base.def("clearLoggedMessages", &BindingBase::clearLoggedMessages, sofapython3::doc::base::clearLoggedMessages); + base.def("setDataValues", &BindingBase::setDataValues, sofapython3::doc::base::setDataValues); } } /// namespace sofapython3 diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h index f5c9616c..884107db 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base.h @@ -65,6 +65,7 @@ class BindingBase /// Set the data field value from the array. static void SetDataFromArray(BaseData* data, const py::array& value); static bool SetData(BaseData* data, pybind11::object value); + static py::object setDataValues(Base& self, py::kwargs kwargs); static py::list getDataFields(Base& self); static py::list getLinks(Base& self); diff --git a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base_doc.h b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base_doc.h index 428a0089..870bc498 100644 --- a/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base_doc.h +++ b/bindings/Sofa/src/SofaPython3/Sofa/Core/Binding_Base_doc.h @@ -70,6 +70,10 @@ static auto findData = :type name: string :return: the data field )"; +static auto setDataValues = + R"( + Set values for a the given data field, multiple pairs of args are allowed. + )"; static auto getDataFields = R"( Accessor to the vector containing all the fields of this object