Integrated Freestyle to rendering pipeline
[blender.git] / release / scripts / faceselect_same_weights.py
1 #!BPY
2 """
3 Name: 'Same Weights...'
4 Blender: 245
5 Group: 'FaceSelect'
6 Tooltip: 'Select same faces with teh same weight for the active group.'
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 Select Same Weights
15
16 Select same weights as the active face on the active group.
17 """
18
19 # ***** BEGIN GPL LICENSE BLOCK *****
20 #
21 # Script copyright (C) Campbell J Barton
22 #
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 2
26 # of the License, or (at your option) any later version.
27 #
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31 # GNU General Public License for more details.
32 #
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software Foundation,
35 # Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36 #
37 # ***** END GPL LICENCE BLOCK *****
38 # --------------------------------------------------------------------------
39
40 from Blender import Scene, Draw, Mesh
41 import BPyMesh
42
43 def selSameWeights(me, PREF_TOLERENCE):
44         
45         # Check for missing data
46         if not me.faceUV:       return
47         
48         act_group= me.activeGroup
49         if not act_group:       return
50         
51         act_face = me.faces[me.activeFace]
52         if act_face == None:    return
53         
54         
55         
56         groupNames, vWeightDict= BPyMesh.meshWeight2Dict(me)
57         
58         def get_face_weight(f):
59                 '''
60                 Return the faces median weight and weight range.
61                 '''
62                 wmin = 1.0
63                 wmax = 0.0
64                 w = 0.0
65                 for v in f:
66                         try:
67                                 new_weight = vWeightDict[v.index][act_group]
68                                 if wmin > new_weight: wmin = new_weight
69                                 if wmax < new_weight: wmax = new_weight
70                                 w += new_weight
71                         except:
72                                 pass
73                 return w, wmax-wmin # weight, range
74         
75         weight_from, weight_range_from = get_face_weight(act_face)
76         for f in me.faces:
77                 if (not f.sel) and f != act_face:
78                         weight, weight_range = get_face_weight(f)
79                         
80                         # Compare the 2 faces weight difference and difference in their contrast.
81                         if\
82                         abs(weight - weight_from) <= PREF_TOLERENCE and\
83                         abs(weight_range - weight_range_from) <= PREF_TOLERENCE:
84                                 f.sel = True
85
86
87 def main():
88         scn= Scene.GetCurrent()
89         ob= scn.objects.active
90         
91         if not ob or ob.type != 'Mesh':
92                 Draw.PupMenu('Error, no active mesh object, aborting.')
93                 return
94         
95         me= ob.getData(mesh=1)
96         
97         PREF_TOLERENCE= Draw.Create(0.1)
98         
99         pup_block= [\
100         ('Tolerence:', PREF_TOLERENCE, 0.01, 1.0, 'Tolerence for selecting faces of the same weight.'),\
101         ]
102         
103         if not Draw.PupBlock('Select Same Weight...', pup_block):
104                 return
105         
106         PREF_TOLERENCE= PREF_TOLERENCE.val
107         
108         selSameWeights(me, PREF_TOLERENCE)
109         
110 if __name__=='__main__':
111         main()