4 # Name: 'UV: Export to SVG'
7 # Tooltip: 'Export selected objects to SVG file'
10 __author__ = "Melchior FRANZ < mfranz # aon : at >"
11 __url__ = "http://members.aon.at/mfranz/flightgear/"
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 FILL_COLOR = '' # 'yellow' or '#ffa000' e.g. for uni-color, empty string for random color
23 import Blender, BPyMessages, sys, random
26 class Abort(Exception):
27 def __init__(self, msg):
32 def __init__(self, mesh):
33 faces = dict([(f.index, f) for f in mesh.faces])
36 self.groups.append(self.adjacent(faces))
39 return len(self.groups)
42 return self.groups.__iter__()
44 def adjacent(self, faces):
46 face = faces.popitem()[1]
49 uvcoords[(c[0], c[1])] = True
53 for face in faces.itervalues():
55 if (c[0], c[1]) in uvcoords:
57 uvcoords[(c[0], c[1])] = True
68 random.seed(hash(name))
69 c = [random.randint(220, 255), random.randint(120, 220), random.randint(120, 220)]
71 return "#%02x%02x%02x" % (c[0], c[1], c[2])
75 size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192")
77 raise Abort('no image size chosen')
78 size = 1 << (size + 6)
80 print "exporting to '%s' (size %d) ... " % (path, size),
82 svg.write('<?xml version="1.0" standalone="no"?>\n')
83 svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
84 svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
85 ' version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
86 % (size, size, size, size))
87 svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % path);
88 svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
92 for o in Blender.Scene.GetCurrent().objects.selected:
96 mesh = o.getData(mesh = 1)
98 objects[mesh.name] = (o.name, mesh)
100 for meshname, v in objects.iteritems():
102 color = FILL_COLOR or hashcolor(meshname)
104 svg.write('\t<g style="fill:%s; stroke:black stroke-width:1px" inkscape:label="%s" ' \
105 'id="%s">\n' % (color, objname, objname))
107 facegroups = UVFaceGroups(mesh)
108 for faces in facegroups:
110 if len(facegroups) > 1:
111 svg.write('\t\t<g>\n')
114 svg.write('%s<polygon id="%s%s%d" points="' % (indent, meshname, ID_SEPARATOR, f.index))
116 svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
118 if len(facegroups) > 1:
119 svg.write('\t\t</g>\n')
121 svg.write("\t</g>\n")
123 svg.write('</svg>\n')
129 if not BPyMessages.Warning_SaveOver(path):
132 editmode = Blender.Window.EditMode()
134 Blender.Window.EditMode(0)
138 Blender.Registry.SetKey("UVImportExportSVG", { "path" : path }, False)
141 print "Error:", e.msg, " -> aborting ...\n"
142 Blender.Draw.PupMenu("Error%t|" + e.msg)
145 Blender.Window.EditMode(1)
148 registry = Blender.Registry.GetKey("UVImportExportSVG", False)
149 if registry and "path" in registry:
150 path = registry["path"]
152 active = Blender.Scene.GetCurrent().objects.active
153 basename = Blender.sys.basename(Blender.sys.splitext(Blender.Get("filename"))[0])
154 path = basename + "-" + active.name + ".svg"
156 Blender.Window.FileSelector(export, "Export to SVG", path)