added list2MeshWeight and meshWeight2List - faster then the dict equivilents and...
authorCampbell Barton <ideasman42@gmail.com>
Fri, 7 Jul 2006 10:46:46 +0000 (10:46 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Fri, 7 Jul 2006 10:46:46 +0000 (10:46 +0000)
Speedup for bpymesh_redux (poly reducer) with reducing vertex weights. use list2MeshWeight and meshWeight2List.

renamed vertex gradient files

Removed reload()'s
- 3ds_import.py
- mesh_cleanup.py
- mesh_poly_reduce.py
- vertexpaint_selfshadow_ao.py

release/scripts/3ds_import.py
release/scripts/bpymodules/BPyMesh.py
release/scripts/bpymodules/BPyMesh_redux.py
release/scripts/bpymodules/mesh_gradient.py [moved from release/scripts/__vertex_gradient__.py with 95% similarity]
release/scripts/mesh_cleanup.py
release/scripts/mesh_poly_reduce.py
release/scripts/vertexpaint_gradient.py [moved from release/scripts/vertpaint_gradient_stub.py with 82% similarity]
release/scripts/vertexpaint_selfshadow_ao.py
release/scripts/weightpaint_gradient.py [moved from release/scripts/weightpaint_gradient_stub.py with 90% similarity]

index 3964a691fe514a42df1d862f5952c97668b872cb..0e5780037019c870149e0a110ebdf2cb78210a05 100644 (file)
@@ -109,7 +109,6 @@ import Blender
 from Blender import Mesh, Scene, Object, Material, Image, Texture, Lamp, Mathutils
 from Blender.Mathutils import Vector
 import BPyImage
-reload( BPyImage )
 
 import struct
 from struct import calcsize, unpack
index 4cba8d6f65be375aec9225963596ab45abd71037..fd588d5669d437e1e55c1f7a2e145ab179a11f3d 100644 (file)
@@ -1,8 +1,5 @@
 import Blender
-# from BPyMesh_redux import redux # seperated because of its size.
-import BPyMesh_redux 
-reload(BPyMesh_redux)
-redux= BPyMesh_redux.redux
+from BPyMesh_redux import redux # seperated because of its size.
 
 # python 2.3 has no reversed() iterator. this will only work on lists and tuples
 try:
@@ -20,7 +17,72 @@ except:
        except:
                set= None
 
+
+
+
+
+def meshWeight2List(me):
+       ''' Takes a mesh and return its group names and a list of lists, one list per vertex.
+       aligning the each vert list with the group names, each list contains float value for the weight.
+       These 2 lists can be modified and then used with list2MeshWeight to apply the changes.
+       '''
+       
+       # Clear the vert group.
+       groupNames= me.getVertGroupNames()
+       len_groupNames= len(groupNames)
+       
+       if not len_groupNames:
+               # no verts? return a vert aligned empty list
+               return [[] for i in xrange(len(me.verts))]
+       
+       else:
+               vWeightList= [[0.0]*len_groupNames for i in xrange(len(me.verts))]
        
+       for group_index, group in enumerate(groupNames):
+               for vert_index, weight in me.getVertsFromGroup(group, 1): # (i,w)  tuples.
+                       vWeightList[vert_index][group_index]= weight
+       
+       # removed this because me may be copying teh vertex groups.
+       #for group in groupNames:
+       #       me.removeVertGroup(group)
+       
+       return groupNames, vWeightList
+
+
+def list2MeshWeight(me, groupNames, vWeightList):
+       ''' Takes a list of groups and a list of vertex Weight lists as created by meshWeight2List
+       and applys it to the mesh.'''
+       
+       if len(vWeightList) != len(me.verts):
+               raise 'Error, Lists Differ in size, do not modify your mesh.verts before updating the weights'
+       
+       # Clear the vert group.
+       currentGroupNames= me.getVertGroupNames()
+       for group in currentGroupNames:
+               me.removeVertGroup(group) # messes up the active group.
+       
+       # Add clean unused vert groupNames back
+       currentGroupNames= me.getVertGroupNames()
+       for group in groupNames:
+               me.addVertGroup(group)
+       
+       add_ = Blender.Mesh.AssignModes.ADD
+       
+       vertList= [None]
+       for i, v in enumerate(me.verts):
+               vertList[0]= i
+               for group_index, weight in enumerate(vWeightList[i]):
+                       if weight:
+                               try:
+                                       me.assignVertsToGroup(groupNames[group_index], vertList, min(1, max(0, weight)), add_)
+                               except:
+                                       pass # vert group is not used anymore.
+       
+       me.update()
+
+
+
+
 def meshWeight2Dict(me):
        ''' Takes a mesh and return its group names and a list of dicts, one dict per vertex.
        using the group as a key and a float value for the weight.
@@ -33,8 +95,8 @@ def meshWeight2Dict(me):
        groupNames= me.getVertGroupNames()
        
        for group in groupNames:
-               for index, weight in me.getVertsFromGroup(group, 1): # (i,w)  tuples.
-                       vWeightDict[index][group]= weight
+               for vert_index, weight in me.getVertsFromGroup(group, 1): # (i,w)  tuples.
+                       vWeightDict[vert_index][group]= weight
        
        # removed this because me may be copying teh vertex groups.
        #for group in groupNames:
index 4636866ab6e3da0604305e3dab2e1f3461f29539..0f3177ae651cd272710162a6eb2b1b8f8898c91f 100644 (file)
@@ -102,7 +102,16 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
        if (VGROUP_INF_REDUX!= None and VGROUP_INF_REDUX not in vgroups) or\
        VGROUP_INF_WEIGHT==0.0:
                VGROUP_INF_REDUX= None
-       del vgroups
+       
+       try:
+               VGROUP_INF_REDUX_INDEX= vgroups.index(VGROUP_INF_REDUX)
+       except:
+               VGROUP_INF_REDUX_INDEX= -1
+       
+       # del vgroups
+       len_vgroups= len(vgroups)
+       
+       
        
        OLD_MESH_MODE= Blender.Mesh.Mode()
        Blender.Mesh.Mode(Blender.Mesh.SelectModes.VERTEX)
@@ -182,7 +191,8 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                BPyMesh.meshCalcNormals(me, reuse_vertNormals)
                
                if DO_WEIGHTS:
-                       groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
+                       #groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
+                       groupNames, vWeightList= BPyMesh.meshWeight2List(me)
                
                # THIS CRASHES? Not anymore.
                verts= list(me.verts)
@@ -276,11 +286,16 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                
                # Use a vertex group as a weighting.
                if VGROUP_INF_REDUX!=None:
-                       vert_weights_map= [1.0] * len(verts)
+                       
                        # Get Weights from a vgroup.
+                       """
+                       vert_weights_map= [1.0] * len(verts)
                        for i, wd in enumerate(vWeightDict):
                                try:    vert_weights_map[i]= 1+(wd[VGROUP_INF_REDUX] * VGROUP_INF_WEIGHT)
                                except: pass
+                       """
+                       vert_weights_map= [1+(wl[VGROUP_INF_REDUX_INDEX]*VGROUP_INF_WEIGHT) for wl in vWeightList ]
+                       
                
                # BOUNDRY CHECKING AND WEIGHT EDGES. CAN REMOVE
                # Now we know how many faces link to an edge. lets get all the boundry verts
@@ -520,8 +535,9 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                                
                                
                                # add verts vgroups to eachother
+                               '''
                                wd1= vWeightDict[i1] # v1 weight dict
-                               wd2= vWeightDict[i2] # v1 weight dict
+                               wd2= vWeightDict[i2] # v2 weight dict
                                
                                # Make sure vert groups on both verts exist.
                                for wd_from, wd_to in ((wd1, wd2), (wd2, wd1)):
@@ -532,6 +548,12 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                                # Mix the weights for vert groups
                                for group_key in wd_from.iterkeys():
                                        wd1[group_key]= wd2[group_key]= (wd1[group_key]*w1) + (wd2[group_key]*w2)
+                               '''
+                               
+                               wl1= vWeightList[i1] # v1 weight dict
+                               wl2= vWeightList[i2] # v2 weight dict
+                               for group_index in xrange(len_vgroups):
+                                       wl1[group_index]= wl2[group_index]= (wl1[group_index]*w1) + (wl2[group_index]*w2)
                                
                        
                        if DO_UV or DO_VCOL:
@@ -593,7 +615,8 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI
                
                # Copy weights back to the mesh before we remove doubles.
                if DO_WEIGHTS:
-                       BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
+                       #BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
+                       BPyMesh.list2MeshWeight(me, groupNames, vWeightList)
                
                doubles= me.remDoubles(0.0001) 
                current_face_count= len(me.faces)
similarity index 95%
rename from release/scripts/__vertex_gradient__.py
rename to release/scripts/bpymodules/mesh_gradient.py
index 84dff78ad0c672d215d3479462ce77232848feaf..186adfa0591c92698835801be02152a1ad379dd6 100755 (executable)
@@ -1,21 +1,10 @@
-#!BPY
-"""
-Name: 'Weight Gradient...'
-Blender: 241
-Group: 'VertPaint'
-Tooltip: 'Grad.'
-"""
+# This is not to be used directly, vertexGradientPick can be used externaly
 
-__author__ = ["Campbell Barton"]
-__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/")
-__version__ = "0.1"
 import Blender
 import BPyMesh
 import BPyWindow
 
 
-reload(BPyWindow)
-reload(BPyMesh)
 mouseViewRay= BPyWindow.mouseViewRay
 from Blender import Mathutils, Window, Scene, Draw, sys
 from Blender.Mathutils import CrossVecs, Vector, Intersect, LineIntersect, AngleBetweenVecs
index 3d4caa50b1d832ab4205250cacfa5579d4549bba..f984b0c2512cbcffb9e8a56a2bd821bd2534b5ba 100644 (file)
@@ -10,7 +10,6 @@ from Blender.Mathutils import TriangleArea
 
 import Blender
 import BPyMesh
-reload(BPyMesh)
 dict2MeshWeight= BPyMesh.dict2MeshWeight
 meshWeight2Dict= BPyMesh.meshWeight2Dict
 
index daa0e1c3854f55ba86fdd6bb721bf651ec8b33d8..1adb9f330cf10f0efb7c52d4685354f27abb5acd 100644 (file)
@@ -8,7 +8,6 @@ Tooltip: 'Removed polygons from a mesh while maintaining the shape, textures and
 
 from Blender import Draw, Window, Scene, Mesh, Mathutils, sys, Object
 import BPyMesh
-reload(BPyMesh)
 
 
 def main():
similarity index 82%
rename from release/scripts/vertpaint_gradient_stub.py
rename to release/scripts/vertexpaint_gradient.py
index 1b94d1bfc6272cb3a2e42574cb0b5e57a1367a12..873560786e6864b2502a57d751058fbcfd64d0d1 100755 (executable)
@@ -10,8 +10,7 @@ __author__ = ["Campbell Barton"]
 __url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/")
 __version__ = "0.1"
 
-import __vertex_gradient__
-reload(__vertex_gradient__)
+import mesh_gradient
 import Blender
 
 def main():
@@ -22,7 +21,7 @@ def main():
                Blender.Draw.PupMenu('Error, no active mesh object, aborting.')
                return
        
-       __vertex_gradient__.vertexGradientPick(ob, 1)
+       mesh_gradient.vertexGradientPick(ob, 1)
        
        
 if __name__ == '__main__':
index 375639a40235fc3f024ef49dd544195214046d87..754b25e7eb501f94a211a0698433d956e9c599d5 100644 (file)
@@ -40,7 +40,6 @@ It removes very low weighted verts from the current group with a weight option.
 
 from Blender import *
 import BPyMesh
-# reload(BPyMesh)
 
 
 def vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY):
similarity index 90%
rename from release/scripts/weightpaint_gradient_stub.py
rename to release/scripts/weightpaint_gradient.py
index 8633ac67a1e37bf6954d7f1d925680981dc56a1c..4c125ca485525ee24e54b04c83eed094ab66b4bf 100755 (executable)
@@ -20,8 +20,7 @@ Note:
 Holding Shift or clicking outside the mesh on the second click will blend the first colour to nothing. 
 '''
 
-import __vertex_gradient__
-reload(__vertex_gradient__)
+import mesh_gradient
 import Blender
 
 def main():
@@ -34,7 +33,7 @@ def main():
        # MODE 0 == VCOL
        # MODE 1 == WEIGHT
        MODE= 0
-       __vertex_gradient__.vertexGradientPick(ob, MODE)
+       mesh_gradient.vertexGradientPick(ob, MODE)
 
 
 if __name__ == '__main__':