To interact with the Bevel modifier, which creates a bevel on specified edges, each of the required edges must be given a specific numeric weight parameter ranging from 0.0 to 1.0. In manual mode, the weight for the bevel is set in the N-panel on the Item tab in Edges Data – Mean Bevel Weight field. This weight can also be read and set via the Blender Python API.
For example, let’s add “Suzanne” mesh to the scene, switch to the edit mode, select several edges and set them some Mean Bevel Weight value.
The values for these edge weights are stored in a mesh “bevel_weight_edge” attribute.
The moment we set weights through the UI panel, Blender automatically creates this attribute and sets the values we specify into it.
To read the given values of weights from the edges, we must first “evaluate” the mesh – using a depsgraph, obtain the final copy of the mesh data having applied all modifiers, geometry nodes and all other dynamical changes.
First, switch the mesh to object mode.
1 2 |
obj = bpy.data.objects['Suzanne'] bpy.ops.object.mode_set(mode='OBJECT') |
Using depsgraph we get an evaluated copy of the mesh data.
1 |
obj_data = obj.evaluated_get(bpy.context.evaluated_depsgraph_get()).data |
Now we can access its attributes, in particular the bevel_weight_edge attribute we need.
1 2 3 |
attr = obj_data.attributes['bevel_weight_edge'] # <bpy_struct, FloatAttribute("bevel_weight_edge") at 0x000001F56976F408> |
Let’s loop through all the edges of the evaluated mesh and print those for which the bevel_weight_edge attribute is set to values greater than 0.0.
1 2 3 4 5 6 7 |
for _index, edge in enumerate(obj_data.edges): if attr.data[_index].value > 0.0: print(edge, _index, attr.data[_index].value) # <bpy_struct, MeshEdge at 0x000001F5695CAD68> 420 1.0 # <bpy_struct, MeshEdge at 0x000001F5695CAD70> 421 1.0 # ... |
These are the edges weighted with Mean Bevel Weight values. We also printed the attribute values themselves.
Through the Python API, we can not only read, but also set bevel weights to the desired edges.
Add another “Suzanne” mesh to the scene and select some edges on it.
If we now try to get the values of the bevel_weight_edge attribute, Blender will throw an error about the absence of this attribute on this mesh.
KeyError: ‘bpy_prop_collection[key]: key “bevel_weight_edge” not found’
1 2 3 4 5 6 |
obj1 = bpy.data.objects['Suzanne.001'] obj1_data = obj1.evaluated_get(bpy.context.evaluated_depsgraph_get()).data attr = obj1_data.attributes['bevel_weight_edge'] # KeyError: 'bpy_prop_collection[key]: key "bevel_weight_edge" not found' |
This happens because we did not assign weights to the edges through the UI and, accordingly, Blender did not create this attribute.
Let’s create it ourselves.
1 2 3 4 5 6 |
if 'bevel_weight_edge' not in obj1_data.attributes: obj1.data.attributes.new( name='bevel_weight_edge', type='FLOAT', domain='EDGE' ) |
Pay attention that although we are checking for the existing of an attribute on the evaluated mesh, we are creating the attribute on the original mesh.
In the function parameters we pass the name of the attribute, its type FLOAT – floating point numbers, and domain – for which area of the mesh the attribute is created, in our case – for the edges.
Now we can loop through all the edges of the mesh in the same way and, if an edge is selected, assign a bevel weight to this edge.
1 2 3 |
for _index, edge in enumerate(obj1.data.edges): if edge.select: obj1.data.attributes['bevel_weight_edge'].data[_index].value = 1.0 |
This way, we have set the required Bevel Weight for the selected mesh edges.