diff --git a/io_mesh_uv_layout/export_uv_eps.py b/io_mesh_uv_layout/export_uv_eps.py index 04b8a38e53133869b28b7210d9213d726b8598e4..9e013e13ef99ffbddc7e515103bd513057d1b426 100644 --- a/io_mesh_uv_layout/export_uv_eps.py +++ b/io_mesh_uv_layout/export_uv_eps.py @@ -8,6 +8,7 @@ def export(filepath, face_data, colors, width, height, opacity): 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) if opacity > 0.0: @@ -35,6 +36,7 @@ def header(width, height): yield "1 setlinejoin\n" yield "1 setlinecap\n" + def prepare_colors(colors, out_name_by_color): for i, color in enumerate(colors): name = f"COLOR_{i}" @@ -48,18 +50,21 @@ def prepare_colors(colors, out_name_by_color): yield "0 setgray\n" yield "} def\n" + def draw_colored_polygons(face_data, name_by_color, width, height): for uvs, color in face_data: yield from draw_polygon_path(uvs, width, height) yield "closepath\n" yield "%s\n" % name_by_color[color] + def draw_lines(face_data, width, height): for uvs, _ in face_data: yield from draw_polygon_path(uvs, width, height) yield "closepath\n" yield "stroke\n" + def draw_polygon_path(uvs, width, height): yield "newpath\n" for j, uv in enumerate(uvs): @@ -69,6 +74,7 @@ def draw_polygon_path(uvs, width, height): else: yield "%.5f %.5f lineto\n" % uv_scale + def footer(): yield "showpage\n" yield "%%EOF\n" diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index b7110c7326c4a1f8216217b9317cdd4903aede09..b051c980c4b651ecfeaecfa8f5e1c5e7929a324f 100644 --- a/io_mesh_uv_layout/export_uv_png.py +++ b/io_mesh_uv_layout/export_uv_png.py @@ -6,6 +6,7 @@ from mathutils import Vector, Matrix from mathutils.geometry import tessellate_polygon from gpu_extras.batch import batch_for_shader + def export(filepath, face_data, colors, width, height, opacity): offscreen = gpu.types.GPUOffScreen(width, height) offscreen.bind() @@ -22,6 +23,7 @@ def export(filepath, face_data, colors, width, height, opacity): offscreen.unbind() offscreen.free() + def draw_image(face_data, opacity): gpu.state.blend_set('ALPHA_PREMULT') @@ -34,6 +36,7 @@ def draw_image(face_data, opacity): gpu.state.blend_set('NONE') + def get_normalize_uvs_matrix(): '''matrix maps x and y coordinates from [0, 1] to [-1, 1]''' matrix = Matrix.Identity(4) @@ -43,6 +46,7 @@ def get_normalize_uvs_matrix(): matrix[1][1] = 2 return matrix + def draw_background_colors(face_data, opacity): coords = [uv for uvs, _ in face_data for uv in uvs] colors = [(*color, opacity) for uvs, color in face_data for _ in range(len(uvs))] @@ -55,28 +59,31 @@ def draw_background_colors(face_data, opacity): offset += len(uvs) shader = gpu.shader.from_builtin('2D_FLAT_COLOR') - batch = batch_for_shader(shader, 'TRIS', - {"pos" : coords, - "color" : colors}, - indices=indices) + batch = batch_for_shader( + shader, 'TRIS', + {"pos": coords, "color": colors}, + indices=indices, + ) batch.draw(shader) + def tessellate_uvs(uvs): return tessellate_polygon([uvs]) + def draw_lines(face_data): coords = [] for uvs, _ in face_data: for i in range(len(uvs)): start = uvs[i] - end = uvs[(i+1) % len(uvs)] + end = uvs[(i + 1) % len(uvs)] coords.append((start[0], start[1])) coords.append((end[0], end[1])) # Use '2D_UNIFORM_COLOR' in the `batch_for_shader` so we don't need to # convert the coordinates to 3D as in the case of # '3D_POLYLINE_UNIFORM_COLOR'. - batch = batch_for_shader(gpu.shader.from_builtin('2D_UNIFORM_COLOR'), 'LINES', {"pos" : coords}) + batch = batch_for_shader(gpu.shader.from_builtin('2D_UNIFORM_COLOR'), 'LINES', {"pos": coords}) shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') shader.bind() shader.uniform_float("viewportSize", gpu.state.viewport_get()[2:]) @@ -84,6 +91,7 @@ def draw_lines(face_data): shader.uniform_float("color", (0, 0, 0, 1)) batch.draw(shader) + def save_pixels(filepath, pixel_data, width, height): image = bpy.data.images.new("temp", width, height, alpha=True) image.filepath = filepath diff --git a/io_mesh_uv_layout/export_uv_svg.py b/io_mesh_uv_layout/export_uv_svg.py index d8e2b5a433d6759e6d49e3e07af5d5919a11cba2..a4b6a3c30a645fc1cec1aade7bbc631399d054e4 100644 --- a/io_mesh_uv_layout/export_uv_svg.py +++ b/io_mesh_uv_layout/export_uv_svg.py @@ -4,16 +4,19 @@ import bpy from os.path import basename from xml.sax.saxutils import escape + def export(filepath, face_data, colors, width, height, opacity): with open(filepath, 'w', encoding='utf-8') 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 '<?xml version="1.0" standalone="no"?>\n' yield '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" \n' @@ -23,6 +26,7 @@ def header(width, height): desc = f"{basename(bpy.data.filepath)}, (Blender {bpy.app.version_string})" yield f'<desc>{escape(desc)}</desc>\n' + def draw_polygons(face_data, width, height, opacity): for uvs, color in face_data: fill = f'fill="{get_color_string(color)}"' @@ -37,10 +41,12 @@ def draw_polygons(face_data, width, height, opacity): yield f'{x*width:.3f},{y*height:.3f} ' 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 '</svg>\n'