diff options
author | Florian Meyer <florianfelix@web.de> | 2010-05-16 22:01:11 +0400 |
---|---|---|
committer | Florian Meyer <florianfelix@web.de> | 2010-05-16 22:01:11 +0400 |
commit | ef71d65d3c9f54b29b4befec0a534866c8cc3cb5 (patch) | |
tree | 8dcf7c1a58ba9b2e7597c1457597675109f520f7 | |
parent | d61abf20a2960bfa3099b3fc7f1699d0e3f82af7 (diff) |
align_matrix for the rest add mesh scripts
all done now
-rw-r--r-- | add_mesh_star.py | 37 | ||||
-rw-r--r-- | add_mesh_twisted_torus.py | 34 | ||||
-rw-r--r-- | add_mesh_wedge.py | 34 |
3 files changed, 59 insertions, 46 deletions
diff --git a/add_mesh_star.py b/add_mesh_star.py index ac83ae4d..9c52b54c 100644 --- a/add_mesh_star.py +++ b/add_mesh_star.py @@ -34,7 +34,7 @@ bl_addon_info = { 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 @@ def store_recall_properties(ob, op, op_args): 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 @@ def apply_object_align(context, ob): # 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,9 +132,8 @@ def create_mesh_object(context, verts, edges, faces, name, edit): ob_new.selected = True # Place the object at the 3D cursor location. - ob_new.location = scene.cursor_location - - apply_object_align(context, ob_new) + # apply viewRotaion + ob_new.matrix = align_matrix if obj_act and obj_act.mode == 'EDIT': if not edit: @@ -315,6 +314,7 @@ class AddStar(bpy.types.Operator): min=0.01, max=9999.0, default=0.5) + align_matrix = Matrix() def execute(self, context): props = self.properties @@ -326,7 +326,7 @@ class AddStar(bpy.types.Operator): 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 @@ class AddStar(bpy.types.Operator): 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, diff --git a/add_mesh_twisted_torus.py b/add_mesh_twisted_torus.py index cc8be0a3..4a538ea0 100644 --- a/add_mesh_twisted_torus.py +++ b/add_mesh_twisted_torus.py @@ -80,18 +80,18 @@ def store_recall_properties(ob, op, op_args): 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 @@ def apply_object_align(context, ob): # 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,9 +153,8 @@ def create_mesh_object(context, verts, edges, faces, name, edit): ob_new.selected = True # Place the object at the 3D cursor location. - ob_new.location = scene.cursor_location - - apply_object_align(context, ob_new) + # apply viewRotaion + ob_new.matrix = align_matrix if obj_act and obj_act.mode == 'EDIT': if not edit: @@ -349,6 +348,7 @@ class AddTwistedTorus(bpy.types.Operator): min=0.01, max=100.0, default=0.5) + align_matrix = Matrix() def execute(self, context): props = self.properties @@ -367,7 +367,7 @@ class AddTwistedTorus(bpy.types.Operator): # 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 @@ class AddTwistedTorus(bpy.types.Operator): 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, diff --git a/add_mesh_wedge.py b/add_mesh_wedge.py index 621f9d81..e251ce4b 100644 --- a/add_mesh_wedge.py +++ b/add_mesh_wedge.py @@ -76,18 +76,18 @@ def store_recall_properties(ob, op, op_args): 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 @@ def apply_object_align(context, ob): # 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,9 +149,8 @@ def create_mesh_object(context, verts, edges, faces, name, edit): ob_new.selected = True # Place the object at the 3D cursor location. - ob_new.location = scene.cursor_location - - apply_object_align(context, ob_new) + # apply viewRotaion + ob_new.matrix = align_matrix if obj_act and obj_act.mode == 'EDIT': if not edit: @@ -310,6 +309,7 @@ class AddWedge(bpy.types.Operator): min=0.01, max=9999.0, default=2.00) + align_matrix = Matrix() def execute(self, context): props = self.properties @@ -320,7 +320,7 @@ class AddWedge(bpy.types.Operator): 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 @@ class AddWedge(bpy.types.Operator): 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, |