added list2MeshWeight and meshWeight2List - faster then the dict equivilents and...
[blender.git] / release / scripts / vertexpaint_selfshadow_ao.py
1 #!BPY
2 """
3 Name: 'Self Shadow VCols (AO)...'
4 Blender: 241
5 Group: 'VertexPaint'
6 Tooltip: 'Generate Fake Ambient Occlusion with vertex colors.'
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 *
42 import BPyMesh
43
44
45 def vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY):
46         Window.WaitCursor(1)
47         V=Mathutils.Vector
48         M=Mathutils.Matrix
49         Ang= Mathutils.AngleBetweenVecs
50         
51         BPyMesh.meshCalcNormals(me)
52                 
53
54         vert_tone= [0.0] * len(me.verts)
55         vert_tone_count= [0] * len(me.verts)
56         
57         ed_face_users = [ [] for i in xrange(len(me.edges)) ]
58
59         fcent= [f.cent for f in me.faces]
60
61         min_tone=0
62         max_tone=0
63
64         for i, f in enumerate(me.faces):
65                 c= fcent[i]
66                 fno = f.no
67                 for v in f.v:
68                         vno=v.no # get a scaled down normal.
69                         
70                         l1= (c-(v.co-vno)).length
71                         l2= (c-(v.co+vno)).length
72                         
73                         vert_tone_count[v.index]+=1
74                         if abs(l1-l2) < 0.0000001:
75                                 pass
76                         else:
77                                 try:
78                                         a= Ang(vno, fno)
79                                 except:
80                                         continue
81                                         
82                                 
83                                 # Convex
84                                 if l1<l2:
85                                         a= min(PREF_CLAMP_CONVEX, a)
86                                         if not PREF_SHADOW_ONLY:
87                                                 vert_tone[v.index] += a
88                                 else:
89                                         a= min(PREF_CLAMP_CONCAVE, a)
90                                         vert_tone[v.index] -= a
91         
92         
93         # average vert_tone_list into vert_tonef
94         for i, tones in enumerate(vert_tone):
95                 vert_tone[i] = vert_tone[i] / vert_tone_count[i]
96
97
98
99         # BLUR TONE
100         edge_lengths= [ ed.length for ed in me.edges]
101         
102         for i in xrange(PREF_BLUR_ITERATIONS):
103                 orig_vert_tone= list(vert_tone)
104                 for ii, ed in enumerate(me.edges):
105                         i1= ed.v1.index
106                         i2= ed.v2.index
107                         l= edge_lengths[ii]
108                         
109                         f=1.0
110                         if l > PREF_MIN_EDLEN and l < PREF_BLUR_RADIUS:
111                                 f= l/PREF_BLUR_RADIUS
112                                 
113                                 len_vert_tone_list_i1 = vert_tone_count[i1]
114                                 len_vert_tone_list_i2 = vert_tone_count[i2]
115                                         
116                                 if not len_vert_tone_list_i1: len_vert_tone_list_i1=1
117                                 if not len_vert_tone_list_i2: len_vert_tone_list_i2=1
118                                         
119                                 vert_tone[i1]+= (orig_vert_tone[i2]/len_vert_tone_list_i1)/ f
120                                 vert_tone[i2]+= (orig_vert_tone[i1]/len_vert_tone_list_i2)/ f
121                                 
122
123         min_tone= min(vert_tone)
124         max_tone= max(vert_tone)
125         
126         print min_tone, max_tone
127         
128         tone_range= max_tone-min_tone
129         if max_tone==min_tone:
130                 return
131         SELFLAG= Mesh.FaceFlags.SELECT
132         for f in me.faces:
133                 if not PREF_SEL_ONLY or f.flag & SELFLAG:
134                         for i, v in enumerate(f.v):
135                                 tone= vert_tone[v.index]
136                                 tone= tone-min_tone
137                                 
138                                 f.col[i].r= f.col[i].g= f.col[i].b= int((tone/tone_range)*255)
139         
140         Window.WaitCursor(0)
141
142 def main():
143         scn= Scene.GetCurrent()
144         ob= scn.getActiveObject()
145         
146         if not ob or ob.getType() != 'Mesh':
147                 Draw.PupMenu('Error, no active mesh object, aborting.')
148                 return
149         
150         me= ob.getData(mesh=1)
151         
152         if not me.faceUV:
153                 Draw.PupMenu('Error, The active mesh does not have texface/vertex colors. aborting')
154                 return
155         
156         PREF_BLUR_ITERATIONS= Draw.Create(1)    
157         PREF_BLUR_RADIUS= Draw.Create(0.05)
158         PREF_MIN_EDLEN= Draw.Create(0.01)
159         PREF_CLAMP_CONCAVE= Draw.Create(180)
160         PREF_CLAMP_CONVEX= Draw.Create(180)
161         PREF_SHADOW_ONLY= Draw.Create(0)
162         PREF_SEL_ONLY= Draw.Create(0)   
163         pup_block= [\
164         'Post AO Blur',\
165         ('  Iterations:', PREF_BLUR_ITERATIONS, 0, 40, 'Number times to blur the colors. (higher blurs more)'),\
166         ('  Blur Radius:', PREF_BLUR_RADIUS, 0.01, 40.0, 'How much distance effects blur transfur (higher blurs more).'),\
167         ('  Min EdgeLen:', PREF_MIN_EDLEN, 0.00001, 1.0, 'Minimim edge length to blur (very low values can cause errors).'),\
168         'Angle Clipping',\
169         ('  Highlight Angle:', PREF_CLAMP_CONVEX, 0, 180, 'Less then 180 limits the angle used in the tonal range.'),\
170         ('  Shadow Angle:', PREF_CLAMP_CONCAVE, 0, 180, 'Less then 180 limits the angle used in the tonal range.'),\
171         ('Shadow Only', PREF_SHADOW_ONLY, 'Dont calculate highlights for convex areas.'),\
172         ('Sel Faces Only', PREF_SEL_ONLY, 'Only apply to UV/Face selected faces (mix vpain/uvface select).'),\
173         ]
174         
175         if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
176                 return
177         
178         PREF_BLUR_ITERATIONS= PREF_BLUR_ITERATIONS.val
179         PREF_BLUR_RADIUS= PREF_BLUR_RADIUS.val
180         PREF_MIN_EDLEN= PREF_MIN_EDLEN.val
181         PREF_CLAMP_CONCAVE= PREF_CLAMP_CONCAVE.val
182         PREF_CLAMP_CONVEX= PREF_CLAMP_CONVEX.val
183         PREF_SHADOW_ONLY= PREF_SHADOW_ONLY.val
184         PREF_SEL_ONLY= PREF_SEL_ONLY.val
185         
186         #t= sys.time()
187         vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY)
188         #print 'done in %.6f' % (sys.time()-t)
189 if __name__=='__main__':
190         main()
191