From 10ad28fcf0e011fae094d8c20362ee7f28fc4df6 Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Wed, 26 Jan 2011 15:14:04 +0000
Subject: [PATCH] remove boiler plate add-object code and use python module as
 add_torus uses. there are some known issues with this but much easier to fix
 bugs in the one place.

---
 add_mesh_ant_landscape.py | 64 ++---------------------------
 add_mesh_extras.py        | 86 ++++-----------------------------------
 add_mesh_gears.py         | 73 ++++-----------------------------
 add_mesh_gemstones.py     | 70 +++----------------------------
 add_mesh_solid.py         | 55 +------------------------
 add_mesh_twisted_torus.py | 63 ++--------------------------
 6 files changed, 30 insertions(+), 381 deletions(-)

diff --git a/add_mesh_ant_landscape.py b/add_mesh_ant_landscape.py
index 896f7fe27..977e52135 100644
--- a/add_mesh_ant_landscape.py
+++ b/add_mesh_ant_landscape.py
@@ -77,24 +77,11 @@ from noise import *
 from math import *
 
 
-###------------------------------------------------------------
-# calculates the matrix for the new object depending on user pref
-def align_matrix(context):
-    loc = Matrix.Translation(context.scene.cursor_location)
-    obj_align = context.user_preferences.edit.object_align
-    if (context.space_data.type == 'VIEW_3D'
-        and obj_align == 'VIEW'):
-        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.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                    new mesh (as used in from_pydata).
 # name ... Name of the new mesh (& object).
-def create_mesh_object(context, verts, edges, faces, name, align_matrix):
+def create_mesh_object(context, verts, edges, faces, name):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -107,44 +94,8 @@ def create_mesh_object(context, verts, edges, faces, name, align_matrix):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    # apply viewRotaion
-    ob_new.matrix_world = align_matrix
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 # A very simple "bridge" tool.
 # Connects two equally long vertex rows with faces.
@@ -489,9 +440,6 @@ class landscape_add(bpy.types.Operator):
     bl_options = {'REGISTER', 'UNDO'}
     bl_description = "Add landscape mesh"
 
-    # align_matrix for the invoke
-    align_matrix = Matrix()
-
     # properties
     AutoUpdate = BoolProperty(name="Mesh update",
                 default=True,
@@ -829,7 +777,7 @@ class landscape_add(bpy.types.Operator):
                 verts, faces = grid_gen( self.Subdivision, self.MeshSize, options )
 
             # create mesh object
-            obj = create_mesh_object(context, verts, [], faces, "Landscape", self.align_matrix)
+            obj = create_mesh_object(context, verts, [], faces, "Landscape")
 
             # sphere, remove doubles
             if self.SphereMesh !=0:
@@ -848,10 +796,6 @@ class landscape_add(bpy.types.Operator):
         else:
             return {'PASS_THROUGH'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 ###------------------------------------------------------------
 # Register
diff --git a/add_mesh_extras.py b/add_mesh_extras.py
index 1c7639323..423677f42 100644
--- a/add_mesh_extras.py
+++ b/add_mesh_extras.py
@@ -36,24 +36,11 @@ from mathutils import *
 from math import *
 from bpy.props import *
 
-# calculates the matrix for the new object
-# depending on user pref
-def align_matrix(context):
-    loc = Matrix.Translation(context.scene.cursor_location)
-    obj_align = context.user_preferences.edit.object_align
-    if (context.space_data.type == 'VIEW_3D'
-        and obj_align == 'VIEW'):
-        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.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                       new mesh (as used in from_pydata).
 # name ... Name of the new mesh (& object).
-def create_mesh_object(context, verts, edges, faces, name, align_matrix):
+def create_mesh_object(context, verts, edges, faces, name):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -66,44 +53,8 @@ def create_mesh_object(context, verts, edges, faces, name, align_matrix):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    # apply viewRotaion
-    ob_new.matrix_world = align_matrix
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 
 # A very simple "bridge" tool.
@@ -437,7 +388,6 @@ class AddSqorus(bpy.types.Operator):
         description="Enable to subdivide the faces on the outside." \
             " This results in equally spaced vertices.",
         default=True)
-    align_matrix = Matrix()
 
     def execute(self, context):
 
@@ -447,15 +397,10 @@ class AddSqorus(bpy.types.Operator):
             self.subdivide)
 
         # Create mesh object (and meshdata)
