From baa57506e7845ebc3f87ac56241d2d798350e129 Mon Sep 17 00:00:00 2001 From: Robert O'Bara Date: Mon, 17 Mar 2025 15:59:17 -0400 Subject: [PATCH] ENH: Adding Python Array Accessors for Derived Value Items The C++ ValueItemTemplate class provided methods of setting multiple values of the item using iterators. Unfortunately these methods could not be made available in the Python wrapped API. Instead we have added *setValues* and *values* methods to the pybind11 wrapping for DoubleItem, IntItem, and DoubleItem which allows the use of Python arrays to set (and get) all of the values of the Item. --- doc/release/notes/python_changes.rst | 7 +++++++ smtk/attribute/pybind11/PybindDoubleItem.h | 14 ++++++++++++++ smtk/attribute/pybind11/PybindIntItem.h | 14 ++++++++++++++ smtk/attribute/pybind11/PybindStringItem.h | 14 ++++++++++++++ 4 files changed, 49 insertions(+) create mode 100644 doc/release/notes/python_changes.rst diff --git a/doc/release/notes/python_changes.rst b/doc/release/notes/python_changes.rst new file mode 100644 index 0000000000..2047e2fa09 --- /dev/null +++ b/doc/release/notes/python_changes.rst @@ -0,0 +1,7 @@ +Attribute Resource Changes +========================== + +Values methods for Items derived from ValueItems +------------------------------------------------ + +The C++ ValueItemTemplate class provided methods of setting multiple values of the item using iterators. Unfortunately these methods could not be made available in the Python wrapped API. Instead we have added *setValues* and *values* methods to the pybind11 wrapping for DoubleItem, IntItem, and DoubleItem which allows the use of Python arrays to set (and get) all of the values of the Item. diff --git a/smtk/attribute/pybind11/PybindDoubleItem.h b/smtk/attribute/pybind11/PybindDoubleItem.h index e1aa67f8dd..ec93395482 100644 --- a/smtk/attribute/pybind11/PybindDoubleItem.h +++ b/smtk/attribute/pybind11/PybindDoubleItem.h @@ -27,6 +27,20 @@ inline PySharedPtrClass< smtk::attribute::DoubleItem, smtk::attribute::ValueItem .def("hasExplicitUnits", &smtk::attribute::DoubleItem::hasExplicitUnits) .def(py::init<::smtk::attribute::DoubleItem const &>()) .def("type", &smtk::attribute::DoubleItem::type) + .def("setValues", [&](smtk::attribute::DoubleItem* self, const std::vector& values) + { + return self->setValues(values.begin(), values.end()); + }, py::arg("values")) + .def("values", [&](smtk::attribute::DoubleItem* self) -> std::vector + { + std::vector values; + values.reserve(self->numberOfValues()); + for (const auto& vv : *self) + { + values.push_back(vv); + } + return values; + }) .def_static("CastTo", [](const std::shared_ptr i) { return std::dynamic_pointer_cast(i); }) diff --git a/smtk/attribute/pybind11/PybindIntItem.h b/smtk/attribute/pybind11/PybindIntItem.h index c990dc2669..925ca3e76d 100644 --- a/smtk/attribute/pybind11/PybindIntItem.h +++ b/smtk/attribute/pybind11/PybindIntItem.h @@ -23,6 +23,20 @@ inline PySharedPtrClass< smtk::attribute::IntItem, smtk::attribute::ValueItemTem instance .def(py::init<::smtk::attribute::IntItem const &>()) .def("type", &smtk::attribute::IntItem::type) + .def("setValues", [&](smtk::attribute::IntItem* self, const std::vector& values) + { + return self->setValues(values.begin(), values.end()); + }, py::arg("values")) + .def("values", [&](smtk::attribute::IntItem* self) -> std::vector + { + std::vector values; + values.reserve(self->numberOfValues()); + for (const auto& vv : *self) + { + values.push_back(vv); + } + return values; + }) .def_static("CastTo", [](const std::shared_ptr i) { return std::dynamic_pointer_cast(i); }) diff --git a/smtk/attribute/pybind11/PybindStringItem.h b/smtk/attribute/pybind11/PybindStringItem.h index 14d77d9aee..e201fb3b55 100644 --- a/smtk/attribute/pybind11/PybindStringItem.h +++ b/smtk/attribute/pybind11/PybindStringItem.h @@ -24,6 +24,20 @@ inline PySharedPtrClass< smtk::attribute::StringItem, smtk::attribute::ValueItem .def(py::init<::smtk::attribute::StringItem const &>()) .def("isSecure", &smtk::attribute::StringItem::isSecure) .def("type", &smtk::attribute::StringItem::type) + .def("setValues", [&](smtk::attribute::StringItem* self, const std::vector& values) + { + return self->setValues(values.begin(), values.end()); + }, py::arg("values")) + .def("values", [&](smtk::attribute::StringItem* self) -> std::vector + { + std::vector values; + values.reserve(self->numberOfValues()); + for (const auto& vv : *self) + { + values.push_back(vv); + } + return values; + }) .def_static("CastTo", [](const std::shared_ptr i) { return std::dynamic_pointer_cast(i); }) -- GitLab