4 # Name: 'UV: Export to SVG'
7 # Tooltip: 'Export selected objects to SVG file'
10 __author__ = "Melchior FRANZ < mfranz # aon : at >"
11 __url__ = "http://www.flightgear.org/"
14 Saves the UV mappings of all selected objects to an SVG file. The uv_import_svg.py
15 script can be used to re-import such a file. Each object and each group of adjacent
16 faces therein will be made a separate SVG group.
19 #--------------------------------------------------------------------------------
20 # Copyright (C) 2008 Melchior FRANZ < mfranz # aon : at >
22 # This program is free software; you can redistribute it and/or
23 # modify it under the terms of the GNU General Public License as
24 # published by the Free Software Foundation; either version 2 of the
25 # License, or (at your option) any later version.
27 # This program is distributed in the hope that it will be useful, but
28 # WITHOUT ANY WARRANTY; without even the implied warranty of
29 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 # General Public License for more details.
32 # You should have received a copy of the GNU General Public License
33 # along with this program; if not, write to the Free Software
34 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
35 #--------------------------------------------------------------------------------
38 FILL_COLOR = None # 'yellow' or '#ffa000' e.g. for uni-color, None for random color
39 ID_SEPARATOR = '_.:._'
42 import Blender, BPyMessages, sys, random
45 class Abort(Exception):
46 def __init__(self, msg):
51 def __init__(self, mesh):
52 faces = dict([(f.index, f) for f in mesh.faces])
55 self.groups.append(self.adjacent(faces))
58 return len(self.groups)
61 return self.groups.__iter__()
63 def adjacent(self, faces):
65 face = faces.popitem()[1]
68 uvcoords[tuple(c)] = True
72 for face in faces.itervalues():
74 if tuple(c) in uvcoords:
76 uvcoords[tuple(c)] = True
86 def stringcolor(string):
87 random.seed(hash(string))
88 c = [random.randint(220, 255), random.randint(120, 240), random.randint(120, 240)]
90 return "#%02x%02x%02x" % tuple(c)
94 size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192")
96 raise Abort('no image size chosen')
97 size = 1 << (size + 6)
100 svg.write('<?xml version="1.0" standalone="no"?>\n')
101 svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
102 svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
103 ' version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
104 % (size, size, size, size))
105 svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % path);
106 svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
110 for o in Blender.Scene.GetCurrent().objects.selected:
114 mesh = o.getData(mesh = 1)
116 objects[mesh.name] = (o.name, mesh)
118 for meshname, v in objects.iteritems():
120 color = FILL_COLOR or stringcolor(meshname)
122 svg.write('\t<g style="fill:%s; stroke:black stroke-width:1px" inkscape:label="%s" ' \
123 'id="%s">\n' % (color, objname, objname))
125 facegroups = UVFaceGroups(mesh)
126 for faces in facegroups:
128 if len(facegroups) > 1:
129 svg.write('\t\t<g>\n')
132 svg.write('%s<polygon id="%s%s%d" points="' % (indent, meshname, ID_SEPARATOR, f.index))
134 svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
136 if len(facegroups) > 1:
137 svg.write('\t\t</g>\n')
139 svg.write("\t</g>\n")
141 svg.write('</svg>\n')
146 if not BPyMessages.Warning_SaveOver(path):
149 editmode = Blender.Window.EditMode()
151 Blender.Window.EditMode(0)
155 Blender.Registry.SetKey("UVImportExportSVG", { "path" : path }, False)
158 print "Error:", e.msg, " -> aborting ...\n"
159 Blender.Draw.PupMenu("Error%t|" + e.msg)
162 Blender.Window.EditMode(1)
165 registry = Blender.Registry.GetKey("UVImportExportSVG", False)
166 if registry and "path" in registry:
167 path = registry["path"]
169 active = Blender.Scene.GetCurrent().objects.active
170 basename = Blender.sys.basename(Blender.sys.splitext(Blender.Get("filename"))[0])
171 path = basename + "-" + active.name + ".svg"
173 Blender.Window.FileSelector(export, "Export to SVG", path)