Skip to content
Snippets Groups Projects
Commit ef71d65d authored by Florian Meyer's avatar Florian Meyer
Browse files

align_matrix for the rest add mesh scripts

all done now
parent d61abf20
No related branches found
No related tags found
No related merge requests found
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment