~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to release/scripts/vertexpaint_selfshadow_ao.py

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!BPY
2
2
"""
3
3
Name: 'Self Shadow VCols (AO)...'
4
 
Blender: 241
 
4
Blender: 245
5
5
Group: 'VertexPaint'
6
6
Tooltip: 'Generate Fake Ambient Occlusion with vertex colors.'
7
7
"""
8
8
 
9
 
__author__ = ["Campbell Barton"]
10
 
__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/")
 
9
__author__ = "Campbell Barton aka ideasman42"
 
10
__url__ = ["www.blender.org", "blenderartists.org", "www.python.org"]
11
11
__version__ = "0.1"
12
12
__bpydoc__ = """\
13
13
 
38
38
# ***** END GPL LICENCE BLOCK *****
39
39
# --------------------------------------------------------------------------
40
40
 
41
 
from Blender import *
 
41
from Blender import Scene, Draw, sys, Window, Mathutils, Mesh
 
42
import bpy
42
43
import BPyMesh
43
44
 
44
45
 
45
46
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
47
        Window.WaitCursor(1)
47
 
        V=Mathutils.Vector
48
 
        M=Mathutils.Matrix
 
48
        DotVecs = Mathutils.DotVecs
49
49
        Ang= Mathutils.AngleBetweenVecs
50
50
        
51
51
        BPyMesh.meshCalcNormals(me)
52
 
                
53
52
 
54
53
        vert_tone= [0.0] * len(me.verts)
55
54
        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
55
 
61
56
        min_tone=0
62
57
        max_tone=0
63
58
 
64
59
        for i, f in enumerate(me.faces):
65
 
                c= fcent[i]
 
60
                fc= f.cent
66
61
                fno = f.no
 
62
                
67
63
                for v in f.v:
68
64
                        vno=v.no # get a scaled down normal.
69
65
                        
70
 
                        l1= (c-(v.co-vno)).length
71
 
                        l2= (c-(v.co+vno)).length
72
 
                        
 
66
                        dot= DotVecs(vno, v.co) - DotVecs(vno, fc)
73
67
                        vert_tone_count[v.index]+=1
74
 
                        if abs(l1-l2) < 0.0000001:
75
 
                                pass
 
68
                        try:
 
69
                                a= Ang(vno, fno)
 
70
                        except:
 
71
                                continue
 
72
                        
 
73
                        # Convex
 
74
                        if dot>0:
 
75
                                a= min(PREF_CLAMP_CONVEX, a)
 
76
                                if not PREF_SHADOW_ONLY:
 
77
                                        vert_tone[v.index] += a
76
78
                        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
 
        
 
79
                                a= min(PREF_CLAMP_CONCAVE, a)
 
80
                                vert_tone[v.index] -= a
92
81
        
93
82
        # average vert_tone_list into vert_tonef
94
83
        for i, tones in enumerate(vert_tone):
95
 
                vert_tone[i] = vert_tone[i] / vert_tone_count[i]
96
 
 
 
84
                if vert_tone_count[i]:
 
85
                        vert_tone[i] = vert_tone[i] / vert_tone_count[i]
97
86
 
98
87
 
99
88
        # BLUR TONE
115
104
                                        
116
105
                                if not len_vert_tone_list_i1: len_vert_tone_list_i1=1
117
106
                                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
 
                                
 
107
                                
 
108
                                val1= (orig_vert_tone[i2]/len_vert_tone_list_i1)/ f
 
109
                                val2= (orig_vert_tone[i1]/len_vert_tone_list_i2)/ f
 
110
                                
 
111
                                vert_tone[i1]+= val1
 
112
                                vert_tone[i2]+= val2
 
113
        
122
114
 
123
115
        min_tone= min(vert_tone)
124
116
        max_tone= max(vert_tone)
125
117
        
126
 
        print min_tone, max_tone
 
118
        #print min_tone, max_tone
127
119
        
128
120
        tone_range= max_tone-min_tone
129
121
        if max_tone==min_tone:
130
122
                return
131
 
        SELFLAG= Mesh.FaceFlags.SELECT
 
123
        
132
124
        for f in me.faces:
133
 
                if not PREF_SEL_ONLY or f.flag & SELFLAG:
134
 
                        for i, v in enumerate(f.v):
 
125
                if not PREF_SEL_ONLY or f.sel:
 
126
                        f_col= f.col
 
127
                        for i, v in enumerate(f):
 
128
                                col= f_col[i]
135
129
                                tone= vert_tone[v.index]
136
 
                                tone= tone-min_tone
 
130
                                tone= (tone-min_tone)/tone_range
137
131
                                
138
 
                                f.col[i].r= f.col[i].g= f.col[i].b= int((tone/tone_range)*255)
 
132
                                col.r= int(tone*col.r)
 
133
                                col.g= int(tone*col.g)
 
134
                                col.b= int(tone*col.b)
139
135
        
140
136
        Window.WaitCursor(0)
141
137
 
142
138
def main():
143
 
        scn= Scene.GetCurrent()
144
 
        ob= scn.getActiveObject()
 
139
        sce= bpy.data.scenes.active
 
140
        ob= sce.objects.active
145
141
        
146
 
        if not ob or ob.getType() != 'Mesh':
 
142
        if not ob or ob.type != 'Mesh':
147
143
                Draw.PupMenu('Error, no active mesh object, aborting.')
148
144
                return
149
145
        
152
148
        PREF_BLUR_ITERATIONS= Draw.Create(1)    
153
149
        PREF_BLUR_RADIUS= Draw.Create(0.05)
154
150
        PREF_MIN_EDLEN= Draw.Create(0.01)
155
 
        PREF_CLAMP_CONCAVE= Draw.Create(180)
156
 
        PREF_CLAMP_CONVEX= Draw.Create(180)
 
151
        PREF_CLAMP_CONCAVE= Draw.Create(90)
 
152
        PREF_CLAMP_CONVEX= Draw.Create(20)
157
153
        PREF_SHADOW_ONLY= Draw.Create(0)
158
154
        PREF_SEL_ONLY= Draw.Create(0)   
159
155
        pup_block= [\
179
175
        PREF_SHADOW_ONLY= PREF_SHADOW_ONLY.val
180
176
        PREF_SEL_ONLY= PREF_SEL_ONLY.val
181
177
        
182
 
        if not me.faceUV:
183
 
                me.faceUV= 1
 
178
        if not me.vertexColors:
 
179
                me.vertexColors= 1
184
180
        
185
 
        #t= sys.time()
 
181
        t= sys.time()
186
182
        vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY)
187
 
        #print 'done in %.6f' % (sys.time()-t)
 
183
        print 'done in %.6f' % (sys.time()-t)
188
184
if __name__=='__main__':
189
185
        main()
190
186