Multi-Objects: UV_OT_follow_active_quads
authorDalai Felinto <dfelinto@gmail.com>
Wed, 5 Sep 2018 22:25:56 +0000 (19:25 -0300)
committerDalai Felinto <dfelinto@gmail.com>
Wed, 5 Sep 2018 22:27:44 +0000 (19:27 -0300)
Starring: the overengineered error report system to make sure only one error
is reported.

release/scripts/startup/bl_operators/uvcalc_follow_active.py

index 9de2aa4e76e6a61a5c8d8771aa10501aad80774a..38af5c69ef91218cfc16187649c6c9a3eebe6b57 100644 (file)
@@ -25,8 +25,12 @@ import bpy
 from bpy.types import Operator
 
 
-def extend(obj, operator, EXTEND_MODE):
+STATUS_OK =               (1 << 0)
+STATUS_ERR_ACTIVE_FACE =  (1 << 1)
+STATUS_ERR_NOT_SELECTED = (1 << 2)
+STATUS_ERR_NOT_QUAD =     (1 << 3)
 
+def extend(obj, operator, EXTEND_MODE):
     import bmesh
     me = obj.data
     # script will fail without UVs
@@ -39,14 +43,11 @@ def extend(obj, operator, EXTEND_MODE):
     uv_act = bm.loops.layers.uv.active
 
     if f_act is None:
-        operator.report({'ERROR'}, "No active face")
-        return
+        return STATUS_ERR_ACTIVE_FACE
     if not f_act.select:
-        operator.report({'ERROR'}, "No active face is not selected")
-        return
+        return STATUS_ERR_NOT_SELECTED
     elif len(f_act.verts) != 4:
-        operator.report({'ERROR'}, "Active face must be a quad")
-        return
+        return STATUS_ERR_NOT_QUAD
 
     faces = [f for f in bm.faces if f.select and len(f.verts) == 4]
 
@@ -212,12 +213,38 @@ def extend(obj, operator, EXTEND_MODE):
         apply_uv(*f_triple)
 
     bmesh.update_edit_mesh(me, False)
+    return STATUS_OK
 
 
 def main(context, operator):
-    obj = context.active_object
-
-    extend(obj, operator, operator.properties.mode)
+    num_meshes = 0
+    num_errors = 0
+    status = 0
+
+    ob_list = [ob for ob in context.selected_objects if ob and ob.type == 'MESH']
+    for ob in ob_list:
+        ob.data.tag = False
+
+    for ob in ob_list:
+        if ob.data.tag:
+            continue
+
+        num_meshes += 1
+        ob.data.tag = True
+
+        ret = extend(ob, operator, operator.properties.mode)
+        if ret != STATUS_OK:
+            num_errors += 1
+            status |= ret
+
+    if num_errors == num_meshes:
+        if status & STATUS_ERR_NOT_QUAD:
+            operator.report({'ERROR'}, "Active face must be a quad")
+        elif status & STATUS_ERR_NOT_SELECTED:
+            operator.report({'ERROR'}, "Active face not selected")
+        else:
+            assert((status & STATUS_ERR_ACTIVE_FACE) != 0)
+            operator.report({'ERROR'}, "No active face")
 
 
 class FollowActiveQuads(Operator):