Fixing a bug found while checking "[#31937] UV/Image Editor: Copy Mirrored UV Coords...
[blender.git] / release / scripts / startup / bl_operators / mesh.py
index 44d81ba53df5ac5eaa79c3a2f477525d5cf8589f..51645bfeeabeed6c1a60fc203d470bcf238312b2 100644 (file)
 #
 # ##### END GPL LICENSE BLOCK #####
 
-# <pep8 compliant>
+# <pep8-80 compliant>
 
 import bpy
+from bpy.types import Operator
 
 from bpy.props import EnumProperty
 
 
-class MeshSelectInteriorFaces(bpy.types.Operator):
-    '''Select faces where all edges have more then 2 face users.'''
-
-    bl_idname = "mesh.faces_select_interior"
-    bl_label = "Select Interior Faces"
-    bl_options = {'REGISTER', 'UNDO'}
-
-    @classmethod
-    def poll(cls, context):
-        ob = context.active_object
-        return (ob and ob.type == 'MESH')
-
-    def execute(self, context):
-        ob = context.active_object
-        context.tool_settings.mesh_select_mode = False, False, True
-        is_editmode = (ob.mode == 'EDIT')
-        if is_editmode:
-            bpy.ops.object.mode_set(mode='OBJECT', toggle=False)
-
-        mesh = ob.data
-
-        face_list = mesh.faces[:]
-        face_edge_keys = [face.edge_keys for face in face_list]
-
-        edge_face_count = mesh.edge_face_count_dict
-
-        def test_interior(index):
-            for key in face_edge_keys[index]:
-                if edge_face_count[key] < 3:
-                    return False
-            return True
-
-        for index, face in enumerate(face_list):
-            if(test_interior(index)):
-                face.select = True
-            else:
-                face.select = False
-
-        if is_editmode:
-            bpy.ops.object.mode_set(mode='EDIT', toggle=False)
-        return {'FINISHED'}
-
-
-class MeshMirrorUV(bpy.types.Operator):
+class MeshMirrorUV(Operator):
     '''Copy mirror UV coordinates on the X axis based on a mirrored mesh'''
     bl_idname = "mesh.faces_mirror_uv"
     bl_label = "Copy Mirrored UV coords"
     bl_options = {'REGISTER', 'UNDO'}
 
-    direction = EnumProperty(items=(
-                        ('POSITIVE', "Positive", ""),
-                        ('NEGATIVE', "Negative", "")),
-                name="Axis Direction",
-                description="")
+    direction = EnumProperty(
+            name="Axis Direction",
+            items=(('POSITIVE', "Positive", ""),
+                   ('NEGATIVE', "Negative", "")),
+            )
 
     @classmethod
     def poll(cls, context):
-        ob = context.active_object
-        return (ob and ob.type == 'MESH')
+        obj = context.active_object
+        return (obj and obj.type == 'MESH' and obj.data.uv_textures.active)
 
     def execute(self, context):
         DIR = (self.direction == 'NEGATIVE')
 
-        from mathutils import Vector
-
         ob = context.active_object
         is_editmode = (ob.mode == 'EDIT')
         if is_editmode:
@@ -99,80 +55,82 @@ class MeshMirrorUV(bpy.types.Operator):
         mirror_gt = {}
         mirror_lt = {}
 
-        vcos = [v.co.to_tuple(5) for v in mesh.vertices]
+        vcos = (v.co.to_tuple(5) for v in mesh.vertices)
 
         for i, co in enumerate(vcos):
-            if co[0] > 0.0:
-                mirror_gt[co] = i
-            elif co[0] < 0.0:
-                mirror_lt[co] = i
-            else:
+            if co[0] >= 0.0:
                 mirror_gt[co] = i
+            if co[0] <= 0.0:
                 mirror_lt[co] = i
 
         #for i, v in enumerate(mesh.vertices):
         vmap = {}
-        for mirror_a, mirror_b in (mirror_gt, mirror_lt), (mirror_lt, mirror_gt):
+        for mirror_a, mirror_b in ((mirror_gt, mirror_lt),
+                                   (mirror_lt, mirror_gt)):
             for co, i in mirror_a.items():
                 nco = (-co[0], co[1], co[2])
                 j = mirror_b.get(nco)
                 if j is not None:
                     vmap[i] = j
 
