\(\renewcommand\AA{\unicode{x212B}}\)
Array Properties¶
So far we have dealt with properties that contain a single item: int, float,
string or workspace. In order to provide multiple items as input for a
single property, i.e. a list of values, we must use a different type of
property called an ArrayProperty
.
While a Python list is capable of storing items of any type in the one list,
the same is not true in C++
. For this reason it is only possible for an
array property to store a single type. This choice must be made when the
property is declared. The options are:
FloatArrayProperty - Stores a list of floats/doubles.
IntArrayProperty - Stores a list of integers.
StringArrayProperty - Stores a list of strings.
The simplest use of each is where the default value is empty:
def PyInit(self):
self.declareProperty(FloatArrayProperty("Floats",
direction=Direction.Input),
doc='Input doubles')
self.declareProperty(IntArrayProperty("Ints",
direction=Direction.Input),
doc='Input integers')
self.declareProperty(StringArrayProperty("Strings",
direction=Direction.Input),
doc='Input strings')
Default values for the list can be specified as a python list
, a numpy
array or a comma-separated string using the values
keyword.
def PyInit(self):
self.declareProperty(FloatArrayProperty(name="PythonListInput",
values=[1.2,4.5,6.7],
direction=Direction.Input))
Validation¶
As with the other property types there is an option to supply a validator
using the validator
keyword. The available validators are:
FloatArrayLengthValidator, IntArrayLengthValidator, StringArrayLengthValidator - Verify that the array is of a given length.
FloatArrayBoundedValidator, IntArrayBoundedValidator - Verify that each value in the array is within the given bounds.
The prefix, Float, Int, String, must match the property type:
def PyInit(self):
length_validator = FloatArrayLengthValidator(5)
self.declareProperty(FloatArrayProperty("Floats",
validator=length_validator,
direction=Direction.Input))