Paste Code
Paste Blends
Paste Images
# Originally made by Tamito KAJIYAMA <19 August 2009>
# modified by mato <25 March 2010>



from freestyle_init import *

from PredicatesU1D import *
from logical_operators import *

from shaders import *

import bpy, os



_HEADER = """\

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"

"http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">

<svg xml:space="default" width="%d" height="%d">

"""

_PATH = """\

<path fill="none" stroke="%s" stroke-width="%d" d="M %s" />

"""

_FOOTER = """\

</svg>

"""


class pyGlobalZInspector(UnaryPredicate1D):
"""Inspect the minimum and maximum Z depth values of all vertices
in the scene."""
z_min, z_max = 1e+8, -1e+8
def __call__(self, inter):
it = inter.verticesBegin()
while it.isEnd() == 0:
z = it.getObject().getProjectedZ()
if z < pyGlobalZInspector.z_min:
pyGlobalZInspector.z_min = z
if z > pyGlobalZInspector.z_max:
pyGlobalZInspector.z_max = z
it.increment()
return 1

class pyGlobalZDependingThicknessShader(StrokeShader):
"""Give a variable thickness to a stroke depending on Z depth
values of the vertices in the stroke. The thickness at a vertex
is proportional to the vertex's relative Z depth (0.0 to 1.0) in
the range of global minimum and maximum Z depth values examined by
pyGlobalZInspector."""
def __init__(self, min_thickness, max_thickness):
StrokeShader.__init__(self)
self.__min = min_thickness
self.__max = max_thickness
def shade(self, stroke):
global_z_min = pyGlobalZInspector.z_min
global_z_max = pyGlobalZInspector.z_max
z_diff = 1.0 / (global_z_max - global_z_min)
z_min, z_max = 1e+8, -1e+8
it = stroke.strokeVerticesBegin()
while it.isEnd() == 0:
v = it.getObject()
z = (v.getProjectedZ() - global_z_min) * z_diff
thickness = (1.0 - z) * self.__max + z * self.__min
v.attribute().setThickness(thickness/2, thickness/2)
it.increment()

class pyTestBP1D(BinaryPredicate1D):
"""True if both operands have the same shape ID and both of them
are either external contours or non-external contours (i.e. unless
one operand is an external contour and the other is not)."""
def __init__(self):
BinaryPredicate1D.__init__(self)
self.__bpred = SameShapeIdBP1D()
self.__upred = ExternalContourUP1D()
def __call__(self, inter1, inter2):
return self.__bpred(inter1, inter2) and \
self.__upred(inter1) == self.__upred(inter2)


class SVGWriter(StrokeShader):

def __init__(self, f, w, h, start_frame, current_frame, frame_step, fps):

StrokeShader.__init__(self)

self.width, self.height = w, h

self.file = f

begin = float(current_frame - start_frame) / frame_step / fps

dur = 1.0 / fps

# self.file.write(_HEADER % (current_frame, begin, dur))

self.file.write(_HEADER % (w, h))

def close(self):

self.file.write(_FOOTER)

self.file.close()

def shade(self, stroke):

points = []

for v in stroke:

x, y = v.getPoint2D()

points.append("%.3f,%.3f" % (x, self.height - y))

points = " ".join(points)

attr = v.attribute()

r, g, b = attr.getColorRGB() * 255

color = "#%02x%02x%02x" % (r, g, b)

width = attr.getThicknessRL()

width = width[0] + width[1]

self.file.write(_PATH % (color, width, points))


scene = bpy.data.scenes[0]

render_data = scene.render

start_frame = scene.start_frame

current_frame = scene.current_frame

frame_step = scene.frame_step

fps = render_data.fps

if not os.path.exists(render_data.output_path):

os.makedirs(render_data.output_path)

path = os.path.join(render_data.output_path, "frame%04d.svg" % current_frame)

f = open(path, "wt")

w = render_data.resolution_x

h = render_data.resolution_y



upred = QuantitativeInvisibilityUP1D(0)

bpred = pyTestBP1D()
Operators.select(upred)

Operators.select(pyGlobalZInspector())
Operators.bidirectionalChain(ChainPredicateIterator(upred, bpred),
NotUP1D(upred))
writer = SVGWriter(f, w, h, start_frame, current_frame, frame_step, fps)

shaders_list = [

pyGlobalZDependingThicknessShader(.1, 5),
#ConstantThicknessShader(3),

#pyDepthDiscontinuityThicknessShader(1, 4),

ConstantColorShader(0, 0, 0),

#pyMaterialColorShader(0.5),

#pyDiffuseColorShader(),

writer,

]

Operators.create(TrueUP1D(), shaders_list)

