The “face_strength” parameter is used in the Weighted Normal modifier to calculate normals. We can set and change its values by calling a special operator, or directly getting and setting its values through the Blender Python API.
Working with face_strength property through an operator
For managing the “force_strength” parameter, the Blender Python API implements a single operator:
1 |
bpy.ops.mesh.mod_weighted_strength(set=False, face_strength='MEDIUM') |
The operator works in the mesh “EDIT” mode.
To set the required strength value of ‘WEAK’, ‘MEDIUM’ or ‘STRONG’ to desired faces, we need to select the required polygons and call the operator with the “set = True” option.
To select faces with the required strength value, we need to call the same operator, but with the “set = False” parameter.
Note that the mod_weighted_strength operator is context sensitive. It is intended for the 3D viewport context, and if we call it from another area, we need to override the context.
Working with face_strength directly
If we explore the structure of the mesh with the API, we will not find a property in it to store the face_strength values. This is because these values are stored to the mesh’s “__mod_weightednormals_faceweight” attribute.
The attribute values can be viewed in the SpreadSheet area in the “Face” domain.
The correspondence of numerical values to the text designation is as follows:
- WEAK = -16348
- MEDIUM = 0
- STRONG = 16348
Attribute values can be accessed through the Python API in the mesh “OBJECT” mode.
To get the face_strength values for each polygon, we refer to them by the attribute name:
1 2 3 |
[attr.value for attr in bpy.context.object.data.attributes['__mod_weightednormals_faceweight'].data] # [16384, 16384, 16384, 16384, -16384, -16384] |
The order of the values in the list corresponds to the mesh’s polygons indices.
We can also set the desired strength value for the required face through the attribute.
For example, let’s set the strength value for the polygon with index 0 to “WEAK”:
1 |
bpy.context.object.data.attributes['__mod_weightednormals_faceweight'].data[0].value = -16384 |
Pay attention that if we did not previously call the mod_weighted_strength operator to set the strength of the polygons, the attribute will not be created initially and Blender will throw the following error:
KeyError: ‘bpy_prop_collection[key]: key “__mod_weightednormals_faceweight” not found’
To set face_strength values through the API without previously calling the mod_weighted_strength operator, we need to create the attribute ourselves:
1 2 3 |
bpy.context.object.data.attributes.new(name='__mod_weightednormals_faceweight', type='INT', domain='FACE') # bpy.data.meshes['Cube'].attributes['__mod_weightednormals_faceweight'] |
After that, we can set the face_strength values in the same way as after calling the operator:
1 |
bpy.context.object.data.attributes['__mod_weightednormals_faceweight'].data[0].value = -16384 |