[Bf-blender-cvs] [dd6fa94] master: Add 'Set From Faces' tool to custom split normals.

Bastien Montagne noreply at git.blender.org
Thu Nov 3 13:26:32 CET 2016


Commit: dd6fa94dcc9fa6002d4901f4afe0b1e997f5fa0c
Author: Bastien Montagne
Date:   Wed Nov 2 15:11:58 2016 +0100
Branches: master
https://developer.blender.org/rBdd6fa94dcc9fa6002d4901f4afe0b1e997f5fa0c

Add 'Set From Faces' tool to custom split normals.

Feature request during bconf, makes sense to have it even as an hack for
now, since this is probably one of the most common use cases. This should
be redone in bmesh once we have proper custom noramls handling in edit mode...

===================================================================

M	release/scripts/startup/bl_operators/mesh.py
M	release/scripts/startup/bl_ui/space_view3d_toolbar.py

===================================================================

diff --git a/release/scripts/startup/bl_operators/mesh.py b/release/scripts/startup/bl_operators/mesh.py
index be74f8d..58eab54 100644
--- a/release/scripts/startup/bl_operators/mesh.py
+++ b/release/scripts/startup/bl_operators/mesh.py
@@ -198,3 +198,53 @@ class MeshSelectPrev(Operator):
             bmesh.update_edit_mesh(me, False)
 
         return {'FINISHED'}
+
+
+# XXX This is hackish (going forth and back from Object mode...), to be redone once we have proper support of
+#     custom normals in BMesh/edit mode.
+class MehsSetNormalsFromFaces(Operator):
+    """Set the custom vertex normals from the selected faces ones"""
+    bl_idname = "mesh.set_normals_from_faces"
+    bl_label = "Set Normals From Faces"
+    bl_options = {'REGISTER', 'UNDO'}
+
+    @classmethod
+    def poll(cls, context):
+        return (context.mode == 'EDIT_MESH' and context.edit_object.data.polygons)
+
+    def execute(self, context):
+        import mathutils
+
+        bpy.ops.object.mode_set(mode='OBJECT')
+        obj = context.active_object
+        me = obj.data
+
+        v2nors = {}
+        for p in me.polygons:
+            if not p.select:
+                continue
+            for lidx, vidx in zip(p.loop_indices, p.vertices):
+                assert(me.loops[lidx].vertex_index == vidx)
+                v2nors.setdefault(vidx, []).append(p.normal)
+
+        for nors in v2nors.values():
+            nors[:] = [sum(nors, mathutils.Vector((0, 0, 0))).normalized()]
+
+        if not me.has_custom_normals:
+            me.create_normals_split()
+        me.calc_normals_split()
+
+        normals = []
+        for l in me.loops:
+            nor = v2nors.get(l.vertex_index, [None])[0]
+            if nor is None:
+                nor = l.normal
+            normals.append(nor.to_tuple())
+
+        me.normals_split_custom_set(normals)
+
+        me.free_normals_split()
+        bpy.ops.object.mode_set(mode='EDIT')
+
+        return {'FINISHED'}
+
diff --git a/release/scripts/startup/bl_ui/space_view3d_toolbar.py b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
index 8019c8d..f97e2d5 100644
--- a/release/scripts/startup/bl_ui/space_view3d_toolbar.py
+++ b/release/scripts/startup/bl_ui/space_view3d_toolbar.py
@@ -431,6 +431,7 @@ class VIEW3D_PT_tools_shading(View3DPanel, Panel):
         col.label(text="Normals:")
         col.operator("mesh.normals_make_consistent", text="Recalculate")
         col.operator("mesh.flip_normals", text="Flip Direction")
+        col.operator("mesh.set_normals_from_faces", text="Set From Faces")
 
 
 class VIEW3D_PT_tools_uvs(View3DPanel, Panel):




More information about the Bf-blender-cvs mailing list