added python 2.3 set importer for BPyMesh_redux
authorCampbell Barton <ideasman42@gmail.com>
Mon, 3 Jul 2006 01:52:14 +0000 (01:52 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Mon, 3 Jul 2006 01:52:14 +0000 (01:52 +0000)
made ngon loop-reduce function faster by replacing dicts with sets
off_export has some errors, modernized the script.
added a python 2.3 reversed compat function - just uses ls[::-1]

Further 2.3 compat testing needed.

release/scripts/3ds_import.py
release/scripts/bpymodules/BPyMesh.py
release/scripts/bpymodules/BPyMesh_redux.py
release/scripts/off_export.py

index 157881087fba115cb5d55c7dabcde6c57a6fab89..f51a764586e934654035a2c02c9677852cbaa7f9 100644 (file)
@@ -112,10 +112,12 @@ from struct import calcsize, unpack
 import os
 
 # If python version is less than 2.4, try to get set stuff from module
-import sys
-if ( (sys.version_info[0] <= 2) and (sys.version_info[1] < 4) ):
+try:
+       set
+except:
        from sets import Set as set
 
+
 #this script imports uvcoords as sticky vertex coords
 #this parameter enables copying these to face uv coords
 #which shold be more useful.
index 5e111340217ed38bec01540f500aa9cbe75c72e3..02896296febf917d239301def2e93ed7246f8b77 100644 (file)
@@ -1,5 +1,22 @@
 import Blender
-from BPyMesh_redux import redux # seperated because of its size.
+#from BPyMesh_redux import redux # seperated because of its size.
+#from BPyMesh_redux import redux # seperated because of its size.
+import BPyMesh_redux 
+reload(BPyMesh_redux)
+redux= BPyMesh_redux.redux
+
+# python 2.3 has no reversed() iterator. this will only work on lists and tuples
+try:
+       reversed
+except:
+       def reversed(l): return l[::-1]
+
+
+# If python version is less than 2.4, try to get set stuff from module
+try:
+       set
+except:
+       from sets import Set as set
 
        
 def meshWeight2Dict(me):
@@ -653,7 +670,7 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                else:
                        verts= [from_data.verts[i].co for ii, i in enumerate(indices)]
                
-               for i in reversed(xrange(1, len(verts))):
+               for i in xrange(len(verts)-1, 0, -1): # same as reversed(xrange(1, len(verts))):
                        if verts[i][1]==verts[i-1][0]:
                                verts.pop(i-1)
                
@@ -678,15 +695,15 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                        return []
                
                
-               edge_used_count= {}
-               del_edges= {}
+               edges_used= set()
+               edges_doubles= set()
                # We need to check if any edges are used twice location based.
                for ed in edges:
                        edkey= ed_key_mlen(verts[ed[0]], verts[ed[1]])
-                       try:
-                               del_edges[edkey]= edge_used_count[edkey]
-                       except:
-                               edge_used_count[edkey]= True
+                       if edkey in edges_used:
+                               edges_doubles.add(edkey)
+                       else:
+                               edges_used.add(edkey)
                
                # Store a list of unconnected loop segments split by double edges.
                # will join later
@@ -698,12 +715,8 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                
                for v in verts:
                        if v!=v_prev:
-                               # Arze we crossing an edge we removed?
-                               #if del_edges.has_key(  ):
-                               try:    eddata= del_edges[ed_key_mlen(v, v_prev)]
-                               except: eddata= None
-                               
-                               if eddata:
+                               # Are we crossing an edge we removed?
+                               if ed_key_mlen(v, v_prev) in edges_doubles:
                                        context_loop= [v]
                                        loop_segments.append(context_loop)
                                else:
@@ -739,10 +752,10 @@ def ngon(from_data, indices, PREF_FIX_LOOPS= True):
                        joining_segments= False
                        segcount= len(loop_segments)
                        
-                       for j in reversed(xrange(segcount)):
+                       for j in xrange(segcount-1, -1, -1): #reversed(xrange(segcount)):
                                seg_j= loop_segments[j]
                                if seg_j:
-                                       for k in reversed(xrange(j)):
+                                       for k in xrange(j-1, -1, -1): # reversed(xrange(j)):
                                                if not seg_j:
                                                        break
                                                seg_k= loop_segments[k]
@@ -855,8 +868,8 @@ def meshCalcNormals(me, vertNormals=None):
                len_fnos= len(fnos)
                if len_fnos>1:
                        totAngDiff=0
-                       for j in reversed(xrange(len_fnos)):
-                               for k in reversed(xrange(j)):
+                       for j in xrange(len_fnos-1, -1, -1): # same as reversed(xrange(...))
+                               for k in xrange(j-1, -1, -1): # same as reversed(xrange(...))
                                        #print j,k
                                        try:
                                                totAngDiff+= (Ang(fnos[j], fnos[k])) # /180 isnt needed, just to keeop the vert small.
index 0ffbc313e413031f3a40bb4a714710f83efee6bc..680ee27859e85f92b737b935abece15bcc262863 100644 (file)
@@ -28,6 +28,12 @@ LineIntersect= Blender.Mathutils.LineIntersect
 CrossVecs= Blender.Mathutils.CrossVecs
 import BPyMesh
 
+# If python version is less than 2.4, try to get set stuff from module
+
+try:
+       set
+except:
+       from sets import Set as set
 
 def uv_key(uv):
        return round(uv.x, 5), round(uv.y, 5)
@@ -185,15 +191,16 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                else:
                        for i, ed in enumerate(edges):
                                collapse_edges[i].init_from_edge(ed)
-                       # Faster then slicing
-                       for ii in xrange(len(collapse_edges)-(i+1)):
-                               collapse_edges.pop()
+                       
+                       # Strip the unneeded end off the list
+                       collapse_edges[i+1:]= []
                                
                        for i, f in enumerate(faces):
                                collapse_faces[i].init_from_face(f)
-                       # Faster then slicing
-                       for ii in xrange(len(collapse_faces)-(i+1)):
-                               collapse_faces.pop()
+                       
+                       # Strip the unneeded end off the list
+                       collapse_faces[i+1:]= []
+                       
                        
                collapse_edges_dict= dict( [(ced.key, ced) for ced in collapse_edges] )
                
index 53bd9ef6a6ff2ac2197a2aa7c8ce63926a8dbd3a..f1c0e397032c9d59f839ad6c67cae3609c5a409e 100644 (file)
@@ -56,54 +56,53 @@ Notes:<br>
 #
 # ***** END GPL LICENCE BLOCK *****
 
-import Blender, meshtools
+import Blender
 #import time
 
+# Python 2.3 has no reversed.
+try:
+       reversed
+except:
+       def reversed(l): return l[::-1]
+
 # ==============================
 # ====== Write OFF Format ======
 # ==============================
 def write(filename):
        #start = time.clock()
-       file = open(filename, "wb")
-
-       objects = Blender.Object.GetSelected()
-       objname = objects[0].name
-       meshname = objects[0].data.name
-       mesh = Blender.NMesh.GetRaw(meshname)
-       #mesh = Blender.NMesh.GetRawFromObject(meshname)        # for SubSurf
-       obj = Blender.Object.Get(objname)
+       file = open(filename, 'wb')
+       scn= Blender.Scene.GetCurrent()
+       object= scn.getActiveObject()
+       if not object or object.getType()!='Mesh':
+               Blender.Draw.PupMenu('Error%t|Select 1 active mesh object')
+               return
+               
+       mesh = object.getData(mesh=1)
 
        # === OFF Header ===
-       file.write("OFF\n")
-       file.write("%d %d %d\n" % (len(mesh.verts), len(mesh.faces), 0))
+       file.write('OFF\n')
+       file.write('%d %d %d\n' % (len(mesh.verts), len(mesh.faces), 0))
 
        # === Vertex List ===
-       for i in range(len(mesh.verts)):
-               if not i%100 and meshtools.show_progress:
-                       Blender.Window.DrawProgressBar(float(i)/len(mesh.verts), "Writing Verts")
-               x, y, z = mesh.verts[i].co
-               file.write("%f %f %f\n" % (x, y, z))
+       for i, v in enumerate(mesh.verts):
+               file.write('%f %f %f\n' % tuple(v.co))
 
        # === Face List ===
-       for i in range(len(mesh.faces)):
-               if not i%100 and meshtools.show_progress:
-                       Blender.Window.DrawProgressBar(float(i)/len(mesh.faces), "Writing Faces")
-               file.write(`len(mesh.faces[i].v)`+' ')
-               mesh.faces[i].v.reverse()
-               for j in range(len(mesh.faces[i].v)):
-                       file.write(`mesh.faces[i].v[j].index`+' ')
-               file.write("\n")
+       for i, f in enumerate(mesh.faces):
+               file.write('%i ' % len(f))
+               for v in reversed(f.v):
+                       file.write('%d ' % v.index)
+               file.write('\n')
 
 
        Blender.Window.DrawProgressBar(1.0, '')  # clear progressbar
        file.close()
        #end = time.clock()
        #seconds = " in %.2f %s" % (end-start, "seconds")
-       message = "Successfully exported " + Blender.sys.basename(filename)# + seconds
-       meshtools.print_boxed(message)
+       message = 'Successfully exported "%s"' % Blender.sys.basename(filename)# + seconds
 
 def fs_callback(filename):
        if filename.find('.off', -4) <= 0: filename += '.off'
        write(filename)
 
-Blender.Window.FileSelector(fs_callback, "Export OFF")
+Blender.Window.FileSelector(fs_callback, "Export OFF", Blender.sys.makename(ext='.off'))