-        obj = create_mesh_object(context, verts, [], faces, "Sqorus",
-            self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Sqorus")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 class AddWedge(bpy.types.Operator):
     '''Add a wedge mesh.'''
@@ -478,7 +423,6 @@ class AddWedge(bpy.types.Operator):
         min=0.01,
         max=9999.0,
         default=2.00)
-    align_matrix = Matrix()
 
     def execute(self, context):
 
@@ -487,16 +431,10 @@ class AddWedge(bpy.types.Operator):
             self.size_y,
             self.size_z)
 
-        obj = create_mesh_object(context, verts, [], faces, "Wedge",
-            self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Wedge")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
-
 
 class AddStar(bpy.types.Operator):
     '''Add a star mesh.'''
@@ -524,7 +462,6 @@ class AddStar(bpy.types.Operator):
         min=0.01,
         max=9999.0,
         default=0.5)
-    align_matrix = Matrix()
 
     def execute(self, context):
 
@@ -534,16 +471,10 @@ class AddStar(bpy.types.Operator):
             self.innter_radius,
             self.height)
 
-        obj = create_mesh_object(context, verts, [], faces, "Star",
-            self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Star")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
-
 
 class AddTrapezohedron(bpy.types.Operator):
     """Add a trapezohedron"""
@@ -561,15 +492,14 @@ class AddTrapezohedron(bpy.types.Operator):
     height = FloatProperty(name = "Tip height",
                 description = "Height of the tip",
                 default = 1, min = 0.01, max = 100.0)
-    align_matrix = Matrix()
+
     def execute(self,context):
         # generate mesh
         verts,faces = trapezohedron(self.segments,
                                     self.radius,
                                     self.height)
         
-        obj = create_mesh_object(context, verts, [], faces, "Trapazohedron",
-            self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Trapazohedron")
 
         return {'FINISHED'}
 
diff --git a/add_mesh_gears.py b/add_mesh_gears.py
index 22ae52868..a623ce48f 100644
--- a/add_mesh_gears.py
+++ b/add_mesh_gears.py
@@ -69,24 +69,11 @@ import mathutils
 from math import *
 from bpy.props import *
 
-# calculates the matrix for the new object
-# depending on user pref
-def align_matrix(context):
-    loc = mathutils.Matrix.Translation(context.scene.cursor_location)
-    obj_align = context.user_preferences.edit.object_align
-    if (context.space_data.type == 'VIEW_3D'
-        and obj_align == 'VIEW'):
-        rot = context.space_data.region_3d.view_matrix.rotation_part().invert().resize4x4()
-    else:
-        rot = mathutils.Matrix()
-    align_matrix = loc * rot
-    return align_matrix
-
 # Create a new mesh (object) from verts/edges/faces.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                       new mesh (as used in from_pydata).
 # name ... Name of the new mesh (& object).
-def create_mesh_object(context, verts, edges, faces, name, align_matrix):
+def create_mesh_object(context, verts, edges, faces, name):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -99,45 +86,8 @@ def create_mesh_object(context, verts, edges, faces, name, align_matrix):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    # apply viewRotaion
-    ob_new.matrix_world = align_matrix
-
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 
 # A very simple "bridge" tool.
@@ -709,7 +659,6 @@ class AddGear(bpy.types.Operator):
         min=0.0,
         max=100.0,
         default=0.0)
-    align_matrix = mathutils.Matrix()
 
     def draw(self, context):
         layout = self.layout
@@ -744,7 +693,8 @@ class AddGear(bpy.types.Operator):
             crown=self.crown)
 
         # Actually create the mesh object from this geometry data.
