Sun Position: update HDRI mode shader for Metal compatibility
[blender-addons.git] / io_mesh_uv_layout / export_uv_svg.py
bloba4b6a3c30a645fc1cec1aade7bbc631399d054e4
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 from os.path import basename
5 from xml.sax.saxutils import escape
8 def export(filepath, face_data, colors, width, height, opacity):
9 with open(filepath, 'w', encoding='utf-8') as file:
10 for text in get_file_parts(face_data, colors, width, height, opacity):
11 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()
20 def header(width, height):
21 yield '<?xml version="1.0" standalone="no"?>\n'
22 yield '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n'
23 yield ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
24 yield f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}"\n'
25 yield ' xmlns="http://www.w3.org/2000/svg" version="1.1">\n'
26 desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})"
27 yield f'<desc>{escape(desc)}</desc>\n'
30 def draw_polygons(face_data, width, height, opacity):
31 for uvs, color in face_data:
32 fill = f'fill="{get_color_string(color)}"'
34 yield '<polygon stroke="black" stroke-width="1"'
35 yield f' {fill} fill-opacity="{opacity:.2g}"'
37 yield ' points="'
39 for uv in uvs:
40 x, y = uv[0], 1.0 - uv[1]
41 yield f'{x*width:.3f},{y*height:.3f} '
42 yield '" />\n'
45 def get_color_string(color):
46 r, g, b = color
47 return f"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})"
50 def footer():
51 yield '\n'
52 yield '</svg>\n'