convenience features for set_*_property_array methods
You need to call set_*_property_array
in order to initialize a property as a 'numpy property', e.g.:
m.set_vertex_property_array('test', np.zeros(m.n_vertices(), 3))
Constructing the second argument can be a bit tedious and I'd like to simplify it by broadening the interface of the set_*_property_array
methods a bit, allowing the following calls:
# the old interface: explicitly setting all values at once;
# data.shape[0] must be equal to the number of elements
m.set_vertex_property_array('test', data)
# setting the same value for each element, e.g. the vector [1,2,3]
m.set_vertex_property_array('test', element_value=[1,2,3])
# setting the same value for each element with a specific shape
# e.g. a 3x3 matrix filled with 4's
# (4 is broadcasted to the 3x3 shape to fill the matrix)
m.set_vertex_property_array('test', element_shape=(3,3), element_value=4)
# creating an uninitialized data array with a specific shape for each element:
m.set_vertex_property_array('test', element_shape=(3,3))
# creating an uninitialized data array with space for a scalar value for each element:
# (common use case)
m.set_vertex_property_array('test')
You can try out this interface by applying the following monkeypatch snippet:
orig_set_vertex_property_array = om.TriMesh.set_vertex_property_array
def svpa(self, prop_name, array=None, element_shape=None, element_value=None):
if element_shape is None:
if element_value is None:
element_shape = ()
else:
element_shape = np.shape(element_value)
if array is None:
if element_value is None:
orig_set_vertex_property_array(self, prop_name, np.empty((self.n_vertices(), *element_shape)))
else:
orig_set_vertex_property_array(self, prop_name, np.array(np.broadcast_to(element_value, (self.n_vertices(), *element_shape))))
else:
assert element_value is None, 'both array and element_value are set'
orig_set_vertex_property_array(self, prop_name, np.reshape(array, (self.n_vertices(), *element_shape)))
om.TriMesh.set_vertex_property_array = svpa
I'm currently not sure whether it's better to implement this on the C++ or Python side. Python code likely makes it easier to use numpy features like broadcast_to
and reshape
but I don't know how to deduplicate the code for the different mesh types and mesh elements.