-        obj = create_mesh_object(context, verts, [], faces, "Gear", self.align_matrix)
+        base = create_mesh_object(context, verts, [], faces, "Gear")
+        obj = base.object
 
         # Create vertex groups from stored vertices.
         tipGroup = obj.vertex_groups.new('Tips')
@@ -755,10 +705,6 @@ class AddGear(bpy.types.Operator):
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 class AddWormGear(bpy.types.Operator):
     '''Add a worm gear mesh.'''
@@ -811,7 +757,6 @@ class AddWormGear(bpy.types.Operator):
         min=0.0,
         max=100.0,
         default=0.0)
-    align_matrix = mathutils.Matrix()
 
     def draw(self, context):
         layout = self.layout
@@ -842,8 +787,8 @@ class AddWormGear(bpy.types.Operator):
             crown=self.crown)
 
         # Actually create the mesh object from this geometry data.
-        obj = create_mesh_object(context, verts, [], faces, "Worm Gear",
-            self.align_matrix)
+        base = create_mesh_object(context, verts, [], faces, "Worm Gear")
+        obj = base.object
 
         # Create vertex groups from stored vertices.
         tipGroup = obj.vertex_groups.new('Tips')
@@ -854,10 +799,6 @@ class AddWormGear(bpy.types.Operator):
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 class INFO_MT_mesh_gears_add(bpy.types.Menu):
     # Define the "Gears" menu
diff --git a/add_mesh_gemstones.py b/add_mesh_gemstones.py
index 1ebcc4eb4..3cd37157a 100644
--- a/add_mesh_gemstones.py
+++ b/add_mesh_gemstones.py
@@ -36,24 +36,11 @@ from mathutils import *
 from math import *
 from bpy.props import *
 
-# calculates the matrix for the new object
-# depending on user pref
-def align_matrix(context):
-    loc = Matrix.Translation(context.scene.cursor_location)
-    obj_align = context.user_preferences.edit.object_align
-    if (context.space_data.type == 'VIEW_3D'
-        and obj_align == 'VIEW'):
-        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.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                       new mesh (as used in from_pydata).
 # name ... Name of the new mesh (& object).
-def create_mesh_object(context, verts, edges, faces, name, align_matrix):
+def create_mesh_object(context, verts, edges, faces, name):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -66,44 +53,8 @@ def create_mesh_object(context, verts, edges, faces, name, align_matrix):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    # apply viewRotaion
-    ob_new.matrix_world = align_matrix
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 
 # A very simple "bridge" tool.
@@ -320,7 +271,6 @@ class AddDiamond(bpy.types.Operator):
         min=0.01,
         max=9999.0,
         default=0.8)
-    align_matrix = Matrix()
 
     def execute(self, context):
         verts, faces = add_diamond(self.segments,
@@ -329,15 +279,10 @@ class AddDiamond(bpy.types.Operator):
             self.crown_height,
             self.pavilion_height)
 
-        obj = create_mesh_object(context, verts, [], faces,
-            "Diamond", self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Diamond")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 class AddGem(bpy.types.Operator):
     """Add a diamond gem"""
@@ -371,7 +316,6 @@ class AddGem(bpy.types.Operator):
        min=0.01,
        max=9999.0,
        default=0.8)
-    align_matrix = Matrix()
 
     def execute(self, context):
 
@@ -383,14 +327,10 @@ class AddGem(bpy.types.Operator):
             self.pavilion_height,
             self.crown_height)
 
-        obj = create_mesh_object(context, verts, [], faces, "Gem", self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "Gem")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 class INFO_MT_mesh_gemstones_add(bpy.types.Menu):
     # Define the "Gemstones" menu
