[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [38511] trunk/blender/release/scripts/ startup/bl_operators/object_align.py: Object Align operator: coudn' t resist and added a high quality (slower) option to get perfect alighment on complex shapes with rotation/scaling :D

Daniel Salazar zanqdo at gmail.com
Tue Jul 19 17:07:30 CEST 2011


Revision: 38511
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=38511
Author:   zanqdo
Date:     2011-07-19 15:07:29 +0000 (Tue, 19 Jul 2011)
Log Message:
-----------
Object Align operator: coudn't resist and added a high quality (slower) option to get perfect alighment on complex shapes with rotation/scaling :D

sexy example:
http://www.pasteall.org/pic/show.php?id=15171

Modified Paths:
--------------
    trunk/blender/release/scripts/startup/bl_operators/object_align.py

Modified: trunk/blender/release/scripts/startup/bl_operators/object_align.py
===================================================================
--- trunk/blender/release/scripts/startup/bl_operators/object_align.py	2011-07-19 13:27:05 UTC (rev 38510)
+++ trunk/blender/release/scripts/startup/bl_operators/object_align.py	2011-07-19 15:07:29 UTC (rev 38511)
@@ -21,7 +21,8 @@
 import bpy
 from mathutils import Vector
 
-def GlobalBB(bb_world):
+def GlobalBB_LQ(bb_world):
+    
     # Initialize the variables with the 8th vertex
     left, right, front, back, down, up =\
     bb_world[7][0],\
@@ -57,11 +58,58 @@
             
         if val > up:
             up = val
+        
+    return (Vector((left, front, up)), Vector((right, back, down)))
+
+def GlobalBB_HQ(obj):
+    
+    # Initialize the variables with the last vertex
+    
+    verts = obj.data.vertices
+    
+    val = verts[-1].co * obj.matrix_world
+    
+    left, right, front, back, down, up =\
+    val[0],\
+    val[0],\
+    val[1],\
+    val[1],\
+    val[2],\
+    val[2]
+    
+    # Test against all other verts
+    for i in range (len(verts)-1):
+        
+        vco = verts[i].co * obj.matrix_world
+        
+        # X Range
+        val = vco[0]
+        if val < left:
+            left = val
             
+        if val > right:
+            right = val
+            
+        # Y Range
+        val = vco[1]
+        if val < front:
+            front = val
+            
+        if val > back:
+            back = val
+            
+        # Z Range
+        val = vco[2]
+        if val < down:
+            down = val
+            
+        if val > up:
+            up = val
+        
     return (Vector((left, front, up)), Vector((right, back, down)))
 
 
-def align_objects(align_x, align_y, align_z, align_mode, relative_to):
+def align_objects(align_x, align_y, align_z, align_mode, relative_to, bb_quality):
 
     cursor = bpy.context.scene.cursor_location
 
@@ -82,7 +130,11 @@
 
     for obj, bb_world in objs:
         
-        GBB = GlobalBB(bb_world)
+        if bb_quality:
+            GBB = GlobalBB_HQ(obj)
+        else:
+            GBB = GlobalBB_LQ(bb_world)
+            
         Left_Front_Up = GBB[0]
         Right_Back_Down = GBB[1]
 
@@ -141,7 +193,11 @@
     for obj, bb_world in objs:
         bb_world = [Vector(v[:]) * obj.matrix_world for v in obj.bound_box]
         
-        GBB = GlobalBB(bb_world)
+        if bb_quality:
+            GBB = GlobalBB_HQ(obj)
+        else:
+            GBB = GlobalBB_LQ(bb_world)
+            
         Left_Front_Up = GBB[0]
         Right_Back_Down = GBB[1]
 
@@ -270,7 +326,7 @@
     return True
 
 
-from bpy.props import EnumProperty
+from bpy.props import EnumProperty, BoolProperty
 
 
 class AlignObjects(bpy.types.Operator):
@@ -279,6 +335,11 @@
     bl_label = "Align Objects"
     bl_options = {'REGISTER', 'UNDO'}
 
+    bb_quality = BoolProperty(
+            name="High Quality",
+            description="Enables high quality calculation of the bounding box for perfect results on complex shape meshes with rotation/scale (Slow)",
+            default=False)
+
     align_mode = EnumProperty(items=(
             ('OPT_1', "Negative Sides", ""),
             ('OPT_2', "Centers", ""),
@@ -311,10 +372,10 @@
 
     def execute(self, context):
         align_axis = self.align_axis
-        ret = align_objects('X' in align_axis, 'Y' in align_axis, 'Z' in align_axis, self.align_mode, self.relative_to)
+        ret = align_objects('X' in align_axis, 'Y' in align_axis, 'Z' in align_axis, self.align_mode, self.relative_to, self.bb_quality)
 
         if not ret:
             self.report({'WARNING'}, "No objects with bound-box selected")
             return {'CANCELLED'}
         else:
-            return {'FINISHED'}
+            return {'FINISHED'}
\ No newline at end of file




More information about the Bf-blender-cvs mailing list