diff options
author | Campbell Barton <ideasman42@gmail.com> | 2006-05-05 20:17:59 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2006-05-05 20:17:59 +0400 |
commit | 75f4416e32353af374c08f18f46c4c5f80dd7985 (patch) | |
tree | febd111d0dc9b6a230f54ec88ca50420ef8b2eb4 /release | |
parent | 9681a329c1d8cc40947023139ad56a63c5ea5243 (diff) |
Added vertex weight python scripts accessable from the paint menu.
* clean weights (removed low weights)
* normalize, maximizes weights to a user set peak, optionaly scales other groups too to keep the proportion of the weights even. (Doubles as a weight scaler)
* Grow/Shrink, uses the mesh topology to expand/contract the vert weights. a few options for iterationsm, max length and strength.
I need a way tell if the users in Face Select mode (in python) so I can make use of the selected face flag.
Diffstat (limited to 'release')
-rw-r--r-- | release/scripts/weightpaint_clean.py | 90 | ||||
-rw-r--r-- | release/scripts/weightpaint_grow_shrink.py | 137 | ||||
-rw-r--r-- | release/scripts/weightpaint_normalize.py | 122 |
3 files changed, 349 insertions, 0 deletions
diff --git a/release/scripts/weightpaint_clean.py b/release/scripts/weightpaint_clean.py new file mode 100644 index 00000000000..d23b4ec916f --- /dev/null +++ b/release/scripts/weightpaint_clean.py @@ -0,0 +1,90 @@ +#!BPY +""" +Name: 'Clean Weight...' +Blender: 241 +Group: 'WeightPaint' +Tooltip: 'Removed verts from groups below a weight limit.' +""" + +__author__ = ["Campbell Barton"] +__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/") +__version__ = "0.1" +__bpydoc__ = """\ + +Clean Weight + +This Script is to be used only in weight paint mode, +It removes very low weighted verts from the current group with a weight option. +""" + +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# Script copyright (C) Campbell J Barton +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ***** END GPL LICENCE BLOCK ***** +# -------------------------------------------------------------------------- + +from Blender import Scene, Draw +import BPyMesh +SMALL_NUM= 0.000001 +def actWeightNormalize(me, PREF_THRESH, PREF_KEEP_SINGLE): + + groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me) + act_group= me.activeGroup + + for wd in vWeightDict: + if not PREF_KEEP_SINGLE or len(wd) > 1: + try: + w= wd[act_group] + if w <= PREF_THRESH: + # small weight, remove. + del wd[act_group] + except: + pass + + # Copy weights back to the mesh. + BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict) + + +def main(): + scn= Scene.GetCurrent() + ob= scn.getActiveObject() + + if not ob or ob.getType() != 'Mesh': + Draw.PupMenu('Error, no active mesh object, aborting.') + return + + me= ob.getData(mesh=1) + + PREF_PEAKWEIGHT= Draw.Create(0.005) + PREF_KEEP_SINGLE= Draw.Create(1) + + pup_block= [\ + ('Peak Weight:', PREF_PEAKWEIGHT, 0.01, 1.0, 'Upper weight for normalizing.'),\ + ('Keep Single User', PREF_KEEP_SINGLE, 'Dont remove verts that are in this group only.'),\ + ] + + if not Draw.PupBlock('Clean Selected Meshes...', pup_block): + return + + PREF_PEAKWEIGHT= PREF_PEAKWEIGHT.val + PREF_KEEP_SINGLE= PREF_KEEP_SINGLE.val + + actWeightNormalize(me, PREF_PEAKWEIGHT, PREF_KEEP_SINGLE) + +if __name__=='__main__': + main()
\ No newline at end of file diff --git a/release/scripts/weightpaint_grow_shrink.py b/release/scripts/weightpaint_grow_shrink.py new file mode 100644 index 00000000000..bfe651bdea5 --- /dev/null +++ b/release/scripts/weightpaint_grow_shrink.py @@ -0,0 +1,137 @@ +#!BPY +""" +Name: 'Grow/Shrink Weight...' +Blender: 241 +Group: 'WeightPaint' +Tooltip: 'Removed verts from groups below a weight limit.' +""" + +__author__ = ["Campbell Barton"] +__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/") +__version__ = "0.1" +__bpydoc__ = """\ + +Grow Shrink Weight + +This Script is to be used only in weight paint mode, +It grows/shrinks the bounds of the weight painted area +""" + +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# Script copyright (C) Campbell J Barton +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ***** END GPL LICENCE BLOCK ***** +# -------------------------------------------------------------------------- + +from Blender import Scene, Draw, Window +import BPyMesh +SMALL_NUM= 0.000001 +def actWeightNormalize(me, PREF_MODE, PREF_MAX_DIST, PREF_STRENGTH, PREF_ITERATIONS): + Window.WaitCursor(1) + groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me) + act_group= me.activeGroup + + # Start with assumed zero weights + orig_vert_weights= [0.0] * len(vWeightDict) # Will be directly assigned to orig_vert_weights + + + # fill in the zeros with real weights. + for i, wd in enumerate(vWeightDict): + try: + orig_vert_weights[i]= wd[act_group] + except: + pass + + new_vert_weights= list(orig_vert_weights) + + for dummy in xrange(PREF_ITERATIONS): + # Minimize or maximize the weights. connection based. + + if PREF_MODE==0: # Grow + op= max + else: # Shrink + op= min + + for ed in me.edges: + if not PREF_MAX_DIST or (ed.v1.co-ed.v2.co).length < PREF_MAX_DIST: + + i1= ed.v1.index + i2= ed.v2.index + new_weight= op(orig_vert_weights[i1], orig_vert_weights[i2]) + + if PREF_STRENGTH==1.0: # do a full copy + new_vert_weights[i1]= op(new_weight, new_vert_weights[i1]) + new_vert_weights[i2]= op(new_weight, new_vert_weights[i2]) + + else: # Do a faded copy + new_vert_weights[i1]= op(new_weight, new_vert_weights[i1]) + new_vert_weights[i2]= op(new_weight, new_vert_weights[i2]) + + # Face the copy with the original (orig is updated per iteration) + new_vert_weights[i1]= (new_vert_weights[i1]*PREF_STRENGTH) + (orig_vert_weights[i1]*(1-PREF_STRENGTH)) + new_vert_weights[i2]= (new_vert_weights[i2]*PREF_STRENGTH) + (orig_vert_weights[i2]*(1-PREF_STRENGTH)) + + + for i, wd in enumerate(vWeightDict): + new_weight= new_vert_weights[i] + if new_weight != orig_vert_weights[i]: + wd[act_group]= new_weight + + if dummy+1 != PREF_ITERATIONS: # dont copy the list on the last round. + orig_vert_weights= list(new_vert_weights) + + + # Copy weights back to the mesh. + BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict) + Window.WaitCursor(0) + +def main(): + scn= Scene.GetCurrent() + ob= scn.getActiveObject() + + if not ob or ob.getType() != 'Mesh': + Draw.PupMenu('Error, no active mesh object, aborting.') + return + + me= ob.getData(mesh=1) + + PREF_MAXDIST= Draw.Create(0.0) + PREF_STRENGTH= Draw.Create(1.0) + PREF_MODE= Draw.Create(0) + PREF_ITERATIONS= Draw.Create(1) + + pup_block= [\ + ('Bleed Dist:', PREF_MAXDIST, 0.0, 1.0, 'Set a distance limit for bleeding.'),\ + ('Bleed Strength:', PREF_STRENGTH, 0.01, 1.0, 'Bleed strength between adjacent verts weight. 1:full, 0:None'),\ + ('Iterations', PREF_ITERATIONS, 1, 20, 'Number of times to run the blending calculation.'),\ + ('Contract (Shrink)', PREF_MODE, 'Shrink instead of growing.'),\ + ] + + if not Draw.PupBlock('Grow/Shrink...', pup_block): + return + + PREF_MAXDIST= PREF_MAXDIST.val + PREF_STRENGTH= PREF_STRENGTH.val + PREF_MODE= PREF_MODE.val + PREF_ITERATIONS= PREF_ITERATIONS.val + + actWeightNormalize(me, PREF_MODE, PREF_MAXDIST, PREF_STRENGTH, PREF_ITERATIONS) + + +if __name__=='__main__': + main()
\ No newline at end of file diff --git a/release/scripts/weightpaint_normalize.py b/release/scripts/weightpaint_normalize.py new file mode 100644 index 00000000000..e0e7ee92b5b --- /dev/null +++ b/release/scripts/weightpaint_normalize.py @@ -0,0 +1,122 @@ +#!BPY +""" +Name: 'Normalize/Scale Weight...' +Blender: 241 +Group: 'WeightPaint' +Tooltip: 'Normalize the weight of the active weightgroup.' +""" + +__author__ = ["Campbell Barton"] +__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/") +__version__ = "0.1" +__bpydoc__ = """\ + +Normalize Weights + +This Script is to be used only in weight paint mode, +It Normalizes the weights of the current group, to the desired peak +optionaly scaling groups that are shared by these verts so the +proportion of the veighting is unchanged. +""" + +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# Script copyright (C) Campbell J Barton +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# 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 +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ***** END GPL LICENCE BLOCK ***** +# -------------------------------------------------------------------------- + +from Blender import Scene, Draw +import BPyMesh +SMALL_NUM= 0.000001 +def actWeightNormalize(me, PREF_PEAKWEIGHT, PREF_KEEP_PROPORTION): + + groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me) + new_weight= max_weight= -1.0 + act_group= me.activeGroup + + vWeightDictUsed=[False] * len(vWeightDict) + + for i, wd in enumerate(vWeightDict): + try: + new_weight= wd[act_group] + if new_weight > max_weight: + max_weight= new_weight + vWeightDictUsed[i]=wd + except: + pass + + if max_weight < SMALL_NUM or new_weight == -1: + Draw.PupMenu('No verts to normalize. exiting.') + return + + if abs(max_weight-PREF_PEAKWEIGHT) < SMALL_NUM: + Draw.PupMenu('Vert Weights are alredy normalized.') + + max_weight= max_weight/PREF_PEAKWEIGHT + + if PREF_KEEP_PROPORTION: + # TODO, PROPORTIONAL WEIGHT SCALING. + for wd in vWeightDictUsed: + if wd: # not false. + if len(wd) == 1: + # Only 1 group for thsi vert. Simple + wd[act_group] /= max_weight + else: + # More then 1 group. will need to scale all users evenly. + local_maxweight= max(wd.itervalues()) / PREF_PEAKWEIGHT + for weight in wd.iterkeys(): + wd[weight] /= local_maxweight + + + else: # Simple, just scale the weights up. + for wd in vWeightDictUsed: + if wd: # not false. + wd[act_group] /= max_weight + + # Copy weights back to the mesh. + BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict) + + +def main(): + scn= Scene.GetCurrent() + ob= scn.getActiveObject() + + if not ob or ob.getType() != 'Mesh': + Draw.PupMenu('Error, no active mesh object, aborting.') + return + + me= ob.getData(mesh=1) + + PREF_PEAKWEIGHT= Draw.Create(1.0) + PREF_KEEP_PROPORTION= Draw.Create(1) + + pup_block= [\ + ('Peak Weight:', PREF_PEAKWEIGHT, 0.01, 1.0, 'Upper weight for normalizing.'),\ + ('Proportional', PREF_KEEP_PROPORTION, 'Scale other weights so verts (Keep weights with other groups in proportion).'),\ + ] + + if not Draw.PupBlock('Clean Selected Meshes...', pup_block): + return + + PREF_PEAKWEIGHT= PREF_PEAKWEIGHT.val + PREF_KEEP_PROPORTION= PREF_KEEP_PROPORTION.val + + actWeightNormalize(me, PREF_PEAKWEIGHT, PREF_KEEP_PROPORTION) + +if __name__=='__main__': + main()
\ No newline at end of file |