diff options
-rw-r--r-- | release/scripts/bpymodules/BPyMesh_redux.py | 47 | ||||
-rw-r--r-- | release/scripts/mesh_poly_reduce.py | 40 | ||||
-rw-r--r-- | source/blender/python/api2_2x/doc/Mesh.py | 2 |
3 files changed, 72 insertions, 17 deletions
diff --git a/release/scripts/bpymodules/BPyMesh_redux.py b/release/scripts/bpymodules/BPyMesh_redux.py index b3ea615371b..4636866ab6e 100644 --- a/release/scripts/bpymodules/BPyMesh_redux.py +++ b/release/scripts/bpymodules/BPyMesh_redux.py @@ -58,7 +58,7 @@ def ed_key(ed): if i1<i2: return i1,i2 return i2,i1 -def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEIGHT=1.0, FACE_TRIANGULATE=True, DO_UV=True, DO_VCOL=True, DO_WEIGHTS=True): +def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEIGHT=1.0, FACE_TRIANGULATE=True, DO_UV=True, DO_VCOL=True, DO_WEIGHTS=True, VGROUP_INF_REDUX= None, VGROUP_INF_WEIGHT=0.5): """ BOUNDRY_WEIGHT - 0 is no boundry weighting. 2.0 will make them twice as unlikely to collapse. FACE_AREA_WEIGHT - 0 is no weight. 1 is normal, 2.0 is higher. @@ -94,9 +94,16 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI if REMOVE_DOUBLES: me.remDoubles(0.0001) - if (not me.getVertGroupNames()) and DO_WEIGHTS: + vgroups= me.getVertGroupNames() + + if not me.getVertGroupNames(): DO_WEIGHTS= False + if (VGROUP_INF_REDUX!= None and VGROUP_INF_REDUX not in vgroups) or\ + VGROUP_INF_WEIGHT==0.0: + VGROUP_INF_REDUX= None + del vgroups + OLD_MESH_MODE= Blender.Mesh.Mode() Blender.Mesh.Mode(Blender.Mesh.SelectModes.VERTEX) @@ -267,6 +274,13 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI vert_weights[ii] += no_ang + # Use a vertex group as a weighting. + if VGROUP_INF_REDUX!=None: + vert_weights_map= [1.0] * len(verts) + # Get Weights from a vgroup. + for i, wd in enumerate(vWeightDict): + try: vert_weights_map[i]= 1+(wd[VGROUP_INF_REDUX] * VGROUP_INF_WEIGHT) + except: pass # BOUNDRY CHECKING AND WEIGHT EDGES. CAN REMOVE # Now we know how many faces link to an edge. lets get all the boundry verts @@ -421,6 +435,13 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI # do *= because we face the boundry weight to initialize the weight. 1.0 default. ced.collapse_weight*= ((no_ang * ced.length) * (1-(1/angle_diff)))# / max(len(test_faces), 1) + + + # are we using a weight map + if VGROUP_INF_REDUX: + v= vert_weights_map[i1]+vert_weights_map[i2] + ced.collapse_weight*= v + # We can calculate the weights on __init__ but this is higher qualuity. for ced in collapse_edges: @@ -496,14 +517,22 @@ def redux(ob, REDUX=0.5, BOUNDRY_WEIGHT=2.0, REMOVE_DOUBLES=False, FACE_AREA_WEI else: w1/= wscale w2/= wscale - wd= vWeightDict[i1] # v1 weight dict - for group_key, weight_value in wd.iteritems(): - wd[group_key]= weight_value*w1 - wd= vWeightDict[i2] # v1 weight dict - for group_key, weight_value in wd.iteritems(): - wd[group_key]= weight_value*w2 - + + # add verts vgroups to eachother + wd1= vWeightDict[i1] # v1 weight dict + wd2= vWeightDict[i2] # v1 weight dict + + # Make sure vert groups on both verts exist. + for wd_from, wd_to in ((wd1, wd2), (wd2, wd1)): + for group_key, weight_value in wd_from.iteritems(): + try: wd_to[group_key] # We have this weight? + except: wd_to[group_key]= 0.0 # Adding a zero weight. + + # 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) + if DO_UV or DO_VCOL: # Handel UV's and vert Colors! diff --git a/release/scripts/mesh_poly_reduce.py b/release/scripts/mesh_poly_reduce.py index d93e62f868d..daa0e1c3854 100644 --- a/release/scripts/mesh_poly_reduce.py +++ b/release/scripts/mesh_poly_reduce.py @@ -10,33 +10,46 @@ from Blender import Draw, Window, Scene, Mesh, Mathutils, sys, Object import BPyMesh reload(BPyMesh) - + def main(): scn = Scene.GetCurrent() act_ob= scn.getActiveObject() - if act_ob.getType()!='Mesh': - act_ob= None - - sel= [ob for ob in Object.GetSelected() if ob.getType()=='Mesh' if ob != act_ob] - if not sel and not act_ob: + if not act_ob or act_ob.getType()!='Mesh': Draw.PupMenu('Error, select a mesh as your active object') return + + act_me= act_ob.getData(mesh=1) + act_group= act_me.activeGroup + if not act_group: act_group= '' + + # Defaults PREF_REDUX= Draw.Create(0.5) PREF_BOUNDRY_WEIGHT= Draw.Create(5.0) PREF_REM_DOUBLES= Draw.Create(1) PREF_FACE_AREA_WEIGHT= Draw.Create(1.0) PREF_FACE_TRIANGULATE= Draw.Create(1) + + VGROUP_INF_ENABLE= Draw.Create(0) + VGROUP_INF_REDUX= Draw.Create(act_group) + VGROUP_INF_WEIGHT= Draw.Create(10.0) + PREF_DO_UV= Draw.Create(1) PREF_DO_VCOL= Draw.Create(1) PREF_DO_WEIGHTS= Draw.Create(1) pup_block = [\ ('Poly Reduce:', PREF_REDUX, 0.05, 0.95, 'Scale the meshes poly count by this value.'),\ + '',\ ('Boundry Weight:', PREF_BOUNDRY_WEIGHT, 0.0, 20.0, 'Weight boundry verts by this scale, 0.0 for no boundry weighting.'),\ ('Area Weight:', PREF_FACE_AREA_WEIGHT, 0.0, 20.0, 'Collapse edges effecting lower area faces first.'),\ ('Triangulate', PREF_FACE_TRIANGULATE, 'Convert quads to tris before reduction, for more choices of edges to collapse.'),\ + '',\ + ('VGroup Weighting', VGROUP_INF_ENABLE, 'Use a vertex group to influence the reduction, higher weights for higher quality '),\ + ('vgroup name: ', VGROUP_INF_REDUX, 0, 32, 'The name of the vertex group to use for the weight map'),\ + ('vgroup mult: ', VGROUP_INF_WEIGHT, 0.0, 100.0, 'How much to make the weight effect the reduction'),\ + '',\ ('UV Coords', PREF_DO_UV, 'Interpolate UV Coords.'),\ ('Vert Colors', PREF_DO_VCOL, 'Interpolate Vertex Colors'),\ ('Vert Weights', PREF_DO_WEIGHTS, 'Interpolate Vertex Weights'),\ @@ -51,17 +64,30 @@ def main(): PREF_REM_DOUBLES= PREF_REM_DOUBLES.val PREF_FACE_AREA_WEIGHT= PREF_FACE_AREA_WEIGHT.val PREF_FACE_TRIANGULATE= PREF_FACE_TRIANGULATE.val + + VGROUP_INF_ENABLE= VGROUP_INF_ENABLE.val + VGROUP_INF_WEIGHT= VGROUP_INF_WEIGHT.val + + if VGROUP_INF_ENABLE and VGROUP_INF_WEIGHT: + VGROUP_INF_REDUX= VGROUP_INF_REDUX.val + else: + VGROUP_INF_WEIGHT= 0.0 + VGROUP_INF_REDUX= None + + PREF_DO_UV= PREF_DO_UV.val PREF_DO_VCOL= PREF_DO_VCOL.val PREF_DO_WEIGHTS= PREF_DO_WEIGHTS.val + + t= sys.time() is_editmode = Window.EditMode() # Exit Editmode. if is_editmode: Window.EditMode(0) Window.WaitCursor(1) - BPyMesh.redux(act_ob, PREF_REDUX, PREF_BOUNDRY_WEIGHT, PREF_REM_DOUBLES, PREF_FACE_AREA_WEIGHT, PREF_FACE_TRIANGULATE, PREF_DO_UV, PREF_DO_VCOL, PREF_DO_WEIGHTS) + BPyMesh.redux(act_ob, PREF_REDUX, PREF_BOUNDRY_WEIGHT, PREF_REM_DOUBLES, PREF_FACE_AREA_WEIGHT, PREF_FACE_TRIANGULATE, PREF_DO_UV, PREF_DO_VCOL, PREF_DO_WEIGHTS, VGROUP_INF_REDUX, VGROUP_INF_WEIGHT) if is_editmode: Window.EditMode(1) Window.WaitCursor(0) diff --git a/source/blender/python/api2_2x/doc/Mesh.py b/source/blender/python/api2_2x/doc/Mesh.py index 47c295e71b1..4a292db9b84 100644 --- a/source/blender/python/api2_2x/doc/Mesh.py +++ b/source/blender/python/api2_2x/doc/Mesh.py @@ -693,7 +693,7 @@ class Mesh: @type activeFace: int @ivar activeGroup: The mesh's active vertex group. The mesh must be linked to an object (read the comment in L{addVertGroup} for more info). - @type activeGroup: string + @type activeGroup: string or None """ def getFromObject(name,cage=0): |