diff --git a/add_mesh_solid.py b/add_mesh_solid.py
index 507358630..5bf081283 100644
--- a/add_mesh_solid.py
+++ b/add_mesh_solid.py
@@ -40,20 +40,6 @@ from mathutils import Vector,Matrix
 #from rawMeshUtils import *
 from functools import reduce
 
-# 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
-
-    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()
-
-
 # Create a new mesh (object) from verts/edges/faces.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                       new mesh (as used in from_pydata).
@@ -71,45 +57,8 @@ def create_mesh_object(context, verts, edges, faces, name):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    ob_new.location = scene.cursor_location
-
-    apply_object_align(context, ob_new)
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 
 # A very simple "bridge" tool.
diff --git a/add_mesh_twisted_torus.py b/add_mesh_twisted_torus.py
index 195dcad81..e33cf9c17 100644
--- a/add_mesh_twisted_torus.py
+++ b/add_mesh_twisted_torus.py
@@ -51,25 +51,12 @@ import mathutils
 from mathutils import *
 from math import cos, sin, pi
 
-# calculates the matrix for the new object
-# depending on user pref
-def align_matrix(context):
-    loc = Matrix.Translation(context.scene.cursor_location)
-    obj_align = context.user_preferences.edit.object_align
-    if (context.space_data.type == 'VIEW_3D'
-        and obj_align == 'VIEW'):
-        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.
 # verts/edges/faces ... List of vertices/edges/faces for the
 #                       new mesh (as used in from_pydata).
 # name ... Name of the new mesh (& object).
-def create_mesh_object(context, verts, edges, faces, name, align_matrix):
+def create_mesh_object(context, verts, edges, faces, name):
     scene = context.scene
     obj_act = scene.objects.active
 
@@ -82,44 +69,8 @@ def create_mesh_object(context, verts, edges, faces, name, align_matrix):
     # Update mesh geometry after adding stuff.
     mesh.update()
 
-    # Deselect all objects.
-    bpy.ops.object.select_all(action='DESELECT')
-    # Always create new object
-    ob_new = bpy.data.objects.new(name, mesh)
-
-    # Link new object to the given scene and select it.
-    scene.objects.link(ob_new)
-    ob_new.select = True
-
-    # Place the object at the 3D cursor location.
-    # apply viewRotaion
-    ob_new.matrix_world = align_matrix
-
-    if obj_act and obj_act.mode == 'EDIT':
-        # We are in EditMode, switch to ObjectMode.
-        bpy.ops.object.mode_set(mode='OBJECT')
-
-        # Select the active object as well.
-        obj_act.select = True
-
-        # Apply location of new object.
-        scene.update()
-
-        # Join new object into the active.
-        bpy.ops.object.join()
-
-        # Switching back to EditMode.
-        bpy.ops.object.mode_set(mode='EDIT')
-
-        ob_new = obj_act
-
-    else:
-        # We are in ObjectMode.
-        # Make the new object the active one.
-        scene.objects.active = ob_new
-
-    return ob_new
-
+    import add_object_utils
+    return add_object_utils.object_data_add(context, mesh, operator=None)
 
 # A very simple "bridge" tool.
 # Connects two equally long vertex rows with faces.
@@ -281,7 +232,6 @@ class AddTwistedTorus(bpy.types.Operator):
         min=0.01,
         max=100.0,
         default=0.5)
-    align_matrix = Matrix()
 
     def execute(self, context):
 
@@ -298,15 +248,10 @@ class AddTwistedTorus(bpy.types.Operator):
             self.twists)
 
         # Actually create the mesh object from this geometry data.
-        obj = create_mesh_object(context, verts, [], faces, "TwistedTorus",
-            self.align_matrix)
+        obj = create_mesh_object(context, verts, [], faces, "TwistedTorus")
 
         return {'FINISHED'}
 
-    def invoke(self, context, event):
-        self.align_matrix = align_matrix(context)
-        self.execute(context)
-        return {'FINISHED'}
 
 # Add to the menu
 def menu_func(self, context):
-- 
GitLab