2006-05-07 14:09:46 +00:00
|
|
|
#!BPY
|
|
|
|
"""
|
|
|
|
Name: 'Self Shadow VCols (AO)...'
|
|
|
|
Blender: 241
|
|
|
|
Group: 'VertexPaint'
|
|
|
|
Tooltip: 'Generate Fake Ambient Occlusion with vertex colors.'
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = ["Campbell Barton"]
|
|
|
|
__url__ = ("blender", "elysiun", "http://members.iinet.net.au/~cpbarton/ideasman/")
|
|
|
|
__version__ = "0.1"
|
|
|
|
__bpydoc__ = """\
|
|
|
|
|
|
|
|
Clean Weight
|
|
|
|
|
|
|
|
This Script is to be used only in weight paint mode,
|
|
|
|
It removes very low weighted verts from the current group with a weight option.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
#
|
|
|
|
# Script copyright (C) Campbell J Barton
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software Foundation,
|
|
|
|
# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ***** END GPL LICENCE BLOCK *****
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
from Blender import *
|
|
|
|
import BPyMesh
|
2006-05-10 12:12:21 +00:00
|
|
|
# reload(BPyMesh)
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
|
2006-05-15 17:42:01 +00:00
|
|
|
def vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY):
|
2006-05-07 18:13:21 +00:00
|
|
|
Window.WaitCursor(1)
|
2006-05-07 14:09:46 +00:00
|
|
|
V=Mathutils.Vector
|
|
|
|
M=Mathutils.Matrix
|
|
|
|
Ang= Mathutils.AngleBetweenVecs
|
2006-05-10 12:12:21 +00:00
|
|
|
|
|
|
|
BPyMesh.meshCalcNormals(me)
|
|
|
|
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
vert_tone= [0.0] * len(me.verts)
|
2006-05-10 12:12:21 +00:00
|
|
|
vert_tone_count= [0] * len(me.verts)
|
|
|
|
|
2006-05-07 14:09:46 +00:00
|
|
|
ed_face_users = [ [] for i in xrange(len(me.edges)) ]
|
|
|
|
|
2006-05-11 10:06:15 +00:00
|
|
|
fcent= [f.cent for f in me.faces]
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
min_tone=0
|
|
|
|
max_tone=0
|
|
|
|
|
|
|
|
for i, f in enumerate(me.faces):
|
|
|
|
c= fcent[i]
|
2006-05-11 10:06:15 +00:00
|
|
|
fno = f.no
|
2006-05-07 14:09:46 +00:00
|
|
|
for v in f.v:
|
2006-05-10 12:12:21 +00:00
|
|
|
vno=v.no # get a scaled down normal.
|
2006-05-07 14:09:46 +00:00
|
|
|
|
2006-05-10 12:12:21 +00:00
|
|
|
l1= (c-(v.co-vno)).length
|
|
|
|
l2= (c-(v.co+vno)).length
|
2006-05-07 14:09:46 +00:00
|
|
|
|
2006-05-10 12:12:21 +00:00
|
|
|
vert_tone_count[v.index]+=1
|
2006-05-07 14:09:46 +00:00
|
|
|
if abs(l1-l2) < 0.0000001:
|
2006-05-10 12:12:21 +00:00
|
|
|
pass
|
2006-05-09 14:36:31 +00:00
|
|
|
else:
|
2006-05-10 12:12:21 +00:00
|
|
|
try:
|
|
|
|
a= Ang(vno, fno)
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2006-05-09 14:36:31 +00:00
|
|
|
|
|
|
|
# Convex
|
|
|
|
if l1<l2:
|
|
|
|
a= min(PREF_CLAMP_CONVEX, a)
|
2006-05-10 12:12:21 +00:00
|
|
|
if not PREF_SHADOW_ONLY:
|
|
|
|
vert_tone[v.index] += a
|
2006-05-09 14:36:31 +00:00
|
|
|
else:
|
|
|
|
a= min(PREF_CLAMP_CONCAVE, a)
|
2006-05-10 12:12:21 +00:00
|
|
|
vert_tone[v.index] -= a
|
|
|
|
|
|
|
|
|
2006-05-09 14:36:31 +00:00
|
|
|
# average vert_tone_list into vert_tonef
|
2006-05-10 12:12:21 +00:00
|
|
|
for i, tones in enumerate(vert_tone):
|
|
|
|
vert_tone[i] = vert_tone[i] / vert_tone_count[i]
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# BLUR TONE
|
2006-05-13 05:24:58 +00:00
|
|
|
edge_lengths= [ ed.length for ed in me.edges]
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
for i in xrange(PREF_BLUR_ITERATIONS):
|
|
|
|
orig_vert_tone= list(vert_tone)
|
|
|
|
for ii, ed in enumerate(me.edges):
|
|
|
|
i1= ed.v1.index
|
|
|
|
i2= ed.v2.index
|
|
|
|
l= edge_lengths[ii]
|
|
|
|
|
2006-05-13 05:24:58 +00:00
|
|
|
f=1.0
|
2006-05-15 17:42:01 +00:00
|
|
|
if l > PREF_MIN_EDLEN and l < PREF_BLUR_RADIUS:
|
2006-05-13 05:24:58 +00:00
|
|
|
f= l/PREF_BLUR_RADIUS
|
2006-05-15 17:42:01 +00:00
|
|
|
|
|
|
|
len_vert_tone_list_i1 = vert_tone_count[i1]
|
|
|
|
len_vert_tone_list_i2 = vert_tone_count[i2]
|
|
|
|
|
|
|
|
if not len_vert_tone_list_i1: len_vert_tone_list_i1=1
|
|
|
|
if not len_vert_tone_list_i2: len_vert_tone_list_i2=1
|
|
|
|
|
|
|
|
vert_tone[i1]+= (orig_vert_tone[i2]/len_vert_tone_list_i1)/ f
|
|
|
|
vert_tone[i2]+= (orig_vert_tone[i1]/len_vert_tone_list_i2)/ f
|
|
|
|
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
min_tone= min(vert_tone)
|
|
|
|
max_tone= max(vert_tone)
|
|
|
|
|
2006-05-15 17:42:01 +00:00
|
|
|
print min_tone, max_tone
|
|
|
|
|
2006-05-07 14:09:46 +00:00
|
|
|
tone_range= max_tone-min_tone
|
|
|
|
if max_tone==min_tone:
|
|
|
|
return
|
|
|
|
SELFLAG= Mesh.FaceFlags.SELECT
|
|
|
|
for f in me.faces:
|
|
|
|
if not PREF_SEL_ONLY or f.flag & SELFLAG:
|
|
|
|
for i, v in enumerate(f.v):
|
|
|
|
tone= vert_tone[v.index]
|
|
|
|
tone= tone-min_tone
|
|
|
|
|
2006-05-10 12:12:21 +00:00
|
|
|
f.col[i].r= f.col[i].g= f.col[i].b= int((tone/tone_range)*255)
|
2006-05-07 18:13:21 +00:00
|
|
|
|
|
|
|
Window.WaitCursor(0)
|
2006-05-07 14:09:46 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
scn= Scene.GetCurrent()
|
|
|
|
ob= scn.getActiveObject()
|
|
|
|
|
|
|
|
if not ob or ob.getType() != 'Mesh':
|
|
|
|
Draw.PupMenu('Error, no active mesh object, aborting.')
|
|
|
|
return
|
|
|
|
|
|
|
|
me= ob.getData(mesh=1)
|
|
|
|
|
|
|
|
if not me.faceUV:
|
|
|
|
Draw.PupMenu('Error, The active mesh does not have texface/vertex colors. aborting')
|
|
|
|
return
|
|
|
|
|
2006-05-13 05:24:58 +00:00
|
|
|
PREF_BLUR_ITERATIONS= Draw.Create(1)
|
|
|
|
PREF_BLUR_RADIUS= Draw.Create(0.05)
|
2006-05-15 17:42:01 +00:00
|
|
|
PREF_MIN_EDLEN= Draw.Create(0.01)
|
2006-05-09 14:36:31 +00:00
|
|
|
PREF_CLAMP_CONCAVE= Draw.Create(180)
|
|
|
|
PREF_CLAMP_CONVEX= Draw.Create(180)
|
2006-05-10 12:12:21 +00:00
|
|
|
PREF_SHADOW_ONLY= Draw.Create(0)
|
|
|
|
PREF_SEL_ONLY= Draw.Create(0)
|
2006-05-07 14:09:46 +00:00
|
|
|
pup_block= [\
|
|
|
|
'Post AO Blur',\
|
2006-05-13 05:24:58 +00:00
|
|
|
(' Iterations:', PREF_BLUR_ITERATIONS, 0, 40, 'Number times to blur the colors. (higher blurs more)'),\
|
|
|
|
(' Blur Radius:', PREF_BLUR_RADIUS, 0.01, 40.0, 'How much distance effects blur transfur (higher blurs more).'),\
|
2006-05-15 17:42:01 +00:00
|
|
|
(' Min EdgeLen:', PREF_MIN_EDLEN, 0.00001, 1.0, 'Minimim edge length to blur (very low values can cause errors).'),\
|
2006-05-09 14:36:31 +00:00
|
|
|
'Angle Clipping',\
|
2006-05-10 12:12:21 +00:00
|
|
|
(' Highlight Angle:', PREF_CLAMP_CONVEX, 0, 180, 'Less then 180 limits the angle used in the tonal range.'),\
|
|
|
|
(' Shadow Angle:', PREF_CLAMP_CONCAVE, 0, 180, 'Less then 180 limits the angle used in the tonal range.'),\
|
|
|
|
('Shadow Only', PREF_SHADOW_ONLY, 'Dont calculate highlights for convex areas.'),\
|
2006-05-07 14:09:46 +00:00
|
|
|
('Sel Faces Only', PREF_SEL_ONLY, 'Only apply to UV/Face selected faces (mix vpain/uvface select).'),\
|
|
|
|
]
|
|
|
|
|
|
|
|
if not Draw.PupBlock('Clean Selected Meshes...', pup_block):
|
|
|
|
return
|
|
|
|
|
|
|
|
PREF_BLUR_ITERATIONS= PREF_BLUR_ITERATIONS.val
|
2006-05-13 05:24:58 +00:00
|
|
|
PREF_BLUR_RADIUS= PREF_BLUR_RADIUS.val
|
2006-05-15 17:42:01 +00:00
|
|
|
PREF_MIN_EDLEN= PREF_MIN_EDLEN.val
|
2006-05-09 14:36:31 +00:00
|
|
|
PREF_CLAMP_CONCAVE= PREF_CLAMP_CONCAVE.val
|
|
|
|
PREF_CLAMP_CONVEX= PREF_CLAMP_CONVEX.val
|
|
|
|
PREF_SHADOW_ONLY= PREF_SHADOW_ONLY.val
|
2006-05-07 14:09:46 +00:00
|
|
|
PREF_SEL_ONLY= PREF_SEL_ONLY.val
|
|
|
|
|
2006-05-10 12:12:21 +00:00
|
|
|
#t= sys.time()
|
2006-05-15 17:42:01 +00:00
|
|
|
vertexFakeAO(me, PREF_BLUR_ITERATIONS, PREF_BLUR_RADIUS, PREF_MIN_EDLEN, PREF_CLAMP_CONCAVE, PREF_CLAMP_CONVEX, PREF_SHADOW_ONLY, PREF_SEL_ONLY)
|
2006-05-10 12:12:21 +00:00
|
|
|
#print 'done in %.6f' % (sys.time()-t)
|
2006-05-07 14:09:46 +00:00
|
|
|
if __name__=='__main__':
|
|
|
|
main()
|
|
|
|
|