fix BGE bug #8668: Behavior of os.getcwd() is not consistent between operating systems
[blender-staging.git] / release / scripts / weightpaint_clean.py
1 #!BPY
2 """
3 Name: 'Clean Weight...'
4 Blender: 245
5 Group: 'WeightPaint'
6 Tooltip: 'Removed verts from groups below a weight limit.'
7 """
8
9 __author__ = "Campbell Barton aka ideasman42"
10 __url__ = ["www.blender.org", "blenderartists.org", "www.python.org"]
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, Object
42 import BPyMesh
43 def weightClean(me, PREF_THRESH, PREF_KEEP_SINGLE, PREF_OTHER_GROUPS):
44         
45         groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
46         act_group= me.activeGroup
47         
48         rem_count = 0
49         
50         if PREF_OTHER_GROUPS:
51                 for wd in vWeightDict:
52                         l = len(wd)
53                         if not PREF_KEEP_SINGLE or l > 1:
54                                 for group in wd.keys():
55                                         w= wd[group]
56                                         if w <= PREF_THRESH:
57                                                 # small weight, remove.
58                                                 del wd[group]
59                                                 rem_count +=1
60                                         l-=1
61                                         
62                                         if PREF_KEEP_SINGLE and l == 1:
63                                                 break
64         
65         else:
66                 for wd in vWeightDict:
67                         if not PREF_KEEP_SINGLE or len(wd) > 1:
68                                 try:
69                                         w= wd[act_group]
70                                         if w <= PREF_THRESH:
71                                                 # small weight, remove.
72                                                 del wd[act_group]
73                                                 rem_count +=1
74                                 except:
75                                         pass
76         
77         # Copy weights back to the mesh.
78         BPyMesh.dict2MeshWeight(me, groupNames, vWeightDict)
79         return rem_count
80
81
82 def main():
83         scn= Scene.GetCurrent()
84         ob= scn.objects.active
85         
86         if not ob or ob.type != 'Mesh':
87                 Draw.PupMenu('Error, no active mesh object, aborting.')
88                 return
89         
90         me= ob.getData(mesh=1)
91         
92         PREF_PEAKWEIGHT= Draw.Create(0.001)
93         PREF_KEEP_SINGLE= Draw.Create(1)
94         PREF_OTHER_GROUPS= Draw.Create(0)
95         
96         pup_block= [\
97         ('Peak Weight:', PREF_PEAKWEIGHT, 0.005, 1.0, 'Remove verts from groups below this weight.'),\
98         ('All Other Groups', PREF_OTHER_GROUPS, 'Clean all groups, not just the current one.'),\
99         ('Keep Single User', PREF_KEEP_SINGLE, 'Keep verts in at least 1 group.'),\
100         ]
101         
102         if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
103                 return
104         
105         rem_count = weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
106         
107         # Run on entire blend file. usefull sometimes but dont let users do it.
108         '''
109         rem_count = 0
110         for ob in Object.Get():
111                 if ob.type != 'Mesh':
112                         continue
113                 me= ob.getData(mesh=1)
114                 
115                 rem_count += weightClean(me, PREF_PEAKWEIGHT.val, PREF_KEEP_SINGLE.val, PREF_OTHER_GROUPS.val)
116         '''
117         Draw.PupMenu('Removed %i verts from groups' % rem_count)
118         
119 if __name__=='__main__':
120         main()