Working with Mean Bevel Weight using the Blender Python API

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.

Using depsgraph we get an evaluated copy of the mesh data.

Now we can access its attributes, in particular the bevel_weight_edge attribute we need.

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.

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’

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.

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.

This way, we have set the required Bevel Weight for the selected mesh edges.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments