Updating vertex coordinates



I was looking for the “proper” way to update vertex coordinates of a Mesh; and the documented examples show the following:

for key, attr in mesh.vertices(True):
    attr['x'] = vertices[key][0]
    attr['y'] = vertices[key][1]
    attr['z'] = vertices[key][2]

Since getting and setting vertex attributes, and it particular its coords seems to be a fairly common operation (see also this thread), wouldn’t it make sense to add get_vertex_attributes and set_vertex_attributes methods to Mesh to manipulate them easily? And since coords are probably very, very often used, maybe also consider adding syntactical sugar methods for get_vertex_coords and set_vertex_coords specifically.



hi gonzalo,

all of the suggested functions already exist, but clearly they need to be documented (better) :slight_smile:

their syntax is also not entirely consistent…

xyz = [mesh.vertex_coordinates(key) for key in mesh.vertices]
xyz = mesh.get_vertices_attributes('xyz')
xyz = mesh.get_vertices_attributes(('x', 'y', 'z'))

for getting stuff there are also the variations


however, use of the setters is not very obvious and actually also not very convenient (you have to pass dicts of attributes, which doesn’t make much sense).
ideally, they can be used as per the example below, but that is not the case at the moment…

mesh.set_vertices_attributes('xyz', xyz)

with xyz a list of XYZ coordinates per vertex.

note that both for the setters and the getters, there is the option to provide a list of keys (in the case of the _vertices_ variant) of the vertices to which the function should be applied.

anyway, i will update the setters to be the opposite of the getters, because that would make the most sense.



by the way, you can find the implementation details in compas.datastructures._mixins.attributes.py