writer.close()
  1. # Originally made by Tamito KAJIYAMA <19 August 2009>
  2. # modified by mato <25 March 2010>
  3.  
  4.  
  5.  
  6. from freestyle_init import *
  7.  
  8. from PredicatesU1D import *
  9. from logical_operators import *
  10.  
  11. from shaders import *
  12.  
  13. import bpy, os
  14.  
  15.  
  16.  
  17. _HEADER = """\
  18.  
  19. <?xml version="1.0" encoding="utf-8"?>
  20.  
  21. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  22.  
  23. "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">
  24.  
  25. <svg xml:space="default" width="%d" height="%d">
  26.  
  27. """
  28.  
  29. _PATH = """\
  30.  
  31. <path fill="none" stroke="%s" stroke-width="%d" d="M %s" />
  32.  
  33. """
  34.  
  35. _FOOTER = """\
  36.  
  37. </svg>
  38.  
  39. """
  40.  
  41.  
  42. class pyGlobalZInspector(UnaryPredicate1D):
  43.     """Inspect the minimum and maximum Z depth values of all vertices
  44.    in the scene."""
  45.     z_min, z_max = 1e+8, -1e+8
  46.     def __call__(self, inter):
  47.         it = inter.verticesBegin()
  48.         while it.isEnd() == 0:
  49.             z = it.getObject().getProjectedZ()
  50.             if z < pyGlobalZInspector.z_min:
  51.                 pyGlobalZInspector.z_min = z
  52.             if z > pyGlobalZInspector.z_max:
  53.                 pyGlobalZInspector.z_max = z
  54.             it.increment()
  55.         return 1
  56.  
  57. class pyGlobalZDependingThicknessShader(StrokeShader):
  58.     """Give a variable thickness to a stroke depending on Z depth
  59.    values of the vertices in the stroke.  The thickness at a vertex
  60.    is proportional to the vertex's relative Z depth (0.0 to 1.0) in
  61.    the range of global minimum and maximum Z depth values examined by
  62.    pyGlobalZInspector."""
  63.     def __init__(self, min_thickness, max_thickness):
  64.         StrokeShader.__init__(self)
  65.         self.__min = min_thickness
  66.         self.__max = max_thickness
  67.     def shade(self, stroke):
  68.         global_z_min = pyGlobalZInspector.z_min
  69.         global_z_max = pyGlobalZInspector.z_max
  70.         z_diff = 1.0 / (global_z_max - global_z_min)
  71.         z_min, z_max = 1e+8, -1e+8
  72.         it = stroke.strokeVerticesBegin()
  73.         while it.isEnd() == 0:
  74.             v = it.getObject()
  75.             z = (v.getProjectedZ() - global_z_min) * z_diff
  76.             thickness = (1.0 - z) * self.__max + z * self.__min
  77.             v.attribute().setThickness(thickness/2, thickness/2)
  78.             it.increment()
  79.  
  80. class pyTestBP1D(BinaryPredicate1D):
  81.     """True if both operands have the same shape ID and both of them
  82.    are either external contours or non-external contours (i.e. unless
  83.    one operand is an external contour and the other is not)."""
  84.     def __init__(self):
  85.         BinaryPredicate1D.__init__(self)
  86.         self.__bpred = SameShapeIdBP1D()
  87.         self.__upred = ExternalContourUP1D()
  88.     def __call__(self, inter1, inter2):
  89.         return self.__bpred(inter1, inter2) and \
  90.                self.__upred(inter1) == self.__upred(inter2)
  91.  
  92.  
  93. class SVGWriter(StrokeShader):
  94.  
  95.         def __init__(self, f, w, h, start_frame, current_frame, frame_step, fps):
  96.  
  97.                 StrokeShader.__init__(self)
  98.  
  99.                 self.width, self.height = w, h
  100.  
  101.                 self.file = f
  102.  
  103.                 begin = float(current_frame - start_frame) / frame_step / fps
  104.  
  105.                 dur = 1.0 / fps
  106.  
  107. #               self.file.write(_HEADER % (current_frame, begin, dur))
  108.  
  109.                 self.file.write(_HEADER % (w, h))
  110.  
  111.         def close(self):
  112.  
  113.                 self.file.write(_FOOTER)
  114.  
  115.                 self.file.close()
  116.  
  117.         def shade(self, stroke):
  118.  
  119.                 points = []
  120.  
  121.                 for v in stroke:
  122.  
  123.                         x, y = v.getPoint2D()
  124.  
  125.                         points.append("%.3f,%.3f" % (x, self.height - y))
  126.  
  127.                 points = " ".join(points)
  128.  
  129.                 attr = v.attribute()
  130.  
  131.                 r, g, b = attr.getColorRGB() * 255
  132.  
  133.                 color = "#%02x%02x%02x" % (r, g, b)
  134.  
  135.                 width = attr.getThicknessRL()
  136.  
  137.                 width = width[0] + width[1]
  138.  
  139.                 self.file.write(_PATH % (color, width, points))
  140.  
  141.  
  142. scene = bpy.data.scenes[0]
  143.  
  144. render_data = scene.render
  145.  
  146. start_frame = scene.start_frame
  147.  
  148. current_frame = scene.current_frame
  149.  
  150. frame_step = scene.frame_step
  151.  
  152. fps = render_data.fps
  153.  
  154. if not os.path.exists(render_data.output_path):
  155.  
  156.         os.makedirs(render_data.output_path)
  157.  
  158. path = os.path.join(render_data.output_path, "frame%04d.svg" % current_frame)
  159.  
  160. f = open(path, "wt")
  161.  
  162. w = render_data.resolution_x
  163.  
  164. h = render_data.resolution_y
  165.  
  166.  
  167.  
  168. upred = QuantitativeInvisibilityUP1D(0)
  169.  
  170. bpred = pyTestBP1D()
  171. Operators.select(upred)
  172.  
  173. Operators.select(pyGlobalZInspector())
  174. Operators.bidirectionalChain(ChainPredicateIterator(upred, bpred),
  175.                              NotUP1D(upred))
  176. writer = SVGWriter(f, w, h, start_frame, current_frame, frame_step, fps)
  177.  
  178. shaders_list = [
  179.  
  180.         pyGlobalZDependingThicknessShader(.1, 5),
  181.         #ConstantThicknessShader(3),
  182.  
  183.         #pyDepthDiscontinuityThicknessShader(1, 4),
  184.  
  185.         ConstantColorShader(0, 0, 0),
  186.  
  187.         #pyMaterialColorShader(0.5),
  188.  
  189.         #pyDiffuseColorShader(),
  190.  
  191.         writer,
  192.  
  193. ]
  194.  
  195. Operators.create(TrueUP1D(), shaders_list)
  196.  
  197. writer.close()
  198.  
go to heaven