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
|
|
|
#
|
2011-10-17 06:58:07 +00:00
|
|
|
# ***** END GPL LICENSE BLOCK *****
|
2009-11-02 09:26:55 +00:00
|
|
|
# --------------------------------------------------------------------------
|
|
|
|
|
2009-12-13 14:38:30 +00:00
|
|
|
# <pep8 compliant>
|
|
|
|
|
2013-04-15 11:29:31 +00:00
|
|
|
# Contributor(s): Keith "Wahooney" Boshoff, Campbell Barton
|
2011-03-14 23:17:52 +00:00
|
|
|
|
2013-06-27 03:05:19 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
def applyVertexDirt(me, blur_iterations, blur_strength, clamp_dirt, clamp_clean, dirt_only):
|
2011-02-27 14:16:32 +00:00
|
|
|
from mathutils import Vector
|
|
|
|
from math import acos
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2010-08-18 03:42:26 +00:00
|
|
|
vert_tone = [0.0] * len(me.vertices)
|
2009-11-02 20:18:05 +00:00
|
|
|
|
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
|
|
|
|
2010-08-18 03:42:26 +00:00
|
|
|
con = [[] for i in range(len(me.vertices))]
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
# add connected verts
|
|
|
|
for e in me.edges:
|
2010-08-18 03:42:26 +00:00
|
|
|
con[e.vertices[0]].append(e.vertices[1])
|
|
|
|
con[e.vertices[1]].append(e.vertices[0])
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2010-08-18 03:42:26 +00:00
|
|
|
for i, v in enumerate(me.vertices):
|
2009-11-02 09:26:55 +00:00
|
|
|
vec = Vector()
|
|
|
|
no = v.normal
|
|
|
|
co = v.co
|
|
|
|
|
|
|
|
# get the direction of the vectors between the vertex and it's connected vertices
|
2010-04-21 16:22:37 +00:00
|
|
|
for c in con[i]:
|
2011-02-11 12:30:17 +00:00
|
|
|
vec += (me.vertices[c].co - co).normalized()
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
# normalize the vector by dividing by the number of connected verts
|
2010-04-21 16:22:37 +00:00
|
|
|
tot_con = len(con[i])
|
2010-05-03 16:00:42 +00:00
|
|
|
|
2010-04-21 16:22:37 +00:00
|
|
|
if tot_con == 0:
|
|
|
|
continue
|
2010-05-03 16:00:42 +00:00
|
|
|
|
2010-04-21 16:22:37 +00:00
|
|
|
vec /= tot_con
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2011-10-17 06:58:07 +00:00
|
|
|
# angle is the acos() of the dot product between vert and connected verts normals
|
2011-02-27 14:16:32 +00:00
|
|
|
ang = acos(no.dot(vec))
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2010-04-21 16:22:37 +00:00
|
|
|
vert_tone[i] = 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:
|
2012-04-13 08:41:30 +00:00
|
|
|
return {'CANCELLED'}
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-11-02 20:18:05 +00:00
|
|
|
active_col_layer = None
|
|
|
|
|
2014-02-18 13:05:31 +00:00
|
|
|
if me.vertex_colors:
|
2009-11-02 20:18:05 +00:00
|
|
|
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:
|
2014-08-15 17:50:16 +00:00
|
|
|
return {'CANCELLED'}
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2012-04-13 08:41:30 +00:00
|
|
|
use_paint_mask = me.use_paint_mask
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2012-04-13 08:41:30 +00:00
|
|
|
for i, p in enumerate(me.polygons):
|
|
|
|
if not use_paint_mask or p.select:
|
|
|
|
for loop_index in p.loop_indices:
|
|
|
|
loop = me.loops[loop_index]
|
|
|
|
v = loop.vertex_index
|
|
|
|
col = active_col_layer[loop_index].color
|
|
|
|
tone = vert_tone[v]
|
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)
|
2013-04-20 13:23:53 +00:00
|
|
|
tone *= 2.0
|
2009-11-02 20:18:05 +00:00
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
col[0] = tone * col[0]
|
|
|
|
col[1] = tone * col[1]
|
|
|
|
col[2] = tone * col[2]
|
2013-01-03 07:01:41 +00:00
|
|
|
me.update()
|
2012-04-13 08:41:30 +00:00
|
|
|
return {'FINISHED'}
|
|
|
|
|
2011-02-27 14:16:32 +00:00
|
|
|
|
|
|
|
import bpy
|
2011-08-12 06:57:00 +00:00
|
|
|
from bpy.types import Operator
|
2011-02-27 14:16:32 +00:00
|
|
|
from bpy.props import FloatProperty, IntProperty, BoolProperty
|
2013-01-03 07:01:41 +00:00
|
|
|
from math import pi
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2009-12-13 22:48:11 +00:00
|
|
|
|
2011-08-12 06:57:00 +00:00
|
|
|
class VertexPaintDirt(Operator):
|
2010-04-21 16:22:37 +00:00
|
|
|
bl_idname = "paint.vertex_color_dirt"
|
2009-11-02 09:26:55 +00:00
|
|
|
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
|
|
|
|
2011-08-19 19:25:20 +00:00
|
|
|
blur_strength = FloatProperty(
|
|
|
|
name="Blur Strength",
|
|
|
|
description="Blur strength per iteration",
|
|
|
|
min=0.01, max=1.0,
|
|
|
|
default=1.0,
|
|
|
|
)
|
|
|
|
blur_iterations = IntProperty(
|
|
|
|
name="Blur Iterations",
|
2011-10-23 17:52:20 +00:00
|
|
|
description="Number of times to blur the colors (higher blurs more)",
|
2011-08-19 19:25:20 +00:00
|
|
|
min=0, max=40,
|
|
|
|
default=1,
|
|
|
|
)
|
|
|
|
clean_angle = FloatProperty(
|
|
|
|
name="Highlight Angle",
|
2012-03-17 14:27:46 +00:00
|
|
|
description="Less than 90 limits the angle used in the tonal range",
|
2013-01-03 07:01:41 +00:00
|
|
|
min=0.0, max=pi,
|
|
|
|
default=pi,
|
|
|
|
unit="ROTATION",
|
2011-08-19 19:25:20 +00:00
|
|
|
)
|
|
|
|
dirt_angle = FloatProperty(
|
|
|
|
name="Dirt Angle",
|
2012-03-17 14:27:46 +00:00
|
|
|
description="Less than 90 limits the angle used in the tonal range",
|
2013-01-03 07:01:41 +00:00
|
|
|
min=0.0, max=pi,
|
2011-08-19 19:25:20 +00:00
|
|
|
default=0.0,
|
2013-01-03 07:01:41 +00:00
|
|
|
unit="ROTATION",
|
2011-08-19 19:25:20 +00:00
|
|
|
)
|
|
|
|
dirt_only = BoolProperty(
|
|
|
|
name="Dirt Only",
|
2012-03-17 14:27:46 +00:00
|
|
|
description="Don't calculate cleans for convex areas",
|
2011-08-19 19:25:20 +00:00
|
|
|
default=False,
|
|
|
|
)
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2013-01-03 07:01:41 +00:00
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
|
|
|
obj = context.object
|
|
|
|
return (obj and obj.type == 'MESH')
|
|
|
|
|
2009-11-02 09:26:55 +00:00
|
|
|
def execute(self, context):
|
2011-02-27 14:16:32 +00:00
|
|
|
import time
|
2009-11-02 09:26:55 +00:00
|
|
|
|
2013-01-03 07:01:41 +00:00
|
|
|
obj = context.object
|
2009-12-26 09:36:50 +00:00
|
|
|
mesh = obj.data
|
2009-11-02 09:26:55 +00:00
|
|
|
|
|
|
|
t = time.time()
|
|
|
|
|
2013-01-03 07:01:41 +00:00
|
|
|
ret = applyVertexDirt(mesh, self.blur_iterations, self.blur_strength, self.dirt_angle, self.clean_angle, self.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
|
|
|
|
2012-04-13 08:41:30 +00:00
|
|
|
return ret
|