[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1875] trunk/py/scripts/addons/ add_mesh_solid.py: first commit, lets see if it works

Gerwin Damsteegt gjdamsteegt at gmail.com
Tue Apr 26 23:14:38 CEST 2011


Revision: 1875
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1875
Author:   dreampainter
Date:     2011-04-26 21:14:37 +0000 (Tue, 26 Apr 2011)
Log Message:
-----------
first commit, lets see if it works

updating regular solids add mesh script to version 2

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

Modified: trunk/py/scripts/addons/add_mesh_solid.py
===================================================================
--- trunk/py/scripts/addons/add_mesh_solid.py	2011-04-26 12:23:02 UTC (rev 1874)
+++ trunk/py/scripts/addons/add_mesh_solid.py	2011-04-26 21:14:37 UTC (rev 1875)
@@ -8,7 +8,7 @@
 #
 # 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
+# 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
@@ -20,10 +20,10 @@
 bl_info = {
     "name": "Regular Solids",
     "author": "DreamPainter",
-    "version": (1, 0, 1),
+    "version": (2, 0),
     "blender": (2, 5, 7),
-    "api": 35853,
-    "location": "View3D > Add > Mesh > Regular Solids",
+    "api": 36336,
+    "location": "View3D > Add > Mesh > Solids",
     "description": "Add a Regular Solid mesh.",
     "warning": "",
     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
@@ -32,102 +32,13 @@
         "func=detail&aid=22405",
     "category": "Add Mesh"}
 
-
 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
+from add_object_utils import object_data_add
 
-# 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):
-    scene = context.scene
-    obj_act = scene.objects.active
-
-    # 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()
-
-    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.
-# 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.
@@ -143,28 +54,26 @@
         poly = [poly] # if only one, make it a list of one face
     faces = []
     for i in poly:
-        l = len(i)
+        L = len(i)
         # let all faces of 3 or 4 verts be
-        if l < 5:
+        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)])        
+            f = [[i[x],i[x+1],i[L-2-x],i[L-1-x]] for x in range(L//2-1)]
             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]])
+            if L&1 == 1: 
+                faces.append([i[L//2-1+x] for x in [0,1,2]])
     return faces
-
+    
 # function to make the reduce function work as a workaround to sum a list of vectors 
-def Asum(list):
+def vSum(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
+#  returns a list of vertices and faces
 def source(plato):
     verts = []
     faces = []
@@ -225,16 +134,11 @@
                  [0,10,8],[1,8,10],[2,9,11],[3,11,9],[4,2,0],[5,0,2],[6,1,3],[7,3,1],
                  [8,6,4],[9,4,6],[10,5,7],[11,7,5]]
 
-    # handles faulty values of plato
-    else:
-        print("Choose keyword 'plato' from {'4','6','8','12','20'}")
-        return None
-
     # convert the tuples to Vectors
     verts = [Vector(i) for i in v]
 
     return verts,faces
-
+    
 # processes the raw data from source
 def createSolid(plato,vtrunc,etrunc,dual,snub):
     verts = []
@@ -250,304 +154,194 @@
     # constants saving space and readability
     vtrunc *= 0.5
     etrunc *= 0.5
-    supposed_size = 0
-    noSnub = (snub == "0") or (etrunc == 0.5) or (etrunc == 0)
-    lSnub = (snub == "L") and (0 < etrunc < 0.5)
-    rSnub = (snub == "R") and (0 < etrunc < 0.5)
+    supposedSize = 0
+    noSnub = (snub == "None") or (etrunc == 0.5) or (etrunc == 0)
+    lSnub = (snub == "Left") and (0 < etrunc < 0.5)
+    rSnub = (snub == "Right") and (0 < etrunc < 0.5)
 
     # no truncation
     if vtrunc == 0:
         if dual: # dual is as simple as another, but mirrored platonic solid
-            vInput,fInput = source(dualSource[plato])
-            supposed_size = Asum(vInput[i] for i in fInput[0]).length / len(fInput[0])
-            vInput = [-i*supposed_size for i in vInput]            # mirror it
-            return vInput,fInput
+            vInput, fInput = source(dualSource[plato])
+            supposedSize = vSum(vInput[i] for i in fInput[0]).length/len(fInput[0])
+            vInput = [-i*supposedSize for i in vInput]            # mirror it
+            return vInput, fInput
         return source(plato)
-    # simple truncation of the source
-    elif 0.5 >= vtrunc > 0:
-        vInput,fInput = source(plato)
-    # truncation is now equal to simple truncation of the dual of the source
-    elif vtrunc > 0.5: 
-        vInput,fInput = source(dualSource[plato])
-        supposed_size = Asum(vInput[i] for i in fInput[0]).length / len(fInput[0])
-        # account for the source being a dual
-        vtrunc = 1-vtrunc
-        if vtrunc == 0: # no truncation
+    elif 0 < vtrunc <= 0.5: # simple truncation of the source
+        vInput, fInput = source(plato)
+    else:
+        # truncation is now equal to simple truncation of the dual of the source
+        vInput, fInput = source(dualSource[plato])
+        supposedSize = vSum(vInput[i] for i in fInput[0]).length / len(fInput[0])
+        vtrunc = 1-vtrunc # account for the source being a dual
+        if vtrunc == 0: # no truncation needed
             if dual:
-                vInput,fInput = source(plato)
-                vInput = [i*supposed_size for i in vInput]
-                return vInput,fInput#,sourceName
-                #JayDez - I don't know what sourceName is, but commenting that
-                #part out fixes vert truncation problems.
-            vInput = [-i*supposed_size for i in vInput]
-            return vInput,fInput
-
-    # generate a database for creating the faces. this exists out of a list for
-    # every vertex in the source
-    # 0 : vertex id
-    # 1 : vertices connected to this vertex, listed ccw(Counter Clock Wise)
-    # 2 : vertices generated to form the faces of this vertex
-    # 3 : faces connected to this vertex, listed ccw
-    # 4 : dictionairy containing the verts used by the connected faces
-    # 5 : list of edges that use this vertex, listed ccw
-    # 6 : dictionairy containing the verts used by the connected edges
-    v = [[i,[],[],[],{},[],{}] for i in range(len(vInput))]
-
-    # this piece of code, generates the database and the lists in ccw order
+                vInput, fInput = source(plato)
+                vInput = [i*supposedSize for i in vInput]
+                return vInput, fInput
+            vInput = [-i*supposedSize for i in vInput]
+            return vInput, fInput
+    
+    # generate connection database
+    vDict = [{} for i in vInput]
+    # for every face, store what vertex comes after and before the current vertex    
     for x in range(len(fInput)):
         i = fInput[x]
-        # in every faces, check which vertices connect the each vert and sort
-        #  in ccw order
-        for j in range(-1,len(i)-1):
-            # only generate an edge dict, if edge truncation is needed
-            if etrunc:
-                # list edges as [min,max], to evade confusion
-                first = min([i[j-1],i[j]])
-                last = max([i[j-1],i[j]])
-                # if an edge is not allready in, add it and give the index
-                try:
-                    y = edges.index([first,last])

@@ Diff output truncated at 10240 characters. @@


More information about the Bf-extensions-cvs mailing list