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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
# SPDX-License-Identifier: GPL-2.0-or-later
"""
This module contains chaining iterators used for the chaining
operation to construct long strokes by concatenating feature edges
according to selected chaining rules. The module is also intended to
be a collection of examples for defining chaining iterators in Python.
"""
__all__ = (
"ChainPredicateIterator",
"ChainSilhouetteIterator",
"pyChainSilhouetteIterator",
"pyChainSilhouetteGenericIterator",
"pyExternalContourChainingIterator",
"pySketchyChainSilhouetteIterator",
"pySketchyChainingIterator",
"pyFillOcclusionsRelativeChainingIterator",
"pyFillOcclusionsAbsoluteChainingIterator",
"pyFillOcclusionsAbsoluteAndRelativeChainingIterator",
"pyFillQi0AbsoluteAndRelativeChainingIterator",
"pyNoIdChainSilhouetteIterator",
)
# module members
from _freestyle import (
ChainPredicateIterator,
ChainSilhouetteIterator,
)
# constructs for predicate definition in Python
from freestyle.types import (
AdjacencyIterator,
ChainingIterator,
Nature,
TVertex,
)
from freestyle.predicates import (
ExternalContourUP1D,
)
from freestyle.utils import (
ContextFunctions as CF,
get_chain_length,
find_matching_vertex,
)
import bpy
NATURES = (
Nature.SILHOUETTE,
Nature.BORDER,
Nature.CREASE,
Nature.MATERIAL_BOUNDARY,
Nature.EDGE_MARK,
Nature.SUGGESTIVE_CONTOUR,
Nature.VALLEY,
Nature.RIDGE
)
def nature_in_preceding(nature, index):
"""Returns True if given nature appears before index, else False."""
return any(nature & nat for nat in NATURES[:index])
class pyChainSilhouetteIterator(ChainingIterator):
"""
Natural chaining iterator that follows the edges of the same nature
following the topology of objects, with decreasing priority for
silhouettes, then borders, then suggestive contours, then all other edge
types. A ViewEdge is only chained once.
"""
def __init__(self, stayInSelection=True):
ChainingIterator.__init__(self, stayInSelection, True, None, True)
def init(self):
pass
def traverse(self, iter):
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
return find_matching_vertex(mate.id, it)
# case of NonTVertex
winner = None
for i, nat in enumerate(NATURES):
if (nat & self.current_edge.nature):
for ve in it:
ve_nat = ve.nature
if (ve_nat & nat):
# search for matches in previous natures. if match -> break
if nat != ve_nat and nature_in_preceding(ve_nat, index=i):
break
# a second match must be an error
if winner is not None:
return None
# assign winner
winner = ve
return winner
class pyChainSilhouetteGenericIterator(ChainingIterator):
"""
Natural chaining iterator that follows the edges of the same nature
following the topology of objects, with decreasing priority for
silhouettes, then borders, then suggestive contours, then all other
edge types.
.. method:: __init__(self, stayInSelection=True, stayInUnvisited=True)
Builds a pyChainSilhouetteGenericIterator object.
:arg stayInSelection: True if it is allowed to go out of the selection
:type stayInSelection: bool
:arg stayInUnvisited: May the same ViewEdge be chained twice
:type stayInUnvisited: bool
"""
def __init__(self, stayInSelection=True, stayInUnvisited=True):
ChainingIterator.__init__(self, stayInSelection, stayInUnvisited, None, True)
def init(self):
pass
def traverse(self, iter):
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
return find_matching_vertex(mate.id, it)
# case of NonTVertex
winner = None
for i, nat in enumerate(NATURES):
if (nat & self.current_edge.nature):
for ve in it:
ve_nat = ve.nature
if ve.id == self.current_edge.id:
continue
if (ve_nat & nat):
if nat != ve_nat and nature_in_preceding(ve_nat, index=i):
break
if winner is not None:
return None
winner = ve
return winner
return None
class pyExternalContourChainingIterator(ChainingIterator):
"""Chains by external contour"""
def __init__(self):
ChainingIterator.__init__(self, False, True, None, True)
self.ExternalContour = ExternalContourUP1D()
def init(self):
self._nEdges = 0
def checkViewEdge(self, ve, orientation):
vertex = (ve.first_viewvertex if orientation else
ve.last_viewvertex)
it = AdjacencyIterator(vertex, True, True)
result = any(self.ExternalContour(ave) for ave in it)
# report if there is no result (that's bad)
if not result and bpy.app.debug_freestyle:
print("pyExternalContourChainingIterator : didn't find next edge")
return result
def traverse(self, iter):
winner = None
self._nEdges += 1
it = AdjacencyIterator(iter)
time_stamp = CF.get_time_stamp()
for ve in it:
if self.ExternalContour(ve) and ve.time_stamp == time_stamp:
winner = ve
if winner is None:
it = AdjacencyIterator(iter)
for ve in it:
if self.checkViewEdge(ve, not it.is_incoming):
winner = ve
return winner
class pySketchyChainSilhouetteIterator(ChainingIterator):
"""
Natural chaining iterator with a sketchy multiple touch. It chains the
same ViewEdge multiple times to achieve a sketchy effect.
.. method:: __init__(self, nRounds=3,stayInSelection=True)
Builds a pySketchyChainSilhouetteIterator object.
:arg nRounds: Number of times every Viewedge is chained.
:type nRounds: int
:arg stayInSelection: if False, edges outside of the selection can be chained.
:type stayInSelection: bool
"""
def __init__(self, nRounds=3, stayInSelection=True):
ChainingIterator.__init__(self, stayInSelection, False, None, True)
self._timeStamp = CF.get_time_stamp() + nRounds
self._nRounds = nRounds
def init(self):
self._timeStamp = CF.get_time_stamp() + self._nRounds
# keeping this local saves passing a reference to 'self' around
def make_sketchy(self, ve):
"""
Creates the skeychy effect by causing the chain to run from
the start again. (loop over itself again)
"""
if ve is None:
ve = self.current_edge
if ve.chaining_time_stamp == self._timeStamp:
return None
return ve
def traverse(self, iter):
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
return self.make_sketchy(find_matching_vertex(mate.id, it))
# case of NonTVertex
winner = None
for i, nat in enumerate(NATURES):
if (nat & self.current_edge.nature):
for ve in it:
if ve.id == self.current_edge.id:
continue
ve_nat = ve.nature
if (ve_nat & nat):
if nat != ve_nat and nature_in_preceding(ve_nat, i):
break
if winner is not None:
return self.make_sketchy(None)
winner = ve
break
return self.make_sketchy(winner)
class pySketchyChainingIterator(ChainingIterator):
"""
Chaining iterator designed for sketchy style. It chains the same
ViewEdge several times in order to produce multiple strokes per
ViewEdge.
"""
def __init__(self, nRounds=3, stayInSelection=True):
ChainingIterator.__init__(self, stayInSelection, False, None, True)
self._timeStamp = CF.get_time_stamp() + nRounds
self._nRounds = nRounds
self.t = False
def init(self):
self._timeStamp = CF.get_time_stamp() + self._nRounds
def traverse(self, iter):
winner = None
found = False
for ve in AdjacencyIterator(iter):
if self.current_edge.id == ve.id:
found = True
continue
winner = ve
if not found:
# This is a fatal error condition: self.current_edge must be found
# among the edges seen by the AdjacencyIterator [bug T35695].
if bpy.app.debug_freestyle:
print('pySketchyChainingIterator: current edge not found')
return None
if winner is None:
winner = self.current_edge
if winner.chaining_time_stamp == self._timeStamp:
return None
return winner
class pyFillOcclusionsRelativeChainingIterator(ChainingIterator):
"""
Chaining iterator that fills small occlusions
.. method:: __init__(self, percent)
Builds a pyFillOcclusionsRelativeChainingIterator object.
:arg percent: The maximal length of the occluded part, expressed
in a percentage of the total chain length.
:type percent: float
"""
def __init__(self, percent):
ChainingIterator.__init__(self, False, True, None, True)
self._length = 0.0
self._percent = float(percent)
self.timestamp = CF.get_time_stamp()
def init(self):
# A chain's length should preferably be evaluated only once.
# Therefore, the chain length is reset here.
self._length = 0.0
def traverse(self, iter):
winner = None
winnerOrientation = False
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
winner = find_matching_vertex(mate.id, it)
winnerOrientation = not it.is_incoming if not it.is_end else False
# case of NonTVertex
else:
for nat in NATURES:
if (self.current_edge.nature & nat):
for ve in it:
if (ve.nature & nat):
if winner is not None:
return None
winner = ve
winnerOrientation = not it.is_incoming
break
# check timestamp to see if this edge was part of the selection
if winner is not None and winner.time_stamp != self.timestamp:
# if the edge wasn't part of the selection, let's see
# whether it's short enough (with respect to self.percent)
# to be included.
if self._length == 0.0:
self._length = get_chain_length(winner, winnerOrientation)
# check if the gap can be bridged
connexl = 0.0
_cit = pyChainSilhouetteGenericIterator(False, False)
_cit.begin = winner
_cit.current_edge = winner
_cit.orientation = winnerOrientation
_cit.init()
while (not _cit.is_end) and _cit.object.time_stamp != self.timestamp:
connexl += _cit.object.length_2d
_cit.increment()
if _cit.is_begin:
break
if connexl > self._percent * self._length:
return None
return winner
class pyFillOcclusionsAbsoluteChainingIterator(ChainingIterator):
"""
Chaining iterator that fills small occlusions
.. method:: __init__(self, length)
Builds a pyFillOcclusionsAbsoluteChainingIterator object.
:arg length: The maximum length of the occluded part in pixels.
:type length: int
"""
def __init__(self, length):
ChainingIterator.__init__(self, False, True, None, True)
self._length = float(length)
self.timestamp = CF.get_time_stamp()
def init(self):
pass
def traverse(self, iter):
winner = None
winnerOrientation = False
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
winner = find_matching_vertex(mate.id, it)
winnerOrientation = not it.is_incoming if not it.is_end else False
# case of NonTVertex
else:
for nat in NATURES:
if (self.current_edge.nature & nat):
for ve in it:
if (ve.nature & nat):
if winner is not None:
return None
winner = ve
winnerOrientation = not it.is_incoming
break
if winner is not None and winner.time_stamp != self.timestamp:
connexl = 0.0
_cit = pyChainSilhouetteGenericIterator(False, False)
_cit.begin = winner
_cit.current_edge = winner
_cit.orientation = winnerOrientation
_cit.init()
while (not _cit.is_end) and _cit.object.time_stamp != self.timestamp:
connexl += _cit.object.length_2d
_cit.increment()
if _cit.is_begin:
break
if connexl > self._length:
return None
return winner
class pyFillOcclusionsAbsoluteAndRelativeChainingIterator(ChainingIterator):
"""
Chaining iterator that fills small occlusions regardless of the
selection.
.. method:: __init__(self, percent, l)
Builds a pyFillOcclusionsAbsoluteAndRelativeChainingIterator object.
:arg percent: The maximal length of the occluded part as a
percentage of the total chain length.
:type percent: float
:arg l: Absolute length.
:type l: float
"""
def __init__(self, percent, l):
ChainingIterator.__init__(self, False, True, None, True)
self._length = 0.0
self._absLength = l
self._percent = float(percent)
def init(self):
# each time we're evaluating a chain length
# we try to do it once. Thus we reinit
# the chain length here:
self._length = 0.0
def traverse(self, iter):
winner = None
winnerOrientation = False
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
winner = find_matching_vertex(mate.id, it)
winnerOrientation = not it.is_incoming if not it.is_end else False
# case of NonTVertex
else:
for nat in NATURES:
if (self.current_edge.nature & nat):
for ve in it:
if (ve.nature & nat):
if winner is not None:
return None
winner = ve
winnerOrientation = not it.is_incoming
break
if winner is not None and winner.time_stamp != CF.get_time_stamp():
if self._length == 0.0:
self._length = get_chain_length(winner, winnerOrientation)
connexl = 0.0
_cit = pyChainSilhouetteGenericIterator(False, False)
_cit.begin = winner
_cit.current_edge = winner
_cit.orientation = winnerOrientation
_cit.init()
while (not _cit.is_end) and _cit.object.time_stamp != CF.get_time_stamp():
connexl += _cit.object.length_2d
_cit.increment()
if _cit.is_begin:
break
if (connexl > self._percent * self._length) or (connexl > self._absLength):
return None
return winner
class pyFillQi0AbsoluteAndRelativeChainingIterator(ChainingIterator):
"""
Chaining iterator that fills small occlusions regardless of the
selection.
.. method:: __init__(self, percent, l)
Builds a pyFillQi0AbsoluteAndRelativeChainingIterator object.
:arg percent: The maximal length of the occluded part as a
percentage of the total chain length.
:type percent: float
:arg l: Absolute length.
:type l: float
"""
def __init__(self, percent, l):
ChainingIterator.__init__(self, False, True, None, True)
self._length = 0.0
self._absLength = l
self._percent = percent
def init(self):
# A chain's length should preverably be evaluated only once.
# Therefore, the chain length is reset here.
self._length = 0.0
def traverse(self, iter):
winner = None
winnerOrientation = False
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
mate = vertex.get_mate(self.current_edge)
winner = find_matching_vertex(mate.id, it)
winnerOrientation = not it.is_incoming if not it.is_end else False
# case of NonTVertex
else:
for nat in NATURES:
if (self.current_edge.nature & nat):
for ve in it:
if (ve.nature & nat):
if winner is not None:
return None
winner = ve
winnerOrientation = not it.is_incoming
break
if winner is not None and winner.qi:
if self._length == 0.0:
self._length = get_chain_length(winner, winnerOrientation)
connexl = 0
_cit = pyChainSilhouetteGenericIterator(False, False)
_cit.begin = winner
_cit.current_edge = winner
_cit.orientation = winnerOrientation
_cit.init()
while (not _cit.is_end) and _cit.object.qi != 0:
connexl += _cit.object.length_2d
_cit.increment()
if _cit.is_begin:
break
if (connexl > self._percent * self._length) or (connexl > self._absLength):
return None
return winner
class pyNoIdChainSilhouetteIterator(ChainingIterator):
"""
Natural chaining iterator that follows the edges of the same nature
following the topology of objects, with decreasing priority for
silhouettes, then borders, then suggestive contours, then all other edge
types. It won't chain the same ViewEdge twice.
.. method:: __init__(self, stayInSelection=True)
Builds a pyNoIdChainSilhouetteIterator object.
:arg stayInSelection: True if it is allowed to go out of the selection
:type stayInSelection: bool
"""
def __init__(self, stayInSelection=True):
ChainingIterator.__init__(self, stayInSelection, True, None, True)
def init(self):
pass
def traverse(self, iter):
winner = None
it = AdjacencyIterator(iter)
# case of TVertex
vertex = self.next_vertex
if type(vertex) is TVertex:
for ve in it:
# case one
vA = self.current_edge.last_fedge.second_svertex
vB = ve.first_fedge.first_svertex
if vA.id.first == vB.id.first:
return ve
# case two
vA = self.current_edge.first_fedge.first_svertex
vB = ve.last_fedge.second_svertex
if vA.id.first == vB.id.first:
return ve
# case three
vA = self.current_edge.last_fedge.second_svertex
vB = ve.last_fedge.second_svertex
if vA.id.first == vB.id.first:
return ve
# case four
vA = self.current_edge.first_fedge.first_svertex
vB = ve.first_fedge.first_svertex
if vA.id.first == vB.id.first:
return ve
return None
# case of NonTVertex
else:
for i, nat in enumerate(NATURES):
if (nat & self.current_edge.nature):
for ve in it:
ve_nat = ve.nature
if (ve_nat & nat):
if (nat != ve_nat) and any(n & ve_nat for n in NATURES[:i]):
break
if winner is not None:
return
winner = ve
return winner
return None
|