4 # Name: 'SVG: Export UV layout to SVG file'
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.
26 class Abort(Exception):
27 def __init__(self, msg):
31 def get_adjacent_faces(pool):
35 i, face = pool.popitem()
40 uvcoords[(c[0], c[1])] = True
44 for face in pool.itervalues():
46 if (c[0], c[1]) in uvcoords:
48 uvcoords[(d[0], d[1])] = True
60 def write_svg(filename):
61 size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192")
63 raise Abort('no image size chosen')
64 size = 1 << (size + 6)
66 print "exporting to '%s' (size %d) ... " % (filename, size),
67 svg = open(filename, "w")
68 svg.write('<?xml version="1.0" standalone="no"?>\n')
69 svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
70 svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
71 ' version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
72 % (size, size, size, size))
73 svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % filename);
74 svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
78 for o in Blender.Scene.GetCurrent().objects.selected:
82 mesh = o.getData(mesh = 1)
85 if mesh.name in unique_meshes:
87 unique_meshes[mesh.name] = True
89 svg.write('\t<g style="fill:%s; stroke:black stroke-width:1px" inkscape:label="%s" ' \
90 'id="%s">\n' % (FILL_COLOR, o.name, o.name))
98 groups.append(get_adjacent_faces(pool))
103 svg.write('\t\t<g>\n')
106 svg.write('%s<polygon id="%s%s%d" points="' % (indent, mesh.name, ID_SEPARATOR, f.index))
108 svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
111 svg.write('\t\t</g>\n')
113 svg.write("\t</g>\n")
115 svg.write('</svg>\n')
120 def export(filename):
122 registry[basename] = Blender.sys.basename(filename)
123 Blender.Registry.SetKey("UVImportExportSVG", registry, False)
125 editmode = Blender.Window.EditMode()
127 Blender.Window.EditMode(0)
132 print "Error:", e.msg, " -> aborting ...\n"
133 Blender.Draw.PupMenu("Error%t|" + e.msg)
136 Blender.Window.EditMode(1)
139 active = Blender.Scene.GetCurrent().objects.active
140 (basename, extname) = Blender.sys.splitext(Blender.Get("filename"))
141 filename = Blender.sys.basename(basename) + "-" + active.name + ".svg"
142 Blender.Window.FileSelector(export, "Export to SVG", filename)