1 # SPDX-License-Identifier: GPL-2.0-or-later
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
):
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
)
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}"'
38 x
, y
= uv
[0], 1.0 - uv
[1]
39 yield f
'{x*width:.3f},{y*height:.3f} '
42 def get_color_string(color
):
44 return f
"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})"