Hi everyone,
I’m trying to modify the vertex normal of a face selection of a mesh. This operator is trying to calculate the average the face normal of the current face selection, and set teh normal of each vertex normal to ths summed vector.
The goal is to flatten the normal of it.
The issue is that I’m not able to set the normal correctly. Maybe someone coud help
Maybe Bmesh is not the best way to acheve it.
class TILA_OT_normalflatten(bpy.types.Operator):
bl_idname = "mesh.normalflatten"
bl_label = "Flatten Normals"
bl_options = {'REGISTER', 'UNDO'}
def flatten(self, context, object):
me = object.data
bm = bmesh.from_edit_mesh(me)
if bpy.context.scene.tool_settings.mesh_select_mode[0]:
bpy.ops.mesh.select_mode(type="FACE")
if bpy.context.scene.tool_settings.mesh_select_mode[1]:
bpy.ops.mesh.select_mode(type="FACE")
if bpy.context.scene.tool_settings.mesh_select_mode[2]:
selected = []
# Get selected faces
for f in bm.faces:
if f.select:
selected.append(f)
# sum all selected normals
sum = Vector((1, 1, 1))
for f in selected:
sum = sum + f.normal
length = len(selected)
sum = (sum.x / length, sum.y / length, sum.z / length)
sum = Vector(sum)
sum.normalize()
for f in selected:
for v in f.verts:
v.normal = sum
v.normal_update()
print(v.normal)
bm.normal_update()
print(dir(bm))
bm.to_mesh(me)
bmesh.update_edit_mesh(me)
bm.free()
def execute(self, context):
active = bpy.context.active_object
if active:
selection = bpy.context.selected_objects
if active not in selection:
selection.append(active)
for o in selection:
context.view_layer.objects.active = o
self.flatten(context, o)
return {'FINISHED'}
Thanks in advance !