1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
import Freestyle
from freestyle_init import *
from logical_operators import *
from ChainingIterators import *
from shaders import *
def blend_curve(blend_type, v1, fac, v2):
facm = 1.0 - fac
if blend_type == "MIX":
v1 = facm * v1 + fac * v2
elif blend_type == "ADD":
v1 += fac * v2
elif blend_type == "MULTIPLY":
v1 *= facm + fac * v2;
elif blend_type == "SUBTRACT":
v1 -= fac * v2
elif blend_type == "DIVIDE":
if v2 != 0.0:
v1 = facm * v1 + fac * v1 / v2
elif blend_type == "DIFFERENCE":
v1 = facm * v1 + fac * abs(v1 - v2)
elif blend_type == "MININUM":
tmp = fac * v1
if v1 > tmp:
v1 = tmp
elif blend_type == "MAXIMUM":
tmp = fac * v1
if v1 < tmp:
v1 = tmp
else:
raise ValueError("unknown curve blend type: " + blend_type)
return v1
class ColorAlongStrokeShader(StrokeShader):
def __init__(self, blend, influence, ramp):
StrokeShader.__init__(self)
self.__blend = blend
self.__influence = influence
self.__ramp = ramp
def getName(self):
return "ColorAlongStrokeShader"
def shade(self, stroke):
distance, total = 0.0, stroke.getLength2D()
it = stroke.strokeVerticesBegin()
while not it.isEnd():
sv = it.getObject()
p = sv.getPoint()
if not it.isBegin():
distance += (prev - p).length
prev = p
t = min(distance / total, 1.0)
b = Freestyle.evaluateColorRamp(self.__ramp, t)
b = b.xyz # omit alpha
a = sv.attribute().getColorRGB()
c = Freestyle.blendRamp(self.__blend, a, self.__influence, b)
sv.attribute().setColor(c)
it.increment()
class AlphaAlongStrokeShader(StrokeShader):
def __init__(self, blend, influence, mapping, invert, curve):
StrokeShader.__init__(self)
self.__blend = blend
self.__influence = influence
assert mapping in ("LINEAR", "CURVE")
self.__mapping = getattr(self, mapping)
self.__invert = invert
self.__curve = curve
def getName(self):
return "AlphaAlongStrokeShader"
def LINEAR(self, t):
if self.__invert:
return 1.0 - t
return t
def CURVE(self, t):
return Freestyle.evaluateCurveMappingF(self.__curve, 0, t)
def shade(self, stroke):
distance, total = 0.0, stroke.getLength2D()
it = stroke.strokeVerticesBegin()
while not it.isEnd():
sv = it.getObject()
p = sv.getPoint()
if not it.isBegin():
distance += (prev - p).length
prev = p
t = min(distance / total, 1.0)
b = self.__mapping(t)
a = sv.attribute().getAlpha()
c = blend_curve(self.__blend, a, self.__influence, b)
sv.attribute().setAlpha(c)
it.increment()
class ThicknessAlongStrokeShader(StrokeShader):
def __init__(self, blend, influence, mapping, invert, curve, value_min, value_max):
StrokeShader.__init__(self)
self.__blend = blend
self.__influence = influence
assert mapping in ("LINEAR", "CURVE")
self.__mapping = getattr(self, mapping)
self.__invert = invert
self.__curve = curve
self.__value_min = value_min
self.__value_max = value_max
def getName(self):
return "ThicknessAlongStrokeShader"
def LINEAR(self, t):
if self.__invert:
return 1.0 - t
return t
def CURVE(self, t):
return Freestyle.evaluateCurveMappingF(self.__curve, 0, t)
def shade(self, stroke):
distance, total = 0.0, stroke.getLength2D()
it = stroke.strokeVerticesBegin()
while not it.isEnd():
sv = it.getObject()
p = sv.getPoint()
if not it.isBegin():
distance += (prev - p).length
prev = p
t = min(distance / total, 1.0)
t = self.__mapping(t)
b = self.__value_min + t * (self.__value_max - self.__value_min)
a = sv.attribute().getThicknessRL()
a = a[0] + a[1]
c = blend_curve(self.__blend, a, self.__influence, b)
sv.attribute().setThickness(c/2, c/2)
it.increment()
class QuantitativeInvisibilityRangeUP1D(UnaryPredicate1D):
def __init__(self, qi_start, qi_end):
UnaryPredicate1D.__init__(self)
self.__getQI = QuantitativeInvisibilityF1D()
self.__qi_start = qi_start
self.__qi_end = qi_end
def getName(self):
return "QuantitativeInvisibilityRangeUP1D"
def __call__(self, inter):
qi = self.__getQI(inter)
return self.__qi_start <= qi <= self.__qi_end
def join_unary_predicates(upred_list, bpred):
if not upred_list:
return TrueUP1D()
upred = upred_list[0]
for p in upred_list[1:]:
upred = bpred(upred, p)
return upred
def process(layer_name, lineset_name):
scene = Freestyle.getCurrentScene()
layer = scene.render.layers[layer_name]
lineset = layer.freestyle_settings.linesets[lineset_name]
linestyle = lineset.linestyle
selection_criteria = []
# prepare selection criteria by visibility
if lineset.select_by_visibility:
if lineset.visibility == "VISIBLE":
selection_criteria.append(
QuantitativeInvisibilityUP1D(0))
elif lineset.visibility == "HIDDEN":
selection_criteria.append(
NotUP1D(QuantitativeInvisibilityUP1D(0)))
elif lineset.visibility == "RANGE":
selection_criteria.append(
QuantitativeInvisibilityRangeUP1D(lineset.qi_start, lineset.qi_end))
# prepare selection criteria by edge types
if lineset.select_by_edge_types:
edge_type_criteria = []
if lineset.edge_type_combination == "OR":
flags = Nature.NO_FEATURE
if lineset.select_silhouette:
flags |= Nature.SILHOUETTE
if lineset.select_border:
flags |= Nature.BORDER
if lineset.select_crease:
flags |= Nature.CREASE
if lineset.select_ridge:
flags |= Nature.RIDGE
if lineset.select_valley:
flags |= Nature.VALLEY
if lineset.select_suggestive_contour:
flags |= Nature.SUGGESTIVE_CONTOUR
if lineset.select_material_boundary:
flags |= Nature.MATERIAL_BOUNDARY
if flags != Nature.NO_FEATURE:
edge_type_criteria.append(pyNatureUP1D(flags))
else:
if lineset.select_silhouette:
edge_type_criteria.append(pyNatureUP1D(Nature.SILHOUETTE))
if lineset.select_border:
edge_type_criteria.append(pyNatureUP1D(Nature.BORDER))
if lineset.select_crease:
edge_type_criteria.append(pyNatureUP1D(Nature.CREASE))
if lineset.select_ridge:
edge_type_criteria.append(pyNatureUP1D(Nature.RIDGE))
if lineset.select_valley:
edge_type_criteria.append(pyNatureUP1D(Nature.VALLEY))
if lineset.select_suggestive_contour:
edge_type_criteria.append(pyNatureUP1D(Nature.SUGGESTIVE_CONTOUR))
if lineset.select_material_boundary:
edge_type_criteria.append(pyNatureUP1D(Nature.MATERIAL_BOUNDARY))
if lineset.select_contour:
edge_type_criteria.append(ContourUP1D())
if lineset.select_external_contour:
edge_type_criteria.append(ExternalContourUP1D())
if lineset.edge_type_combination == "OR":
upred = join_unary_predicates(edge_type_criteria, OrUP1D)
else:
upred = join_unary_predicates(edge_type_criteria, AndUP1D)
if upred is not None:
if lineset.edge_type_negation == "EXCLUSIVE":
upred = NotUP1D(upred)
selection_criteria.append(upred)
# do feature edge selection
upred = join_unary_predicates(selection_criteria, AndUP1D)
if upred is None:
upred = TrueUP1D()
Operators.select(upred)
# join feature edges
Operators.bidirectionalChain(ChainSilhouetteIterator(), NotUP1D(upred)) # FIXME
# prepare a list of stroke shaders
color = linestyle.color
shaders_list = [
SamplingShader(5.0),
ConstantThicknessShader(linestyle.thickness),
ConstantColorShader(color.r, color.g, color.b, linestyle.alpha)]
for m in linestyle.color_modifiers:
if not m.enabled:
continue
if m.type == "ALONG_STROKE":
shaders_list.append(
ColorAlongStrokeShader(m.blend, m.influence, m.color_ramp))
for m in linestyle.alpha_modifiers:
if not m.enabled:
continue
if m.type == "ALONG_STROKE":
shaders_list.append(
AlphaAlongStrokeShader(m.blend, m.influence, m.mapping, m.invert, m.curve))
for m in linestyle.thickness_modifiers:
if not m.enabled:
continue
if m.type == "ALONG_STROKE":
shaders_list.append(
ThicknessAlongStrokeShader(m.blend, m.influence, m.mapping, m.invert, m.curve,
m.value_min, m.value_max))
# create strokes using the shaders list
Operators.create(TrueUP1D(), shaders_list)
|