[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [672] trunk/py/scripts/addons: align_matrix for the rest add mesh scripts

Florian Meyer florianfelix at web.de
Sun May 16 20:01:11 CEST 2010


Revision: 672
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=672
Author:   testscreenings
Date:     2010-05-16 20:01:11 +0200 (Sun, 16 May 2010)

Log Message:
-----------
align_matrix for the rest add mesh scripts

all done now

Modified Paths:
--------------
    trunk/py/scripts/addons/add_mesh_star.py
    trunk/py/scripts/addons/add_mesh_twisted_torus.py
    trunk/py/scripts/addons/add_mesh_wedge.py

Modified: trunk/py/scripts/addons/add_mesh_star.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_star.py	2010-05-16 16:04:45 UTC (rev 671)
+++ trunk/py/scripts/addons/add_mesh_star.py	2010-05-16 18:01:11 UTC (rev 672)
@@ -34,7 +34,7 @@
 
 import bpy
 import mathutils
-from mathutils import Vector, Quaternion
+from mathutils import Vector, Quaternion, TranslationMatrix, Matrix
 from math import pi
 from bpy.props import IntProperty, FloatProperty, BoolProperty
 
@@ -59,18 +59,18 @@
         ob['recall'] = recall_properties
 
 
-# Apply view rotation to objects if "Align To" for
-# new objects was set to "VIEW" in the User Preference.
-def apply_object_align(context, ob):
-    obj_align = bpy.context.user_preferences.edit.object_align
-
+# calculates the matrix for the new object
+# depending on user pref
+def align_matrix(context):
+    loc = TranslationMatrix(context.scene.cursor_location)
+    obj_align = context.user_preferences.edit.object_align
     if (context.space_data.type == 'VIEW_3D'
         and obj_align == 'VIEW'):
-            view3d = context.space_data
-            region = view3d.region_3d
-            viewMatrix = region.view_matrix
-            rot = viewMatrix.rotation_part()
-            ob.rotation_euler = rot.invert().to_euler()
+        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
+    else:
+        rot = Matrix()
+    align_matrix = loc * rot
+    return align_matrix
 
 
 # Create a new mesh (object) from verts/edges/faces.
@@ -79,7 +79,7 @@
 # name ... Name of the new mesh (& object).
 # edit ... Replace existing mesh data.
 # Note: Using "edit" will destroy/delete existing mesh data.
-def create_mesh_object(context, verts, edges, faces, name, edit):
+def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -132,10 +132,9 @@
         ob_new.selected = True
 
         # Place the object at the 3D cursor location.
-        ob_new.location = scene.cursor_location
+        # apply viewRotaion
+        ob_new.matrix = align_matrix
 
-        apply_object_align(context, ob_new)
-
     if obj_act and obj_act.mode == 'EDIT':
         if not edit:
             # We are in EditMode, switch to ObjectMode.
@@ -315,6 +314,7 @@
         min=0.01,
         max=9999.0,
         default=0.5)
+    align_matrix = Matrix()
 
     def execute(self, context):
         props = self.properties
