Freestyle: Py-Hooks for custom pre/post-processing line style

Patch D839, needed for SVG-render to be made into an addon.
This commit is contained in:
Tamito Kajiyama 2014-11-24 22:41:34 +01:00 committed by Campbell Barton
parent c35f563bb7
commit e3b68dbaf8

View File

@ -97,7 +97,7 @@ from freestyle.utils import (
stroke_normal, stroke_normal,
bound, bound,
pairwise, pairwise,
BoundedProperty BoundedProperty,
) )
from _freestyle import ( from _freestyle import (
blendRamp, blendRamp,
@ -110,6 +110,12 @@ from mathutils import Vector
from math import pi, sin, cos, acos, radians from math import pi, sin, cos, acos, radians
from itertools import cycle, tee from itertools import cycle, tee
# lists of callback functions
# WARNING: highly experimental, not a stable API
callbacks_lineset_pre = []
callbacks_modifiers_post = []
callbacks_lineset_post = []
class ColorRampModifier(StrokeShader): class ColorRampModifier(StrokeShader):
"""Primitive for the color modifiers.""" """Primitive for the color modifiers."""
@ -878,6 +884,21 @@ class Seed:
_seed = Seed() _seed = Seed()
def get_dashed_pattern(linestyle):
"""Extracts the dashed pattern from the various UI options """
pattern = []
if linestyle.dash1 > 0 and linestyle.gap1 > 0:
pattern.append(linestyle.dash1)
pattern.append(linestyle.gap1)
if linestyle.dash2 > 0 and linestyle.gap2 > 0:
pattern.append(linestyle.dash2)
pattern.append(linestyle.gap2)
if linestyle.dash3 > 0 and linestyle.gap3 > 0:
pattern.append(linestyle.dash3)
pattern.append(linestyle.gap3)
return pattern
integration_types = { integration_types = {
'MEAN': IntegrationType.MEAN, 'MEAN': IntegrationType.MEAN,
'MIN': IntegrationType.MIN, 'MIN': IntegrationType.MIN,
@ -887,13 +908,19 @@ integration_types = {
# main function for parameter processing # main function for parameter processing
def process(layer_name, lineset_name): def process(layer_name, lineset_name):
scene = getCurrentScene() scene = getCurrentScene()
layer = scene.render.layers[layer_name] layer = scene.render.layers[layer_name]
lineset = layer.freestyle_settings.linesets[lineset_name] lineset = layer.freestyle_settings.linesets[lineset_name]
linestyle = lineset.linestyle linestyle = lineset.linestyle
# execute line set pre-processing callback functions
for fn in callbacks_lineset_pre:
try:
fn(scene, layer, lineset)
except Exception as e:
print(e)
selection_criteria = [] selection_criteria = []
# prepare selection criteria by visibility # prepare selection criteria by visibility
if lineset.select_by_visibility: if lineset.select_by_visibility:
@ -1172,24 +1199,26 @@ def process(layer_name, lineset_name):
has_tex = True has_tex = True
if has_tex: if has_tex:
shaders_list.append(StrokeTextureStepShader(linestyle.texture_spacing)) shaders_list.append(StrokeTextureStepShader(linestyle.texture_spacing))
# execute post-base stylization callbacks
for fn in callbacks_modifiers_post:
shaders_list.extend(fn(scene, layer, lineset))
# -- Stroke caps -- # # -- Stroke caps -- #
if linestyle.caps == 'ROUND': if linestyle.caps == 'ROUND':
shaders_list.append(RoundCapShader()) shaders_list.append(RoundCapShader())
elif linestyle.caps == 'SQUARE': elif linestyle.caps == 'SQUARE':
shaders_list.append(SquareCapShader()) shaders_list.append(SquareCapShader())
# -- Dashed line -- # # -- Dashed line -- #
if linestyle.use_dashed_line: if linestyle.use_dashed_line:
pattern = [] pattern = get_dashed_pattern(linestyle)
if linestyle.dash1 > 0 and linestyle.gap1 > 0:
pattern.append(linestyle.dash1)
pattern.append(linestyle.gap1)
if linestyle.dash2 > 0 and linestyle.gap2 > 0:
pattern.append(linestyle.dash2)
pattern.append(linestyle.gap2)
if linestyle.dash3 > 0 and linestyle.gap3 > 0:
pattern.append(linestyle.dash3)
pattern.append(linestyle.gap3)
if len(pattern) > 0: if len(pattern) > 0:
shaders_list.append(DashedLineShader(pattern)) shaders_list.append(DashedLineShader(pattern))
# create strokes using the shaders list # create strokes using the shaders list
Operators.create(TrueUP1D(), shaders_list) Operators.create(TrueUP1D(), shaders_list)
# execute line set post-processing callback functions
for fn in callbacks_lineset_post:
fn(scene, layer, lineset)