[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1143] trunk/py/scripts/addons/ add_mesh_BoltFactory: Now works in edit mode

Aaron Keith aaroninusa at gmail.com
Mon Nov 8 09:26:52 CET 2010


Revision: 1143
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=1143
Author:   spudmn
Date:     2010-11-08 09:26:50 +0100 (Mon, 08 Nov 2010)

Log Message:
-----------
Now works in edit mode

Modified Paths:
--------------
    trunk/py/scripts/addons/add_mesh_BoltFactory/Boltfactory.py
    trunk/py/scripts/addons/add_mesh_BoltFactory/__init__.py
    trunk/py/scripts/addons/add_mesh_BoltFactory/createMesh.py

Modified: trunk/py/scripts/addons/add_mesh_BoltFactory/Boltfactory.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_BoltFactory/Boltfactory.py	2010-11-07 15:09:58 UTC (rev 1142)
+++ trunk/py/scripts/addons/add_mesh_BoltFactory/Boltfactory.py	2010-11-08 08:26:50 UTC (rev 1143)
@@ -50,6 +50,13 @@
     
     align_matrix = mathutils.Matrix()
     MAX_INPUT_NUMBER = 50
+  
+  # edit - Whether to add or update.
+    edit = BoolProperty(name="",
+        description="",
+        default=False,
+        options={'HIDDEN'})
+
     
     #Model Types
     Model_Type_List = [('bf_Model_Bolt','BOLT','Bolt Model'),
@@ -291,7 +298,7 @@
         #self.bf_Minor_Dia = self.bf_Major_Dia - (1.082532 * self.bf_Pitch)
         
         Create_New_Mesh(self, context, self.align_matrix)
-
+	
         return {'FINISHED'}
         
     ##### INVOKE #####

Modified: trunk/py/scripts/addons/add_mesh_BoltFactory/__init__.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_BoltFactory/__init__.py	2010-11-07 15:09:58 UTC (rev 1142)
+++ trunk/py/scripts/addons/add_mesh_BoltFactory/__init__.py	2010-11-08 08:26:50 UTC (rev 1143)
@@ -44,7 +44,7 @@
 ##### REGISTER #####
 
 def add_mesh_bolt_button(self, context):
-    self.layout.operator(Boltfactory.add_mesh_bolt.bl_idname, text="BOLT", icon="PLUGIN")
+    self.layout.operator(Boltfactory.add_mesh_bolt.bl_idname, text="Bolt", icon="PLUGIN")
 
 
 def register():

Modified: trunk/py/scripts/addons/add_mesh_BoltFactory/createMesh.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_BoltFactory/createMesh.py	2010-11-07 15:09:58 UTC (rev 1142)
+++ trunk/py/scripts/addons/add_mesh_BoltFactory/createMesh.py	2010-11-08 08:26:50 UTC (rev 1143)
@@ -2057,7 +2057,109 @@
     
     return Move_Verts_Up_Z(verts,Thread_Height),faces
 
+# 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).
+# edit ... Replace existing mesh data.
+# Note: Using "edit" will destroy/delete existing mesh data.
+def create_mesh_object(context, verts, edges, faces, name, edit, align_matrix):
+    scene = context.scene
+    obj_act = scene.objects.active
+
+    # Can't edit anything, unless we have an active obj.
+    if edit and not obj_act:
+        return None
+
+    # Create new mesh
+    mesh = bpy.data.meshes.new(name)
+
+    # Make a mesh from a list of verts/edges/faces.
+    mesh.from_pydata(verts, edges, faces)
+
+    # Update mesh geometry after adding stuff.
+    mesh.update()
+
+    # Deselect all objects.
+    bpy.ops.object.select_all(action='DESELECT')
+
+    if edit:
+        # Replace geometry of existing object
+
+        # Use the active obj and select it.
+        ob_new = obj_act
+        ob_new.select = True
+
+        if obj_act.mode == 'OBJECT':
+            # Get existing mesh datablock.
+            old_mesh = ob_new.data
+
+            # Set object data to nothing
+            ob_new.data = None
+
+            # Clear users of existing mesh datablock.
+            old_mesh.user_clear()
+
+            # Remove old mesh datablock if no users are left.
+            if (old_mesh.users == 0):
+                bpy.data.meshes.remove(old_mesh)
+
+            # Assign new mesh datablock.
+            ob_new.data = mesh
+
+    else:
+        # 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':
+        if not 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
+
+
 def Create_New_Mesh(props, context, align_matrix):
 
     verts = []
@@ -2081,30 +2183,11 @@
     verts, faces = RemoveDoubles(verts, faces)
     
     verts = Scale_Mesh_Verts(verts,GLOBAL_SCALE)
-    
-    
-    mesh = bpy.data.meshes.new(sMeshName)
-    
-    mesh.vertices.add(len(verts))
-    mesh.faces.add(len(faces))
+  
+    obj = create_mesh_object(context, verts, [], faces,sObjName,
+            props.edit, align_matrix)
 
-    mesh.vertices.foreach_set("co", unpack_list(verts))
-    mesh.faces.foreach_set("vertices_raw", unpack_face_list(faces))
 
-
-    scene = context.scene
-
-    bpy.ops.object.select_all(action='DESELECT')
-
-    mesh.update()
-    ob_new = bpy.data.objects.new(sObjName, mesh)
-    scene.objects.link(ob_new)
-    ob_new.select = True
-    scene.objects.active = ob_new
-
-    #ob_new.location = scene.cursor_location
-    ob_new.matrix_world = align_matrix
-
     #print("Created_Object")
     return
     




More information about the Bf-extensions-cvs mailing list