@@ -326,7 +326,7 @@
             props.height)
 
         obj = create_mesh_object(context, verts, [], faces, "Star",
-            props.edit)
+            props.edit, self.align_matrix)
 
         # Store 'recall' properties in the object.
         recall_args_list = {
@@ -339,6 +339,11 @@
 
         return {'FINISHED'}
 
+    def invoke(self, context, event):
+        self.align_matrix = align_matrix(context)
+        self.execute(context)
+        return {'FINISHED'}
+
 # Register the operator
 
 menu_func = (lambda self, context: self.layout.operator(AddStar.bl_idname,

Modified: trunk/py/scripts/addons/add_mesh_twisted_torus.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_twisted_torus.py	2010-05-16 16:04:45 UTC (rev 671)
+++ trunk/py/scripts/addons/add_mesh_twisted_torus.py	2010-05-16 18:01:11 UTC (rev 672)
@@ -80,18 +80,18 @@
         ob['recall'] = recall_properties
 
 
-# Apply view rotation to objects if "Align To" for
-# new objects was set to "VIEW" in the User Preference.
-def apply_object_align(context, ob):
-    obj_align = bpy.context.user_preferences.edit.object_align
-
+# calculates the matrix for the new object
+# depending on user pref
+def align_matrix(context):
+    loc = TranslationMatrix(context.scene.cursor_location)
+    obj_align = context.user_preferences.edit.object_align
     if (context.space_data.type == 'VIEW_3D'
         and obj_align == 'VIEW'):
-            view3d = context.space_data
-            region = view3d.region_3d
-            viewMatrix = region.view_matrix
-            rot = viewMatrix.rotation_part()
-            ob.rotation_euler = rot.invert().to_euler()
+        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
+    else:
+        rot = Matrix()
+    align_matrix = loc * rot
+    return align_matrix
 
 
 # Create a new mesh (object) from verts/edges/faces.
@@ -100,7 +100,7 @@
 # name ... Name of the new mesh (& object).
 # edit ... Replace existing mesh data.
 # Note: Using "edit" will destroy/delete existing mesh data.
-def create_mesh_object(context, verts, edges, faces, name, edit):
+def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -153,10 +153,9 @@
         ob_new.selected = True
 
         # Place the object at the 3D cursor location.
-        ob_new.location = scene.cursor_location
+        # apply viewRotaion
+        ob_new.matrix = align_matrix
 
-        apply_object_align(context, ob_new)
-
     if obj_act and obj_act.mode == 'EDIT':
         if not edit:
             # We are in EditMode, switch to ObjectMode.
@@ -349,6 +348,7 @@
         min=0.01,
         max=100.0,
         default=0.5)
+    align_matrix = Matrix()
 
     def execute(self, context):
         props = self.properties
@@ -367,7 +367,7 @@
 
         # Actually create the mesh object from this geometry data.
         obj = create_mesh_object(context, verts, [], faces, "TwistedTorus",
-            props.edit)
+            props.edit, self.align_matrix)
 
         # Store 'recall' properties in the object.
         recall_args_list = {
@@ -384,6 +384,10 @@
 
         return {'FINISHED'}
 
+    def invoke(self, context, event):
+        self.align_matrix = align_matrix(context)
+        self.execute(context)
+        return {'FINISHED'}
 
 # Add to the menu
 menu_func = (lambda self,

Modified: trunk/py/scripts/addons/add_mesh_wedge.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_wedge.py	2010-05-16 16:04:45 UTC (rev 671)
+++ trunk/py/scripts/addons/add_mesh_wedge.py	2010-05-16 18:01:11 UTC (rev 672)
@@ -76,18 +76,18 @@
         ob['recall'] = recall_properties
 
 
-# Apply view rotation to objects if "Align To" for
-# new objects was set to "VIEW" in the User Preference.
-def apply_object_align(context, ob):
-    obj_align = bpy.context.user_preferences.edit.object_align
-
+# calculates the matrix for the new object
+# depending on user pref
+def align_matrix(context):
+    loc = TranslationMatrix(context.scene.cursor_location)
+    obj_align = context.user_preferences.edit.object_align
     if (context.space_data.type == 'VIEW_3D'
         and obj_align == 'VIEW'):
-            view3d = context.space_data
-            region = view3d.region_3d
-            viewMatrix = region.view_matrix
-            rot = viewMatrix.rotation_part()
-            ob.rotation_euler = rot.invert().to_euler()
+        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
+    else:
+        rot = Matrix()
+    align_matrix = loc * rot
+    return align_matrix
 
 
 # Create a new mesh (object) from verts/edges/faces.
@@ -96,7 +96,7 @@
 # name ... Name of the new mesh (& object).
 # edit ... Replace existing mesh data.
 # Note: Using "edit" will destroy/delete existing mesh data.
-def create_mesh_object(context, verts, edges, faces, name, edit):
+def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -149,10 +149,9 @@
         ob_new.selected = True
 
         # Place the object at the 3D cursor location.
-        ob_new.location = scene.cursor_location
+        # apply viewRotaion
+        ob_new.matrix = align_matrix
 
-        apply_object_align(context, ob_new)
-
     if obj_act and obj_act.mode == 'EDIT':
         if not edit:
             # We are in EditMode, switch to ObjectMode.
@@ -310,6 +309,7 @@
         min=0.01,
         max=9999.0,
         default=2.00)
+    align_matrix = Matrix()
 
     def execute(self, context):
         props = self.properties
@@ -320,7 +320,7 @@
             props.size_z)
 
         obj = create_mesh_object(context, verts, [], faces, "Wedge",
-            props.edit)
+            props.edit, self.align_matrix)
 
         # Store 'recall' properties in the object.
         recall_args_list = {
@@ -332,6 +332,10 @@
 
         return {'FINISHED'}
 
+    def invoke(self, context, event):
+        self.align_matrix = align_matrix(context)
+        self.execute(context)
+        return {'FINISHED'}
 
 # Register the operator
 menu_func = (lambda self, context: self.layout.operator(AddWedge.bl_idname,




More information about the Bf-extensions-cvs mailing list