File headers: use SPDX license identifiers
[blender-addons.git] / io_mesh_uv_layout / export_uv_svg.py
blob971ee55874fda6697ae44dac287d9ee58c891d03
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 # <pep8-80 compliant>
5 import bpy
6 from os.path import basename
7 from xml.sax.saxutils import escape
9 def export(filepath, face_data, colors, width, height, opacity):
10 with open(filepath, 'w', encoding='utf-8') as file:
11 for text in get_file_parts(face_data, colors, width, height, opacity):
12 file.write(text)
14 def get_file_parts(face_data, colors, width, height, opacity):
15 yield from header(width, height)
16 yield from draw_polygons(face_data, width, height, opacity)
17 yield from footer()
19 def header(width, height):
20 yield '<?xml version="1.0" standalone="no"?>\n'
21 yield '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n'
22 yield ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
23 yield f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}"\n'
24 yield ' xmlns="http://www.w3.org/2000/svg" version="1.1">\n'
25 desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})"
26 yield f'<desc>{escape(desc)}</desc>\n'
28 def draw_polygons(face_data, width, height, opacity):
29 for uvs, color in face_data:
30 fill = f'fill="{get_color_string(color)}"'
32 yield '<polygon stroke="black" stroke-width="1"'
33 yield f' {fill} fill-opacity="{opacity:.2g}"'
35 yield ' points="'
37 for uv in uvs:
38 x, y = uv[0], 1.0 - uv[1]
39 yield f'{x*width:.3f},{y*height:.3f} '
40 yield '" />\n'
42 def get_color_string(color):
43 r, g, b = color
44 return f"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})"
46 def footer():
47 yield '\n'
48 yield '</svg>\n'