]> git.mxchange.org Git - flightgear.git/blob - utils/Modeller/uv_export_svg.py
a270fe13add8bd06044eb0065b582dba0b114752
[flightgear.git] / utils / Modeller / uv_export_svg.py
1 #!BPY
2
3 # """
4 # Name: 'SVG: Export UV layout to SVG file'
5 # Blender: 245
6 # Group: 'UV'
7 # Tooltip: 'Export selected objects to SVG file'
8 # """
9
10 __author__ = "Melchior FRANZ < mfranz # aon : at >"
11 __url__ = "http://members.aon.at/mfranz/flightgear/"
12 __version__ = "0.1"
13 __bpydoc__ = """\
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.
17 """
18
19 ID_SEPARATOR = '#'
20
21
22 import Blender, sys
23
24
25 class Abort(Exception):
26         def __init__(self, msg):
27                 self.msg = msg
28
29
30 def get_adjacent(pool):
31         i, face = pool.popitem()
32         group = [face]
33
34         uvcoords = {}
35         for c in face.uv:
36                 uvcoords[(c[0], c[1])] = True
37
38         while True:
39                 found = []
40                 for face in pool.itervalues():
41                         for c in face.uv:
42                                 if (c[0], c[1]) in uvcoords:
43                                         for d in face.uv:
44                                                 uvcoords[(d[0], d[1])] = True
45                                         found.append(face)
46                                         break
47                 if not found:
48                         break
49                 for face in found:
50                         group.append(face)
51                         del pool[face.index]
52
53         return group
54
55
56 def write_svg(filename):
57         size = Blender.Draw.PupMenu("Image size%t|128|256|512|1024|2048|4096|8192")
58         if size < 0:
59                 raise Abort('no image size chosen')
60         size = 1 << (size + 6)
61
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'
71                         % (size, size, 1.0))
72
73         unique_meshes = {}
74         for o in Blender.Scene.GetCurrent().objects.selected:
75                 if o.type != "Mesh":
76                         continue
77
78                 mesh = o.getData(mesh = 1)
79                 if not mesh.faceUV:
80                         continue
81                 if mesh.name in unique_meshes:
82                         #print "dropping duplicate mesh", mesh.name, "of object", o.name
83                         continue
84                 unique_meshes[mesh.name] = True
85
86                 svg.write('\t<g style="fill:yellow; stroke:black stroke-width:1px" inkscape:label="%s" id="%s">\n' % (o.name, o.name))
87
88                 pool = {}
89                 for f in mesh.faces:
90                         pool[f.index] = f
91
92                 while len(pool):
93                         svg.write('\t\t<g>\n')
94                         for f in get_adjacent(pool):
95                                 svg.write('\t\t\t<polygon points="')
96                                 for p in f.uv:
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')
100
101                 svg.write("\t</g>\n")
102
103         svg.write('</svg>\n')
104         svg.close()
105         print "done."
106
107
108 def export(filename):
109         registry = {}
110         registry[basename] = Blender.sys.basename(filename)
111         Blender.Registry.SetKey("UVImportExportSVG", registry, False)
112
113         editmode = Blender.Window.EditMode()
114         if editmode:
115                 Blender.Window.EditMode(0)
116
117         try:
118                 write_svg(filename)
119         except Abort, e:
120                 print "Error:", e.msg, "  -> aborting ...\n"
121                 Blender.Draw.PupMenu("Error%t|" + e.msg)
122
123         if editmode:
124                 Blender.Window.EditMode(1)
125
126
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)