Update for changes in Blender's API
[blender-addons.git] / io_mesh_uv_layout / export_uv_svg.py
blob764f0d341bb7ad1edc65d87796c38688d3c71cf8
1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
19 # <pep8-80 compliant>
21 import bpy
24 def write(fw, mesh, image_width, image_height, opacity, face_iter_func):
25 # for making an XML compatible string
26 from xml.sax.saxutils import escape
27 from os.path import basename
29 fw('<?xml version="1.0" standalone="no"?>\n')
30 fw('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n')
31 fw(' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n')
32 fw('<svg width="%d" height="%d" viewBox="0 0 %d %d"\n' %
33 (image_width, image_height, image_width, image_height))
34 fw(' xmlns="http://www.w3.org/2000/svg" version="1.1">\n')
35 desc = ("%r, %s, (Blender %s)" %
36 (basename(bpy.data.filepath), mesh.name, bpy.app.version_string))
37 fw('<desc>%s</desc>\n' % escape(desc))
39 # svg colors
40 fill_settings = []
41 fill_default = 'fill="grey"'
42 for mat in mesh.materials if mesh.materials else [None]:
43 if mat:
44 fill_settings.append('fill="rgb(%d, %d, %d)"' %
45 tuple(int(c * 255)
46 for c in mat.diffuse_color))
47 else:
48 fill_settings.append(fill_default)
50 polys = mesh.polygons
51 for i, uvs in face_iter_func():
52 try: # rare cases material index is invalid.
53 fill = fill_settings[polys[i].material_index]
54 except IndexError:
55 fill = fill_default
57 fw('<polygon stroke="black" stroke-width="1"')
58 if opacity > 0.0:
59 fw(' %s fill-opacity="%.2g"' % (fill, opacity))
61 fw(' points="')
63 for j, uv in enumerate(uvs):
64 x, y = uv[0], 1.0 - uv[1]
65 fw('%.3f,%.3f ' % (x * image_width, y * image_height))
66 fw('" />\n')
67 fw('\n')
68 fw('</svg>\n')