In Blender when assigning custom properties to vertices using the bmesh layer system, this data can then be accessed directly from the object, through its attributes.
As an example, let’s assign a custom property of the integer type to the vertices of the currently active object.
Load the active mesh data into bmesh. And use bmesh to create an int-type layer for the vertices. Set each vertex on this layer to a random value of 0 or 1 and dump the data back to the object.
1 2 3 4 5 6 |
obj_data = bpy.context.object.data bm = bmesh.from_edit_mesh(obj_data) vert_layer = bm.verts.layers.int.new('vert_layer') for v in bm.verts: v[vert_layer] = randint(0, 1) bmesh.update_edit_mesh(obj_data) |
If we look at the object’s attribute list, we will see a new attribute named “vert_layer”. This is exactly the name we used when creating the bmesh layer.
We can get custom vertices values set through the bmesh layer using this attribute.
1 2 3 |
[attr.value for attr in bpy.context.object.data.attributes['vert_layer'].data] # [0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, ... 1, 0] |
Let’s select vertices for which our custom property is set to 1.
1 2 |
values = [attr.value for attr in bpy.context.object.data.attributes['vert_layer'].data] bpy.context.object.data.vertices.foreach_set('select', values) |
We will get the following result: