Cleanup: Node Wrangler: preview_node operator
[blender-addons.git] / io_mesh_uv_layout / export_uv_svg.py
bloba4811ed11ffd36400ed359f4207883c531727839
1 # SPDX-FileCopyrightText: 2011-2022 Blender Foundation
3 # SPDX-License-Identifier: GPL-2.0-or-later
5 import bpy
6 from os.path import basename
7 from xml.sax.saxutils import escape
10 def export(filepath, tile, face_data, colors, width, height, opacity):
11 with open(filepath, 'w', encoding='utf-8') as file:
12 for text in get_file_parts(tile, face_data, colors, width, height, opacity):
13 file.write(text)
16 def get_file_parts(tile, face_data, colors, width, height, opacity):
17 yield from header(width, height)
18 yield from draw_polygons(tile, face_data, width, height, opacity)
19 yield from footer()
22 def header(width, height):
23 yield '<?xml version="1.0" standalone="no"?>\n'
24 yield '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n'
25 yield ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
26 yield f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}"\n'
27 yield ' xmlns="http://www.w3.org/2000/svg" version="1.1">\n'
28 desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})"
29 yield f'<desc>{escape(desc)}</desc>\n'
32 def draw_polygons(tile, face_data, width, height, opacity):
33 for uvs, color in face_data:
34 fill = f'fill="{get_color_string(color)}"'
36 yield '<polygon stroke="black" stroke-width="1"'
37 yield f' {fill} fill-opacity="{opacity:.2g}"'
39 yield ' points="'
41 for uv in uvs:
42 x, y = uv[0] - tile[0], 1.0 - uv[1] + tile[1]
43 yield f'{x*width:.3f},{y*height:.3f} '
44 yield '" />\n'
47 def get_color_string(color):
48 r, g, b = color
49 return f"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})"
52 def footer():
53 yield '\n'
54 yield '</svg>\n'