[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [679] trunk/py/scripts/addons/ add_mesh_solid.py: addons/add_mesh_solid.py

Brendon Murphy meta.androcto1 at gmail.com
Sun May 23 08:35:42 CEST 2010


Revision: 679
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=679
Author:   meta-androcto
Date:     2010-05-23 08:35:42 +0200 (Sun, 23 May 2010)

Log Message:
-----------
addons/add_mesh_solid.py
creates Platonic & Archimedean & Catalan objects & more.
over 3o math object presets are included.

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

Added: trunk/py/scripts/addons/add_mesh_solid.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_solid.py	                        (rev 0)
+++ trunk/py/scripts/addons/add_mesh_solid.py	2010-05-23 06:35:42 UTC (rev 679)
@@ -0,0 +1,920 @@
+import bpy
+from bpy.props import FloatProperty,EnumProperty,BoolProperty
+from math import sqrt
+from mathutils import Vector,Matrix
+#from rawMeshUtils import *
+from functools import reduce
+
+bl_addon_info = {
+    'name': 'Add Mesh: Regular Solids',
+    'author': 'DreamPainter',
+    'version': '1',
+    'blender': (2, 5, 3),
+    'location': 'View3D > Add > Mesh > Regular Solids',
+    'description': 'Add a Regular Solid mesh.',
+    'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/Add_Mesh/Add_Solid',
+    'category': 'Add Mesh'}
+
+# 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.
+        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
+
+    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).
+# 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)
+
+    # 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.selected = 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.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
+
+
+# A very simple "bridge" tool.
+# Connects two equally long vertex rows with faces.
+# Returns a list of the new faces (list of  lists)
+#
+# vertIdx1 ... First vertex list (list of vertex indices).
+# vertIdx2 ... Second vertex list (list of vertex indices).
+# closed ... Creates a loop (first & last are closed).
+# flipped ... Invert the normal of the face(s).
+#
+# Note: You can set vertIdx1 to a single vertex index to create
+#       a fan/star of faces.
+# Note: If both vertex idx list are the same length they have
+#       to have at least 2 vertices.
+def createFaces(vertIdx1, vertIdx2, closed=False, flipped=False):
+    faces = []
+
+    if not vertIdx1 or not vertIdx2:
+        return None
+
+    if len(vertIdx1) < 2 and len(vertIdx2) < 2:
+        return None
+
+    fan = False
+    if (len(vertIdx1) != len(vertIdx2)):
+        if (len(vertIdx1) == 1 and len(vertIdx2) > 1):
+            fan = True
+        else:
+            return None
+
+    total = len(vertIdx2)
+
+    if closed:
+        # Bridge the start with the end.
+        if flipped:
+            face = [
+                vertIdx1[0],
+                vertIdx2[0],
+                vertIdx2[total - 1]]
+            if not fan:
+                face.append(vertIdx1[total - 1])
+            faces.append(face)
+
+        else:
+            face = [vertIdx2[0], vertIdx1[0]]
+            if not fan:
+                face.append(vertIdx1[total - 1])
+            face.append(vertIdx2[total - 1])
+            faces.append(face)
+
+    # Bridge the rest of the faces.
+    for num in range(total - 1):
+        if flipped:
+            if fan:
+                face = [vertIdx2[num], vertIdx1[0], vertIdx2[num + 1]]
+            else:
+                face = [vertIdx2[num], vertIdx1[num],
+                    vertIdx1[num + 1], vertIdx2[num + 1]]
+            faces.append(face)
+        else:
+            if fan:
+                face = [vertIdx1[0], vertIdx2[num], vertIdx2[num + 1]]
+            else:
+                face = [vertIdx1[num], vertIdx2[num],
+                    vertIdx2[num + 1], vertIdx1[num + 1]]
+            faces.append(face)
+
+    return faces
+# this function creates a chain of quads and, when necessary, a remaining tri
+# for each polygon created in this script. be aware though, that this function
+# assumes each polygon is convex.
+#  poly: list of faces, or a single face, like those
+#        needed for mesh.from_pydata.
+#  returns the tesselated faces.
+def createPolys(poly):
+    # check for faces
+    if len(poly) == 0:
+        return []
+    # one or more faces
+    if type(poly[0]) == type(1):
+        poly = [poly] # if only one, make it a list of one face
+    faces = []
+    for i in poly:
+        l = len(i)
+        # let all faces of 3 or 4 verts be
+        if l < 5:
+            faces.append(i)
+        # split all polygons in half and bridge the two halves
+        else:
+            half = int(l/2)
+            f = createFaces(i[:half],[i[-1-j] for j in range(half)])        
+            faces.extend(f)
+            # if the polygon has an odd number of verts, add the last tri
+            if l%2 == 1:
+                faces.append([i[half-1],i[half],i[half+1]])
+    return faces
+
+# function to make the reduce function work as a workaround to sum a list of vectors 
+def Asum(list):
+    return reduce(lambda a,b: a+b, list)
+
+# creates the 5 platonic solids as a base for the rest
+#  plato: should be one of {"4","6","8","12","20"}. decides what solid the
+#         outcome will be.
+#  returns a list of vertices and faces and the appropriate name
+def source(plato):
+    verts = []
+    faces = []
+
+    # Tetrahedron
+    if plato == "4":
+        # Calculate the necessary constants
+        s = sqrt(2)/3.0
+        t = -1/3
+        u = sqrt(6)/3
+
+        # create the vertices and faces
+        v = [(0,0,1),(2*s,0,t),(-s,u,t),(-s,-u,t)]
+        faces = [[0,1,2],[0,2,3],[0,3,1],[1,3,2]]
+
+    # Hexahedron (cube)
+    elif plato == "6":
+        # Calculate the necessary constants
+        s = 1/sqrt(3)
+    
+        # create the vertices and faces
+        v = [(-s,-s,-s),(s,-s,-s),(s,s,-s),(-s,s,-s),(-s,-s,s),(s,-s,s),(s,s,s),(-s,s,s)]
+        faces = [[0,3,2,1],[0,1,5,4],[0,4,7,3],[6,5,1,2],[6,2,3,7],[6,7,4,5]]
+
+    # Octahedron
+    elif plato == "8":
+        # create the vertices and faces
+        v = [(1,0,0),(-1,0,0),(0,1,0),(0,-1,0),(0,0,1),(0,0,-1)]
+        faces = [[4,0,2],[4,2,1],[4,1,3],[4,3,0],[5,2,0],[5,1,2],[5,3,1],[5,0,3]]
+
+    # Dodecahedron
+    elif plato == "12":
+        # Calculate the necessary constants
+        s = 1/sqrt(3)
+        t = sqrt((3-sqrt(5))/6)
+        u = sqrt((3+sqrt(5))/6)
+
+        # create the vertices and faces
+        v = [(s,s,s),(s,s,-s),(s,-s,s),(s,-s,-s),(-s,s,s),(-s,s,-s),(-s,-s,s),(-s,-s,-s),
+             (t,u,0),(-t,u,0),(t,-u,0),(-t,-u,0),(u,0,t),(u,0,-t),(-u,0,t),(-u,0,-t),(0,t,u),
+             (0,-t,u),(0,t,-u),(0,-t,-u)]
+        faces = [[0,8,9,4,16],[0,12,13,1,8],[0,16,17,2,12],[8,1,18,5,9],[12,2,10,3,13],
+                 [16,4,14,6,17],[9,5,15,14,4],[6,11,10,2,17],[3,19,18,1,13],[7,15,5,18,19],
+                 [7,11,6,14,15],[7,19,3,10,11]]
+
+    # Icosahedron
+    elif plato == "20":
+        # Calculate the necessary constants
+        s = (1+sqrt(5))/2
+        t = sqrt(1+s*s)
+        s = s/t
+        t = 1/t
+
+        # create the vertices and faces
+        v = [(s,t,0),(-s,t,0),(s,-t,0),(-s,-t,0),(t,0,s),(t,0,-s),(-t,0,s),(-t,0,-s),
+             (0,s,t),(0,-s,t),(0,s,-t),(0,-s,-t)]
+        faces = [[0,8,4],[0,5,10],[2,4,9],[2,11,5],[1,6,8],[1,10,7],[3,9,6],[3,7,11],

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list