13 #ifndef PropertyOwnerBindings_h
14 #define PropertyOwnerBindings_h
20 #include <pybind11/pybind11.h>
22 namespace py = pybind11;
35 template <
typename PyClass>
38 using CppClass =
typename PyClass::type;
42 [](
const CppClass &obj,
const std::string &key,
bool raw) -> py::object
44 auto prop = obj.GetConstProperty(key);
48 return py::cast(prop, py::return_value_policy::reference);
52 py::arg(
"raw") =
false,
53 R
"(Return the property value for *key*, or ``None`` if not set.
55 By default returns a coerced Python-native value: ``bool``, ``int``,
56 ``float``, ``str``, or an ``(r, g, b)`` tuple for ``ColorProperty``. For
57 types without a known Python equivalent the raw ``mitk.BaseProperty``
62 raw: If True, always return the underlying ``mitk.BaseProperty`` object
63 (useful to access metadata or to pass to a C++ function that
64 expects one). Defaults to False.
67 The property value, or ``None`` if the property is not set.
72 [](
const CppClass &obj,
const std::string &key) ->
bool {
return obj.PropertyIsOwned(key); },
74 R
"(Return True if the property *key* is owned (writable) by this object.
76 Returns False if the property does not exist or is provided read-only
77 (for example, routed from an internal component like a Label inside a
78 MultiLabelSegmentation).
86 [](CppClass &obj,
const std::string &key, py::object value)
88 auto existing = obj.GetConstProperty(key);
89 if (existing && !obj.PropertyIsOwned(key))
92 "' is provided read-only by this object "
93 "and cannot be changed via set_property(). "
94 "It may be owned by an internal component "
95 "(e.g., a Label in a MultiLabelSegmentation).");
101 R
"(Set the property *key* to *value*.
103 The value is auto-wrapped into the appropriate ``BaseProperty`` subtype
104 based on its Python type: ``bool`` -> ``BoolProperty``, ``int`` ->
105 ``IntProperty``, ``float`` -> ``DoubleProperty``, ``str`` ->
106 ``StringProperty``, 3-tuple -> ``ColorProperty``, or an existing
107 ``mitk.BaseProperty`` instance is used as-is.
111 value: Property value (auto-wrapped to a ``BaseProperty`` subtype).
114 PropertyNotOwnedError: If the property is provided read-only (not
115 owned) by this object.
120 [](CppClass &obj,
const std::string &key)
122 auto existing = obj.GetConstProperty(key);
123 if (existing && !obj.PropertyIsOwned(key))
126 "' is provided read-only by this object "
127 "and cannot be removed via remove_property().");
129 obj.RemoveProperty(key);
132 R
"(Remove the property *key* if it exists.
138 PropertyNotOwnedError: If the property is provided read-only and
142 cls.def_property_readonly("property_keys",
143 [](
const CppClass &obj)
145 auto keys = obj.GetPropertyKeys();
146 return std::vector<std::string>(keys.begin(), keys.end());
148 "List of all property keys currently set on this object.");
mitk::BaseProperty::Pointer resolvePropertyValue(mitk::IPropertyOwner &owner, const std::string &key, py::object value)
Resolves a Python value to a BaseProperty, respecting any existing property's type.
Centralized property conversion utilities for Python bindings.
void bind_property_owner(PyClass &cls)
Binds IPropertyOwner methods to a Python class.
Thrown when a write operation targets a property that is provided read-only (not owned) by the surrou...
py::object propertyToPythonValue(const mitk::BaseProperty &prop)
Converts a BaseProperty to its Python native value.