From 70177028974edb177fe7a4fa7096ee0e80ce7fb3 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 24 Sep 2018 12:21:05 +0200 Subject: Export UV Layout: Rewrite Export UV Layout addon Differential Revision: https://developer.blender.org/D3715 Reviewer: brecht --- io_mesh_uv_layout/export_uv_svg.py | 101 +++++++++++++++---------------------- 1 file changed, 41 insertions(+), 60 deletions(-) (limited to 'io_mesh_uv_layout/export_uv_svg.py') diff --git a/io_mesh_uv_layout/export_uv_svg.py b/io_mesh_uv_layout/export_uv_svg.py index fe727d09..d00f9402 100644 --- a/io_mesh_uv_layout/export_uv_svg.py +++ b/io_mesh_uv_layout/export_uv_svg.py @@ -19,65 +19,46 @@ # import bpy - - -from xml.sax.saxutils import escape from os.path import basename +from xml.sax.saxutils import escape - -class Export_UV_SVG: - def begin(self, fw, image_size, opacity): - - self.fw = fw - self.image_width = image_size[0] - self.image_height = image_size[1] - self.opacity = opacity - - fw('\n') - fw('\n') - fw('\n') - desc = ("%r, (Blender %s)" % - (basename(bpy.data.filepath), bpy.app.version_string)) - fw('%s\n' % escape(desc)) - - def build(self, mesh, face_iter_func): - self.fw('\n') - desc = ("Mesh: %s" % (mesh.name)) - self.fw('%s\n' % escape(desc)) - - # svg colors - fill_settings = [] - fill_default = 'fill="grey"' - for mat in mesh.materials if mesh.materials else [None]: - if mat: - fill_settings.append('fill="rgb(%d, %d, %d)"' % - tuple(int(c * 255) for c in mat.diffuse_color)) - else: - fill_settings.append(fill_default) - - polys = mesh.polygons - for i, uvs in face_iter_func(): - try: # rare cases material index is invalid. - fill = fill_settings[polys[i].material_index] - except IndexError: - fill = fill_default - - self.fw(' 0.0: - self.fw(' %s fill-opacity="%.2g"' % (fill, self.opacity)) - - self.fw(' points="') - - for j, uv in enumerate(uvs): - x, y = uv[0], 1.0 - uv[1] - self.fw('%.3f,%.3f ' % (x * self.image_width, y * self.image_height)) - self.fw('" />\n') - - self.fw('\n') - - def end(self): - self.fw('\n') - self.fw('\n') +def export(filepath, face_data, colors, width, height, opacity): + with open(filepath, "w") as file: + for text in get_file_parts(face_data, colors, width, height, opacity): + file.write(text) + +def get_file_parts(face_data, colors, width, height, opacity): + yield from header(width, height) + yield from draw_polygons(face_data, width, height, opacity) + yield from footer() + +def header(width, height): + yield '\n' + yield '\n' + yield f'\n' + desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})" + yield f'{escape(desc)}\n' + +def draw_polygons(face_data, width, height, opacity): + for uvs, color in face_data: + fill = f'fill="{get_color_string(color)}"' + + yield '\n' + +def get_color_string(color): + r, g, b = color + return f"rgb({round(r*255)}, {round(g*255)}, {round(b*255)})" + +def footer(): + yield '\n' + yield '\n' \ No newline at end of file -- cgit v1.2.3