[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [578] trunk/py/scripts/addons/ mesh_utils.py: * Added mesh_utils.py (Temporarily - This may be moved to " modules" in the future).

Martin Buerbaum martin.buerbaum at gmx.at
Wed Apr 14 16:17:20 CEST 2010


Revision: 578
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=578
Author:   pontiac
Date:     2010-04-14 16:17:20 +0200 (Wed, 14 Apr 2010)

Log Message:
-----------
* Added mesh_utils.py (Temporarily - This may be moved to "modules" in the future).
* See also the patch [#21731] New module mesh_utils.py (various functions to create "smart" mesh objects)
* http://projects.blender.org/tracker/index.php?func=detail&aid=21731&group_id=9&atid=127

Added Paths:
-----------
    trunk/py/scripts/addons/mesh_utils.py

Added: trunk/py/scripts/addons/mesh_utils.py
===================================================================
--- trunk/py/scripts/addons/mesh_utils.py	                        (rev 0)
+++ trunk/py/scripts/addons/mesh_utils.py	2010-04-14 14:17:20 UTC (rev 578)
@@ -0,0 +1,126 @@
+# ##### BEGIN GPL LICENSE BLOCK #####
+#
+#  This program is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+import bpy
+
+
+# Stores the values of a list of properties in a
+# property group (named like the operator) in the object.
+# Could (in theory) be used for non-objects.
+# Note: Replaces any existing property group with the same name!
+def store_recall_properties(ob, op, prop_list):
+    if ob and op and prop_list:
+        # Store new recall properties.
+        prop_list['recall_op'] = op.bl_idname
+        ob['recall'] = prop_list
+
+
+# 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 and faces for the new mesh.
+# 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):
+    '''Creates Meshes (& Objects) for the given lists of vertices and faces.'''
+
+    scene = context.scene
+    obj_act = scene.objects.active
+
+    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
+        ob_new = obj_act
+
+        if obj_act.mode == 'OBJECT':
+            # Remove existing vertices.
+            # @todo There is probably a better/faster way (delete(mesh)?).
+            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')
+
+            ob_new.data = mesh
+
+        ob_new.selected = True
+
+    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.selected = 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':
+        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.selected = 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




More information about the Bf-extensions-cvs mailing list