compas-dev / compas-dev/compas

Mesh.vertex_attribute doesn't work ass expected

Open
#715 10 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
386
Forks
122
Avg merge
11d 46m
Merged PRs (30d)
1

Description

**Describe the bug**
I might be tired, or stupid... While looping through all vertices of a mesh and updating a vertex attribute of one vertex, all the vertices in the loop also get that value.

**To Reproduce**
Run the following code:
```
from compas.geometry import Box
from compas.datastructures import Mesh

# make a simple compas mesh
mesh = Mesh.from_shape(Box.from_width_height_depth(2, 2, 2))

# add a new vertex attribute to all vertices
mesh.update_default_vertex_attributes({"new_attr": []})

# add a value to this new attribute only to some vertices
for index, v_attributes in enumerate(mesh.vertices(data=True)):
print("------", index)
v_key, v_data = v_attributes
attribute = mesh.vertex_attribute(v_key, 'new_attr')
print('before', attribute)
if index % 2 == 0:
attribute.append('why_{}'.format(index))
print('after', mesh.vertex_attribute(v_key, 'new_attr'))
```

The output I get is:
```
------ 0
before []
after ['why_0']
------ 1
before ['why_0']
after ['why_0']
------ 2
before ['why_0']
after ['why_0', 'why_2']
------ 3
before ['why_0', 'why_2']
after ['why_0', 'why_2']
------ 4
before ['why_0', 'why_2']
after ['why_0', 'why_2', 'why_4']
------ 5
before ['why_0', 'why_2', 'why_4']
after ['why_0', 'why_2', 'why_4']
------ 6
before ['why_0', 'why_2', 'why_4']
after ['why_0', 'why_2', 'why_4', 'why_6']
------ 7
before ['why_0', 'why_2', 'why_4', 'why_6']
after ['why_0', 'why_2', 'why_4', 'why_6']
```

**Expected behavior**
I'm expecting to change the attribute only of the desired vertex/vertices. An output like the following:
```
------ 0
before []
after ['why_0']
------ 1
before []
after []
------ 2
before []
after ['why_2']
------ 3
before []
after []
------ 4
before []
after ['why_4']
------ 5
before []
after []
------ 6
before []
after ['why_6']
------ 7
before []
after []
```

**Desktop (please complete the following information):**
- OS: macOS Catalina
- Python version: 3.7.6
- Python package manager: Conda
- Compas: 0.19.1

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.