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