13 #ifndef PropertyAutoWrap_h
14 #define PropertyAutoWrap_h
21 #include <pybind11/pybind11.h>
23 namespace py = pybind11;
45 if (py::isinstance<mitk::BaseProperty>(value))
46 return value.cast<mitk::BaseProperty::Pointer>();
49 if (py::isinstance<py::bool_>(value))
50 return mitk::BoolProperty::New(value.cast<
bool>());
52 if (py::isinstance<py::int_>(value))
53 return mitk::IntProperty::New(value.cast<
int>());
56 if (py::isinstance<py::float_>(value))
57 return mitk::DoubleProperty::New(value.cast<
double>());
59 if (py::isinstance<py::str>(value))
62 if (py::isinstance<mitk::Color>(value))
69 if (py::isinstance<py::tuple>(value))
71 auto t = value.cast<py::tuple>();
75 c[0] = t[0].cast<
float>();
76 c[1] = t[1].cast<
float>();
77 c[2] = t[2].cast<
float>();
82 throw py::type_error(
"Cannot auto-convert " + std::string(py::str(py::type::of(value))) +
83 " to a property. Pass a mitk.BaseProperty subclass explicitly "
84 "(e.g. mitk.StringProperty(...), mitk.ColorProperty(...)).");
95 if (py::isinstance<mitk::BaseProperty>(value))
96 return value.cast<mitk::BaseProperty::Pointer>();
98 if (targetType.empty())
101 if (targetType ==
"StringProperty")
104 if (targetType ==
"BoolProperty")
105 return mitk::BoolProperty::New(value.cast<
bool>());
107 if (targetType ==
"IntProperty")
108 return mitk::IntProperty::New(value.cast<
int>());
110 if (targetType ==
"FloatProperty")
111 return mitk::FloatProperty::New(value.cast<
float>());
113 if (targetType ==
"DoubleProperty")
114 return mitk::DoubleProperty::New(value.cast<
double>());
116 if (targetType ==
"ColorProperty")
120 if (py::isinstance<mitk::Color>(value))
122 if (py::isinstance<py::sequence>(value))
124 auto seq = value.cast<py::sequence>();
126 c[0] = seq[0].cast<
float>();
127 c[1] = seq[1].cast<
float>();
128 c[2] = seq[2].cast<
float>();
131 throw py::type_error(
"ColorProperty requires a mitk.Color or a 3-element (r,g,b) sequence.");
134 throw py::type_error(
"Cannot coerce " + std::string(py::str(py::type::of(value))) +
" to " + targetType +
135 ". Pass the new property value as a mitk." + targetType +
" instance explicitly.");
143 if (
nullptr == existing)
144 throw std::runtime_error(
"Cannot coerce to existing type: no existing property");
158 const std::string &key,
161 if (py::isinstance<mitk::BaseProperty>(value))
162 return value.cast<mitk::BaseProperty::Pointer>();
mitk::BaseProperty::Pointer pythonValueToProperty(py::object value)
Converts a Python object to a BaseProperty, auto-wrapping common scalar types.
mitk::BaseProperty::Pointer coerceToExistingType(const mitk::BaseProperty *existing, py::object value)
Coerces a Python value to the class of an existing property.
mitk::BaseProperty::Pointer pythonValueToPropertyWithType(py::object value, const std::string &targetType)
Converts a Python object to a BaseProperty, coercing to a specific target class when given.
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.
Abstract base class for properties.
Interface implemented by classes that provide write/read access to property information.
virtual BaseProperty::ConstPointer GetConstProperty(const std::string &propertyKey, const std::string &contextName="", bool fallBackOnDefaultContext=true) const =0
Get property by its key.
Declares commonly used GenericProperty specializations for MITK.
itk::RGBPixel< float > Color
Standard RGB color typedef using float components.