1# ##### BEGIN GPL LICENSE BLOCK ##### 2# 3# This program is free software; you can redistribute it and/or 4# modify it under the terms of the GNU General Public License 5# as published by the Free Software Foundation; either version 2 6# of the License, or (at your option) any later version. 7# 8# This program is distributed in the hope that it will be useful, 9# but WITHOUT ANY WARRANTY; without even the implied warranty of 10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11# GNU General Public License for more details. 12# 13# You should have received a copy of the GNU General Public License 14# along with this program; if not, write to the Free Software Foundation, 15# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 16# 17# ##### END GPL LICENSE BLOCK ##### 18 19# <pep8-80 compliant> 20 21import bpy 22from os.path import basename 23from xml.sax.saxutils import escape 24 25def export(filepath, face_data, colors, width, height, opacity): 26 with open(filepath, 'w', encoding='utf-8') as file: 27 for text in get_file_parts(face_data, colors, width, height, opacity): 28 file.write(text) 29 30def get_file_parts(face_data, colors, width, height, opacity): 31 yield from header(width, height) 32 yield from draw_polygons(face_data, width, height, opacity) 33 yield from footer() 34 35def header(width, height): 36 yield '<?xml version="1.0" standalone="no"?>\n' 37 yield '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n' 38 yield ' "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n' 39 yield f'<svg width="{width}" height="{height}" viewBox="0 0 {width} {height}"\n' 40 yield ' xmlns="http://www.w3.org/2000/svg" version="1.1">\n' 41 desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})" 42 yield f'<desc>{escape(desc)}</desc>\n' 43 44def draw_polygons(face_data, width, height, opacity): 45 for uvs, color in face_data: 46 fill = f'fill="{get_color_string(color)}"' 47 48 yield '<polygon stroke="black" stroke-width="1"' 49 yield f' {fill} fill-opacity="{opacity:.2g}"' 50 51 yield ' points="' 52 53 for uv in uvs: 54 x, y = uv[0], 1.0 - uv[1] 55 yield f'{x*width:.3f},{y*height:.3f} ' 56 yield '" />\n' 57 58def get_color_string(color): 59 r, g, b = color 60 return f"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})" 61 62def footer(): 63 yield '\n' 64 yield '</svg>\n' 65