use f.area where possible over python function and use len(mface) over len(mface.v)
[blender.git] / release / scripts / weightpaint_clean.py
1 #!BPY
2 """
3 Name: 'Clean Weight...'
4 Blender: 241
5 Group: 'WeightPaint'
6 Tooltip: 'Removed verts from groups below a weight limit.'
7 """
8
9 __author__ = ["Campbell Barton"]
10 __url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/")
11 __version__ = "0.1"
12 __bpydoc__ = """\
13
14 Clean Weight
15
16 This Script is to be used only in weight paint mode,
17 It removes very low weighted verts from the current group with a weight option.
18 """
19
20 # ***** BEGIN GPL LICENSE BLOCK *****
21 #
22 # Script copyright (C) Campbell J Barton
23 #
24 # This program is free software; you can redistribute it and/or
25 # modify it under the terms of the GNU General Public License
26 # as published by the Free Software Foundation; either version 2
27 # of the License, or (at your option) any later version.
28 #
29 # This program is distributed in the hope that it will be useful,
30 # but WITHOUT ANY WARRANTY; without even the implied warranty of
31 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32 # GNU General Public License for more details.
33 #
34 # You should have received a copy of the GNU General Public License
35 # along with this program; if not, write to the Free Software Foundation,
36 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
37 #
38 # ***** END GPL LICENCE BLOCK *****
39 # --------------------------------------------------------------------------
40
41 from Blender import Scene, Draw
42 import BPyMesh
43 SMALL_NUM= 0.000001
44 def actWeightNormalize(me, PREF_THRESH, PREF_KEEP_SINGLE):
45         
46         groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
47         act_group= me.activeGroup
48         
49         for wd in vWeightDict:
50                 if not PREF_KEEP_SINGLE or len(wd) > 1:
51                         try:
52                                 w= wd[act_group]
53                                 if w <= PREF_THRESH:
54                                         # small weight, remove.
55                                         del wd[act_group]
56                         except:
57                                 pass
58         
59         # Copy weights back to the mesh.
60         BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
61
62
63 def main():
64         scn= Scene.GetCurrent()
65         ob= scn.getActiveObject()
66         
67         if not ob or ob.getType() != 'Mesh':
68                 Draw.PupMenu('Error, no active mesh object, aborting.')
69                 return
70         
71         me= ob.getData(mesh=1)
72         
73         PREF_PEAKWEIGHT= Draw.Create(0.005)
74         PREF_KEEP_SINGLE= Draw.Create(1)
75         
76         pup_block= [\
77         ('Peak Weight:', PREF_PEAKWEIGHT, 0.01, 1.0, 'Upper weight for normalizing.'),\
78         ('Keep Single User', PREF_KEEP_SINGLE, 'Dont remove verts that are in this group only.'),\
79         ]
80         
81         if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
82                 return
83         
84         PREF_PEAKWEIGHT= PREF_PEAKWEIGHT.val
85         PREF_KEEP_SINGLE= PREF_KEEP_SINGLE.val
86         
87         actWeightNormalize(me, PREF_PEAKWEIGHT, PREF_KEEP_SINGLE)
88         
89 if __name__=='__main__':
90         main()