-        active_uv_layer = None
-        for lay in mesh.uv_textures:
-            if lay.active:
-                active_uv_layer = lay.data
-                break
-
-        fuvs = [(uv.uv1, uv.uv2, uv.uv3, uv.uv4) for uv in active_uv_layer]
-        fuvs_cpy = [(uv[0].copy(), uv[1].copy(), uv[2].copy(), uv[3].copy()) for uv in fuvs]
-
-        # as a list
-        faces = mesh.faces[:]
-
-        fuvsel = [(False not in uv.select_uv) for uv in active_uv_layer]
-        fcents = [f.center for f in faces]
-
-        # find mirror faces
-        mirror_fm = {}
-        for i, f in enumerate(faces):
-            verts = list(f.vertices)
-            verts.sort()
-            verts = tuple(verts)
-            mirror_fm[verts] = i
-
-        fmap = {}
-        for i, f in enumerate(faces):
-            verts = [vmap.get(j) for j in f.vertices]
-            if None not in verts:
-                verts.sort()
-                j = mirror_fm.get(tuple(verts))
+        polys = mesh.polygons
+        loops = mesh.loops
+        verts = mesh.vertices
+        uv_loops = mesh.uv_layers.active.data
+        nbr_polys = len(polys)
+
+        mirror_pm = {}
+        pmap = {}
+        puvs = [None] * nbr_polys
+        puvs_cpy = [None] * nbr_polys
+        puvsel = [None] * nbr_polys
+        pcents = [None] * nbr_polys
+        vidxs = [None] * nbr_polys
+        for i, p in enumerate(polys):
+            lstart = lend = p.loop_start
+            lend += p.loop_total
+            puvs[i] = tuple(uv.uv for uv in uv_loops[lstart:lend])
+            puvs_cpy[i] = tuple(uv.copy() for uv in puvs[i])
+            puvsel[i] = (False not in
+                               (uv.select for uv in uv_loops[lstart:lend]))
+            # Vert idx of the poly.
+            vidxs[i] = tuple(l.vertex_index for l in loops[lstart:lend])
+            # As we have no poly.center yet...
+            pcents[i] = tuple(map(lambda x: x / p.loop_total,
+                                  map(sum, zip(*(verts[idx].co
+                                                 for idx in vidxs[i])))))
+            # Preparing next step finding matching polys.
+            mirror_pm[tuple(sorted(vidxs[i]))] = i
+
+        for i in range(nbr_polys):
+            # Find matching mirror poly.
+            tvidxs = [vmap.get(j) for j in vidxs[i]]
+            if None not in tvidxs:
+                tvidxs.sort()
+                j = mirror_pm.get(tuple(tvidxs))
                 if j is not None:
-                    fmap[i] = j
-
-        done = [False] * len(faces)
-        for i, j in fmap.items():
+                    pmap[i] = j
 
-            if not fuvsel[i] or not fuvsel[j]:
+        for i, j in pmap.items():
+            if not puvsel[i] or not puvsel[j]:
                 continue
-            elif DIR == 0 and fcents[i][0] < 0.0:
+            elif DIR == 0 and pcents[i][0] < 0.0:
                 continue
-            elif DIR == 1 and fcents[i][0] > 0.0:
+            elif DIR == 1 and pcents[i][0] > 0.0:
                 continue
 
             # copy UVs
-            uv1 = fuvs[i]
-            uv2 = fuvs_cpy[j]
+            uv1 = puvs[i]
+            uv2 = puvs_cpy[j]
 
             # get the correct rotation
-            v1 = faces[j].vertices[:]
-            v2 = [vmap[k] for k in faces[i].vertices[:]]
+            v1 = vidxs[j]
+            v2 = tuple(vmap[k] for k in vidxs[i])
 
-            for k in range(len(uv1)):
-                k_map = v1.index(v2[k])
-                uv1[k].x = - (uv2[k_map].x - 0.5) + 0.5
-                uv1[k].y = uv2[k_map].y
+            if len(v1) == len(v2):
+                for k in range(len(v1)):
+                    k_map = v1.index(v2[k])
+                    uv1[k].xy = - (uv2[k_map].x - 0.5) + 0.5, uv2[k_map].y
 
         if is_editmode:
             bpy.ops.object.mode_set(mode='EDIT', toggle=False)