2009-11-02 09:26:55 +00:00
|
|
|
# ***** 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,
|
2010-02-12 13:34:04 +00:00
|
|
|
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-11-02 09:26:55 +00:00
|
|
|
#
|
|
|
|
# ***** END GPL LICENCE BLOCK *****
|
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
2009-12-13 14:38:30 +00:00
|
|
|
# <pep8 compliant>
|
|
|
|
|
2009-11-02 09:26:55 +00:00
|
|
|
# History
|
|
|
|
#
|
2009-11-02 20:18:05 +00:00
|
|
|
# Originally written by Campbell Barton aka ideasman42
|
|
|
|
#
|
2009-11-02 09:26:55 +00:00
|
|
|
# 2009-11-01: * 2.5 port by Keith "Wahooney" Boshoff
|
|
|
|
# * Replaced old method with my own, speed is similar (about 0.001 sec on Suzanne)
|
|
|
|
# but results are far more accurate
|
|
|
|
#
|
|
|
|
|
|
|
|
import bpy
|
|
|
|
import math
|
|
|
|
import time
|
|
|
|
|
|
|
|
from Mathutils import Vector
|
|
|
|
from bpy.props import *
|
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
def applyVertexDirt(me, blur_iterations, blur_strength, clamp_dirt, clamp_clean, dirt_only):
|
2009-11-02 09:26:55 +00:00
|
|
|
## Window.WaitCursor(1)
|
|
|
|
|
|
|
|
#BPyMesh.meshCalcNormals(me)
|
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
vert_tone = [0.0] * len(me.verts)
|
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
min_tone = 180.0
|
|
|
|
max_tone = 0.0
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
# create lookup table for each vertex's connected vertices (via edges)
|
2009-11-02 20:18:05 +00:00
|
|
|
con = []
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
con = [[] for i in range(len(me.verts))]
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
# add connected verts
|
|
|
|
for e in me.edges:
|
|
|
|
con[e.verts[0]].append(e.verts[1])
|
|
|
|
con[e.verts[1]].append(e.verts[0])
|
|
|
|
|
|
|
|
for v in me.verts:
|
|
|
|
vec = Vector()
|
|
|
|
no = v.normal
|
|
|
|
co = v.co
|
|
|
|
|
|
|
|
# get the direction of the vectors between the vertex and it's connected vertices
|
|
|
|
for c in con[v.index]:
|
|
|
|
vec += Vector(me.verts[c].co - co).normalize()
|
|
|
|
|
|
|
|
# normalize the vector by dividing by the number of connected verts
|
|
|
|
vec /= len(con[v.index])
|
|
|
|
|
|
|
|
# angle is the acos of the dot product between vert and connected verts normals
|
|
|
|
ang = math.acos(no.dot(vec))
|
|
|
|
|
|
|
|
# enforce min/max
|
2009-11-02 20:18:05 +00:00
|
|
|
ang = max(clamp_dirt, ang)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
if not dirt_only:
|
|
|
|
ang = min(clamp_clean, ang)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
vert_tone[v.index] = ang
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
# blur tones
|
2009-11-02 09:26:55 +00:00
|
|
|
for i in range(blur_iterations):
|
|
|
|
# backup the original tones
|
2009-11-02 20:18:05 +00:00
|
|
|
orig_vert_tone = list(vert_tone)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
# use connected verts look up for blurring
|
|
|
|
for j, c in enumerate(con):
|
|
|
|
for v in c:
|
|
|
|
vert_tone[j] += blur_strength * orig_vert_tone[v]
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
vert_tone[j] /= len(c) * blur_strength + 1
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
min_tone = min(vert_tone)
|
|
|
|
max_tone = max(vert_tone)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
# debug information
|
|
|
|
# print(min_tone * 2 * math.pi)
|
|
|
|
# print(max_tone * 2 * math.pi)
|
|
|
|
# print(clamp_clean)
|
|
|
|
# print(clamp_dirt)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
tone_range = max_tone - min_tone
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
if not tone_range:
|
2009-11-02 09:26:55 +00:00
|
|
|
return
|
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
active_col_layer = None
|
|
|
|
|
|
|
|
if len(me.vertex_colors):
|
|
|
|
for lay in me.vertex_colors:
|
|
|
|
if lay.active:
|
|
|
|
active_col_layer = lay.data
|
|
|
|
else:
|
|
|
|
bpy.ops.mesh.vertex_color_add()
|
|
|
|
me.vertex_colors[0].active = True
|
|
|
|
active_col_layer = me.vertex_colors[0].data
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
if not active_col_layer:
|
2009-11-07 22:12:03 +00:00
|
|
|
return('CANCELLED', )
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
for i, f in enumerate(me.faces):
|
2009-11-02 20:18:05 +00:00
|
|
|
if not me.use_paint_mask or f.selected:
|
|
|
|
|
2009-11-02 09:26:55 +00:00
|
|
|
f_col = active_col_layer[i]
|
|
|
|
|
|
|
|
f_col = [f_col.color1, f_col.color2, f_col.color3, f_col.color4]
|
|
|
|
|
|
|
|
for j, v in enumerate(f.verts):
|
|
|
|
col = f_col[j]
|
|
|
|
tone = vert_tone[me.verts[v].index]
|
2009-12-13 22:48:11 +00:00
|
|
|
tone = (tone - min_tone) / tone_range
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
if dirt_only:
|
|
|
|
tone = min(tone, 0.5)
|
|
|
|
tone *= 2
|
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
col[0] = tone * col[0]
|
|
|
|
col[1] = tone * col[1]
|
|
|
|
col[2] = tone * col[2]
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
## Window.WaitCursor(0)
|
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
|
2009-11-02 09:26:55 +00:00
|
|
|
class VertexPaintDirt(bpy.types.Operator):
|
|
|
|
|
|
|
|
bl_idname = "mesh.vertex_paint_dirt"
|
|
|
|
bl_label = "Dirty Vertex Colors"
|
2010-03-01 00:03:51 +00:00
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
blur_strength = FloatProperty(name="Blur Strength", description="Blur strength per iteration", default=1.0, min=0.01, max=1.0)
|
|
|
|
blur_iterations = IntProperty(name="Blur Iterations", description="Number times to blur the colors. (higher blurs more)", default=1, min=0, max=40)
|
|
|
|
clean_angle = FloatProperty(name="Highlight Angle", description="Less then 90 limits the angle used in the tonal range", default=180.0, min=0.0, max=180.0)
|
|
|
|
dirt_angle = FloatProperty(name="Dirt Angle", description="Less then 90 limits the angle used in the tonal range", default=0.0, min=0.0, max=180.0)
|
|
|
|
dirt_only = BoolProperty(name="Dirt Only", description="Dont calculate cleans for convex areas", default=False)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
def execute(self, context):
|
2009-12-26 09:36:50 +00:00
|
|
|
obj = context.object
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-12-26 09:36:50 +00:00
|
|
|
if not obj or obj.type != 'MESH':
|
2010-02-11 23:13:47 +00:00
|
|
|
print('Error, no active mesh object, aborting')
|
2009-11-02 09:26:55 +00:00
|
|
|
return('CANCELLED',)
|
|
|
|
|
2009-12-26 09:36:50 +00:00
|
|
|
mesh = obj.data
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
t = time.time()
|
|
|
|
|
2009-12-26 09:36:50 +00:00
|
|
|
applyVertexDirt(mesh, self.properties.blur_iterations, self.properties.blur_strength, math.radians(self.properties.dirt_angle), math.radians(self.properties.clean_angle), self.properties.dirt_only)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
print('Dirt calculated in %.6f' % (time.time() - t))
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
return('FINISHED',)
|
|
|
|
|
|
|
|
|
2010-02-14 11:21:21 +00:00
|
|
|
def register():
|
|
|
|
bpy.types.register(VertexPaintDirt)
|
|
|
|
|
2010-02-22 23:32:58 +00:00
|
|
|
|
2010-02-14 11:21:21 +00:00
|
|
|
def unregister():
|
|
|
|
bpy.types.unregister(VertexPaintDirt)
|
|
|
|
|
2010-02-16 09:55:07 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
register()
|