Advertisement
Guest User

SVGWriter

a guest
Oct 8th, 2012
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.93 KB | None | 0 0
  1. # Tamito KAJIYAMA <19 August 2009>
  2.  
  3. from freestyle_init import *
  4. from logical_operators import *
  5. from shaders import *
  6. import bpy
  7. import os
  8.  
  9. _HEADER = """\
  10. <?xml version="1.0" encoding="utf-8"?>
  11. <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
  12. "http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd">
  13. <svg xml:space="default" width="%d" height="%d">
  14. """
  15. _PATH = """\
  16. <path fill="none" stroke="%s" stroke-width="%d" d="M %s" />
  17. """
  18. _FOOTER = """\
  19. </svg>
  20. """
  21.  
  22. class SVGWriter(StrokeShader):
  23.     def __init__(self, f, w, h):
  24.         StrokeShader.__init__(self)
  25.         self.width, self.height = w, h
  26.         self.file = f
  27.         self.file.write(_HEADER % (w, h))
  28.     def close(self):
  29.         self.file.write(_FOOTER)
  30.         self.file.close()
  31.     def shade(self, stroke):
  32.         points = []
  33.         for v in stroke:
  34.             x, y = v.getPoint2D()
  35.             points.append("%.3f,%.3f" % (x, self.height - y))
  36.         points = " ".join(points)
  37.         attr = v.attribute()
  38.         r, g, b = attr.getColorRGB() * 255
  39.         color = "#%02x%02x%02x" % (r, g, b)
  40.         width = attr.getThicknessRL()
  41.         width = width[0] + width[1]
  42.         self.file.write(_PATH % (color, width, points))
  43.  
  44. import Freestyle
  45. scene = Freestyle.getCurrentScene()
  46. start_frame = scene.frame_start
  47. current_frame = scene.frame_current
  48. frame_step = scene.frame_step
  49. fps = scene.render.fps
  50. output_dir = bpy.path.abspath(scene.render.filepath)
  51. if not os.path.exists(output_dir):
  52.     os.makedirs(output_dir)
  53. path = os.path.join(output_dir, "output%06d.svg" % current_frame)
  54. f = open(path, "wt")
  55. w = scene.render.resolution_x
  56. h = scene.render.resolution_y
  57.  
  58. upred = QuantitativeInvisibilityUP1D(0)
  59. Operators.select(upred)
  60. Operators.bidirectionalChain(ChainSilhouetteIterator(), NotUP1D(upred))
  61. writer = SVGWriter(f, w, h)
  62. shaders_list = [
  63.     #ConstantThicknessShader(0.2),
  64.     pyDepthDiscontinuityThicknessShader(1, 4),
  65.     ConstantColorShader(0, 0, 0),
  66.     #pyMaterialColorShader(0.5),
  67.     writer,
  68. ]
  69. Operators.create(TrueUP1D(), shaders_list)
  70. writer.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement