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 files 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 put into an SVG group.
25 class Abort(Exception):
26 def __init__(self, msg):
30 def get_adjacent(pool):
31 i, face = pool.popitem()
36 uvcoords[(c[0], c[1])] = True
40 for face in pool.itervalues():
42 if (c[0], c[1]) in uvcoords:
44 uvcoords[(d[0], d[1])] = True
56 def write_svg(filename):
57 size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192")
59 raise Abort('no image size chosen')
60 size = 1 << (size + 6)
62 print "exporting to '%s' (size %d) ... " % (filename, size),
63 svg = open(filename, "w")
64 svg.write('<?xml version="1.0" standalone="no"?>\n')
65 svg.write('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n\n')
66 svg.write('<svg width="%spx" height="%spx" viewBox="0 0 %d %d" xmlns="http://www.w3.org/2000/svg"' \
67 'version="1.1" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">\n'
68 % (size, size, size, size))
69 svg.write("\t<desc>uv_export_svg.py: %s</desc>\n" % filename);
70 svg.write('\t<rect x="0" y="0" width="%d" height="%d" fill="none" stroke="blue" stroke-width="%f"/>\n'
74 for o in Blender.Scene.GetCurrent().objects.selected:
78 mesh = o.getData(mesh = 1)
81 if mesh.name in unique_meshes:
82 #print "dropping duplicate mesh", mesh.name, "of object", o.name
84 unique_meshes[mesh.name] = True
86 svg.write('\t<g style="fill:yellow; stroke:black stroke-width:1px" inkscape:label="%s" id="%s">\n' % (o.name, o.name))
93 svg.write('\t\t<g>\n')
94 for f in get_adjacent(pool):
95 svg.write('\t\t\t<polygon points="')
97 svg.write('%.8f,%.8f ' % (p[0] * size, size - p[1] * size))
98 svg.write('" id="%s%s%d"/>\n' % (mesh.name, ID_SEPARATOR, f.index))
99 svg.write('\t\t</g>\n')
101 svg.write("\t</g>\n")
103 svg.write('</svg>\n')
108 def export(filename):
110 registry[basename] = Blender.sys.basename(filename)
111 Blender.Registry.SetKey("UVImportExportSVG", registry, False)
113 editmode = Blender.Window.EditMode()
115 Blender.Window.EditMode(0)
120 print "Error:", e.msg, " -> aborting ...\n"
121 Blender.Draw.PupMenu("Error%t|" + e.msg)
124 Blender.Window.EditMode(1)
127 active = Blender.Scene.GetCurrent().objects.active
128 (basename, extname) = Blender.sys.splitext(Blender.Get("filename"))
129 filename = Blender.sys.basename(basename) + "-" + active.name + ".svg"
130 Blender.Window.FileSelector(export, "Export to SVG", filename)