[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1623] trunk/py/scripts/addons: Initial commit of SVG importer for Blender 2.5
Sergey Sharybin
g.ulairi at gmail.com
Sun Feb 20 22:13:13 CET 2011
Revision: 1623
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-extensions&revision=1623
Author: nazgul
Date: 2011-02-20 21:13:12 +0000 (Sun, 20 Feb 2011)
Log Message:
-----------
Initial commit of SVG importer for Blender 2.5
Only <path> is supported at this moment, other geometries would
be added a bit later. Transform attribute, defined, groups and
uses should work pretty fine.
Work is in progress, so please repoer issues to me, not to tracker :)
Added Paths:
-----------
trunk/py/scripts/addons/io_curve_svg/
trunk/py/scripts/addons/io_curve_svg/__init__.py
trunk/py/scripts/addons/io_curve_svg/import_svg.py
trunk/py/scripts/addons/io_curve_svg/svg_colors.py
Added: trunk/py/scripts/addons/io_curve_svg/__init__.py
===================================================================
--- trunk/py/scripts/addons/io_curve_svg/__init__.py (rev 0)
+++ trunk/py/scripts/addons/io_curve_svg/__init__.py 2011-02-20 21:13:12 UTC (rev 1623)
@@ -0,0 +1,83 @@
+# ##### 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 #####
+
+# <pep8 compliant>
+
+bl_info = {
+ "name": "Scalable Vector Graphics (SVG) 1.1 format",
+ "author": "Sergey Sharybin",
+ "blender": (2, 5, 6),
+ "api": 34996,
+ "location": "File > Import-Export",
+ "description": "Import SVG",
+ "warning": "",
+ "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.5/Py/"\
+ "Scripts/Import-Export/SVG",
+ "tracker_url": "",
+ "support": 'OFFICIAL',
+ "category": "Import-Export"}
+
+# To support reload properly, try to access a package var,
+# if it's there, reload everything
+if "bpy" in locals():
+ import imp
+ if "import_svg" in locals():
+ imp.reload(import_svg)
+
+
+import bpy
+from bpy.props import *
+from io_utils import ImportHelper, ExportHelper
+
+
+class ImportSVG(bpy.types.Operator, ImportHelper):
+ '''Load a SVG file'''
+ bl_idname = "import_curve.svg"
+ bl_label = "Import SVG"
+
+ filename_ext = ".svg"
+ filter_glob = StringProperty(default="*.svg", options={'HIDDEN'})
+
+ def execute(self, context):
+ from . import import_svg
+
+ return import_svg.load(self, context,
+ **self.as_keywords(ignore=("filter_glob",)))
+
+
+def menu_func_import(self, context):
+ self.layout.operator(ImportSVG.bl_idname,
+ text="Scalable Vector Graphics (.svg)")
+
+
+def register():
+ bpy.utils.register_module(__name__)
+
+ bpy.types.INFO_MT_file_import.append(menu_func_import)
+
+
+def unregister():
+ bpy.utils.unregister_module(__name__)
+
+ bpy.types.INFO_MT_file_import.remove(menu_func_import)
+
+# NOTES
+# - blender version is hardcoded
+
+if __name__ == "__main__":
+ register()
Added: trunk/py/scripts/addons/io_curve_svg/import_svg.py
===================================================================
--- trunk/py/scripts/addons/io_curve_svg/import_svg.py (rev 0)
+++ trunk/py/scripts/addons/io_curve_svg/import_svg.py 2011-02-20 21:13:12 UTC (rev 1623)
@@ -0,0 +1,1156 @@
+# ##### 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 #####
+
+# <pep8 compliant>
+
+import re
+import xml.dom.minidom
+from math import cos, sin, tan, atan2, pi, ceil
+
+import bpy
+from mathutils import Vector, Matrix
+
+from . import svg_colors
+
+#### Common utilities ####
+
+# TODO: 'em' and 'ex' aren't actually supported
+SVGUnits = {'': 1.0,
+ 'px': 1.0,
+ 'in': 90,
+ 'mm': 90 * 0.254,
+ 'cm': 90 * 2.54,
+ 'pt': 1.25,
+ 'pc': 15.0,
+ 'em': 1.0,
+ 'ex': 1.0}
+
+
+def SVGCreateCurve():
+ """
+ Create new curve object to hold splines in
+ """
+
+ cu = bpy.data.curves.new("Curve", 'CURVE')
+ obj = bpy.data.objects.new("Curve", cu)
+ bpy.context.scene.objects.link(obj)
+
+ return obj
+
+
+def SVGFinishCurve():
+ """
+ Finish curve creation
+ """
+
+ pass
+
+
+def SVGFlipHandle(x, y, x1, y1):
+ """
+ Flip handle around base point
+ """
+
+ x = x + (x - x1)
+ y = y + (y - y1)
+
+ return x, y
+
+
+def SVGParseCoord(coord, size):
+ """
+ Parse coordinate component to common basis
+
+ Needed to handle coordinates set in cm, mm, iches..
+ """
+
+ r = re.compile('([0-9\\-\\+\\.])([A-z%]*)')
+ val = float(r.sub('\\1', coord))
+ unit = r.sub('\\2', coord).lower()
+
+ if unit == '%':
+ return float(size) / 100.0 * val
+ else:
+ global SVGUnits
+
+ return val * SVGUnits[unit]
+
+ return val
+
+
+def SVGRectFromNode(node, context):
+ """
+ Get display rectangle from node
+ """
+
+ w = context['rect'][0]
+ h = context['rect'][1]
+
+ if node.getAttribute('viewBox'):
+ viewBox = node.getAttribute('viewBox').split()
+ w = SVGParseCoord(viewBox[2], w)
+ h = SVGParseCoord(viewBox[3], h)
+ else:
+ if node.getAttribute('width'):
+ w = SVGParseCoord(node.getAttribute('width'), w)
+
+ if node.getAttribute('height'):
+ h = SVGParseCoord(node.getAttribute('height'), h)
+
+
+ return (w, h)
+
+
+def SVGMatrixFromNode(node, context):
+ """
+ Get transformation matrix from given node
+ """
+
+ rect = context['rect']
+
+ m = Matrix()
+ x = SVGParseCoord(node.getAttribute('x') or '0', rect[0])
+ y = SVGParseCoord(node.getAttribute('y') or '0', rect[1])
+ w = SVGParseCoord(node.getAttribute('width') or str(rect[0]), rect[0])
+ h = SVGParseCoord(node.getAttribute('height') or str(rect[1]), rect[1])
+
+ m = m.Translation(Vector((x, y, 0.0)))
+ if len(context['rects']) > 1:
+ m = m * m.Scale(w / rect[0], 4, Vector((1.0, 0.0, 0.0)))
+ m = m * m.Scale(h / rect[1], 4, Vector((0.0, 1.0, 0.0)))
+
+ if node.getAttribute('viewBox'):
+ viewBox = node.getAttribute('viewBox').split()
+ vx = SVGParseCoord(viewBox[0], w)
+ vy = SVGParseCoord(viewBox[1], h)
+ vw = SVGParseCoord(viewBox[2], w)
+ vh = SVGParseCoord(viewBox[3], h)
+
+ m = m * m.Translation(Vector((-vx, -vy, 0.0)))
+ m = m * m.Scale(w / vw, 4, Vector((1.0, 0.0, 0.0)))
+ m = m * m.Scale(h / vh, 4, Vector((0.0, 1.0, 0.0)))
+
+ return m
+
+
+def SVGParseTransform(transform):
+ """
+ Parse transform string and return transformation matrix
+ """
+
+ m = Matrix()
+ r = re.compile('\s*([A-z]+)\s*\((.*?)\)')
+
+ for match in r.finditer(transform):
+ func = match.group(1)
+ params = match.group(2)
+ params = params.replace(',', ' ').split()
+
+ proc = SVGTransforms.get(func)
+ if proc is None:
+ raise Exception('Unknown trasnform function: ' + func)
+
+ m = m * proc(params)
+
+ return m
+
+
+def SVGGetMaterial(color, context):
+ """
+ Get material for specified color
+ """
+
+ materials = context['materials']
+
+ if color in materials:
+ return materials[color]
+
+ diff = None
+ if color.startswith('#'):
+ color = color[1:]
+
+ if len(color) == 3:
+ color = color[0] * 2 + color[1] * 2 + color[2] * 2
+
+ diff = (int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16))
+ elif color in svg_colors.SVGColors:
+ diff = svg_colors.SVGColors[color]
+ else:
+ return None
+
+ mat = bpy.data.materials.new(name='SVGMat')
+ mat.diffuse_color = diff
+
+ materials[color] = mat
+
+ return mat
+
+
+def SVGTransformTranslate(params):
+ """
+ translate SVG transform command
+ """
+
+ tx = float(params[0])
+ ty = float(params[1])
+ return Matrix.Translation(Vector((tx, ty, 0.0)))
+
+
+def SVGTransformMatrix(params):
+ """
+ matrix SVG transform command
+ """
+
+ a = float(params[0])
+ b = float(params[1])
+ c = float(params[2])
+ d = float(params[3])
+ e = float(params[4])
+ f = float(params[5])
+
+ return Matrix(((a, c, 0.0, 0.0),
+ (b, d, 0.0, 0.0),
+ (0, 0, 1.0, 0.0),
+ (e, f, 0.0, 1.0)))
+
+
+def SVGTransformScale(params):
+ """
+ scale SVG transform command
+ """
+
+ sx = sy = float(params[0])
+ if len(params) > 1:
+ sy = float(params[1])
+
+ m = Matrix()
+
+ m = m * m.Scale(sx, 4, Vector((1.0, 0.0, 0.0)))
+ m = m * m.Scale(sy, 4, Vector((0.0, 1.0, 0.0)))
+
+ return m
+
+
+def SVGTransformSkewX(params):
+ """
+ skewX SVG transform command
+ """
+
+ ang = float(params[0]) * pi / 180.0
+
+ return Matrix(((1.0, 0.0, 0.0),
+ (tan(ang), 1.0, 0.0),
+ (0.0, 0.0, 1.0))).to_4x4()
+
+
+def SVGTransformSkewY(params):
+ """
+ skewX SVG transform command
+ """
+
+ ang = float(params[0]) * pi / 180.0
+
+ return Matrix(((1.0, tan(ang), 0.0),
+ (0.0, 1.0, 0.0),
+ (0.0, 0.0, 1.0))).to_4x4()
+
+
+def SVGTransformRotate(params):
+ """
+ skewX SVG transform command
+ """
+
+ ang = float(params[0]) * pi / 180.0
+ cx = cy = 0.0
+ if len(params) >= 3:
+ cx = float(params[1])
+ cy = float(params[2])
+
+ tm = Matrix.Translation(Vector((cx, cy, 0.0)))
+ rm = Matrix.Rotation(ang, 4, Vector((0.0, 0.0, 1.0)))
+
+ return tm * rm * tm.inverted()
+
+SVGTransforms = {'translate': SVGTransformTranslate,
+ 'scale': SVGTransformScale,
+ 'skewX': SVGTransformSkewX,
+ 'skewY': SVGTransformSkewY,
+ 'matrix': SVGTransformMatrix,
+ 'rotate': SVGTransformRotate}
+
+#### SVG path helpers ####
+
+
+class SVGPathData:
+ """
+ SVG Path data token supplier
+ """
+
+ __slots__ = ('_data', # List of tokens
+ '_index', # Index of current token in tokens list
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-extensions-cvs
mailing list