[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [581] trunk/py/scripts/addons/ add_mesh_gem.py: * v1.2 Updated gem script to use/write new recall data.

Martin Buerbaum martin.buerbaum at gmx.at
Thu Apr 15 10:19:06 CEST 2010


Revision: 581
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=581
Author:   pontiac
Date:     2010-04-15 10:19:06 +0200 (Thu, 15 Apr 2010)

Log Message:
-----------
* v1.2 Updated gem script to use/write new recall data.

Modified Paths:
--------------
    trunk/py/scripts/addons/add_mesh_gem.py

Modified: trunk/py/scripts/addons/add_mesh_gem.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_gem.py	2010-04-15 08:18:04 UTC (rev 580)
+++ trunk/py/scripts/addons/add_mesh_gem.py	2010-04-15 08:19:06 UTC (rev 581)
@@ -23,7 +23,7 @@
 bl_addon_info = {
     'name': 'Add Mesh: Gem',
     'author': 'Dreampainter',
-    'version': '1.1',
+    'version': '1.2',
     'blender': (2, 5, 3),
     'location': 'View3D > Add > Mesh ',
     'description': 'Adds a mesh Gem to the Add Mesh menu',
@@ -35,26 +35,33 @@
 import bpy
 
 
-# Stores the values of a list of properties in a
-# property group (named like the operator) in the object.
-# Always replaces any existing property group with the same name!
-# @todo: Should we do this in EDIT Mode? Sounds dangerous.
-def obj_store_recall_properties(ob, op, prop_list):
-    if ob and op and prop_list:
-        #print("Storing recall data for operator: " + op.bl_idname)  # DEBUG
+# Stores the values of a list of properties and the
+# operator id in a property group ('recall_op') inside the object.
+# Could (in theory) be used for non-objects.
+# Note: Replaces any existing property group with the same name!
+# ob ... Object to store the properties in.
+# op ... The operator that should be used.
+# op_args ... A dictionary with valid Blender
+#             properties (operator arguments/parameters).
+def store_recall_properties(ob, op, op_args):
+    if ob and op and op_args:
+        recall_properties = {}
 
+        # Add the operator identifier and op parameters to the properties.
+        recall_properties['op'] = op.bl_idname
+        recall_properties['args'] = op_args
+
         # Store new recall properties.
-        prop_list['recall_op'] = op.bl_idname
-        ob['recall'] = prop_list
+        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_view_rotation(context, ob):
-    align = bpy.context.user_preferences.edit.object_align
+# 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 align == 'VIEW'):
+        and obj_align == 'VIEW'):
             view3d = context.space_data
             region = view3d.region_3d
             viewMatrix = region.view_matrix
@@ -62,46 +69,56 @@
             ob.rotation_euler = rot.invert().to_euler()
 
 
-def createObject(context, verts, faces, name, edit):
-    '''Creates Meshes & Objects for the given lists of vertices and faces.'''
-
+# 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):
     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)
 
-    # Add the geometry to the mesh.
-    #mesh.add_geometry(len(verts), 0, len(faces))
-    #mesh.verts.foreach_set("co", unpack_list(verts))
-    #mesh.faces.foreach_set("verts_raw", unpack_face_list(faces))
+    # Make a mesh from a list of verts/edges/faces.
+    mesh.from_pydata(verts, edges, faces)
 
-    # To quote the documentation:
-    # "Make a mesh from a list of verts/edges/faces Until we have a nicer
-    #  way to make geometry, use this."
-    # http://www.blender.org/documentation/250PythonDoc/
-    # bpy.types.Mesh.html#bpy.types.Mesh.from_pydata
-    mesh.from_pydata(verts, [], faces)
+    # Update mesh geometry after adding stuff.
+    mesh.update()
 
     # Deselect all objects.
     bpy.ops.object.select_all(action='DESELECT')
 
-    # Update mesh geometry after adding stuff.
-    mesh.update()
+    if edit:
+        # Replace geometry of existing object
 
-    if edit:
-        # Recreate geometry of existing object
-        obj_act = context.active_object
+        # Use the active obj and select it.
         ob_new = obj_act
+        ob_new.selected = True
 
-        bpy.ops.object.mode_set(mode='EDIT')
-        bpy.ops.mesh.select_all(action='SELECT')
-        bpy.ops.mesh.delete(type='VERT')
-        bpy.ops.object.mode_set(mode='OBJECT')
+        if obj_act.mode == 'OBJECT':
+            # Get existing mesh datablock.
+            old_mesh = ob_new.data
 
-        ob_new.data = mesh
+            # Set object data to nothing
+            ob_new.data = None
 
-        ob_new.selected = True
+            # 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)
@@ -113,10 +130,8 @@
         # Place the object at the 3D cursor location.
         ob_new.location = scene.cursor_location
 
-        obj_act = scene.objects.active
+        apply_object_align(context, ob_new)
 
-        apply_view_rotation(context, ob_new)
-
     if obj_act and obj_act.mode == 'EDIT':
         if not edit:
             # We are in EditMode, switch to ObjectMode.
@@ -134,6 +149,8 @@
             # 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.
@@ -244,17 +261,17 @@
                props.pav_height,
                props.crown_height)
 
-        obj = createObject(context, verts, faces, "Gem", props.edit)
+        obj = create_mesh_object(context, verts, [], faces, "Gem", props.edit)
 
         # Store 'recall' properties in the object.
-        recall_prop_list = {
+        recall_args_list = {
             "edit": True,
             "segments": props.segments,
             "pav_radius": props.pav_radius,
             "crown_radius": props.crown_radius,
             "pav_height": props.pav_height,
             "crown_height": props.crown_height}
-        obj_store_recall_properties(obj, self, recall_prop_list)
+        store_recall_properties(obj, self, recall_args_list)
 
         return {'FINISHED'}
 




More information about the Bf-extensions-cvs mailing list