diff options
Diffstat (limited to 'io_mesh_uv_layout')
-rw-r--r-- | io_mesh_uv_layout/__init__.py | 12 | ||||
-rw-r--r-- | io_mesh_uv_layout/export_uv_eps.py | 6 | ||||
-rw-r--r-- | io_mesh_uv_layout/export_uv_png.py | 47 | ||||
-rw-r--r-- | io_mesh_uv_layout/export_uv_svg.py | 6 |
4 files changed, 43 insertions, 28 deletions
diff --git a/io_mesh_uv_layout/__init__.py b/io_mesh_uv_layout/__init__.py index 324f2254..40d9b501 100644 --- a/io_mesh_uv_layout/__init__.py +++ b/io_mesh_uv_layout/__init__.py @@ -3,8 +3,8 @@ bl_info = { "name": "UV Layout", "author": "Campbell Barton, Matt Ebb", - "version": (1, 1, 1), - "blender": (2, 80, 0), + "version": (1, 1, 3), + "blender": (3, 0, 0), "location": "Image-Window > UVs > Export UV Layout", "description": "Export the UV layout as a 2D graphic", "warning": "", @@ -128,10 +128,10 @@ class ExportUVLayout(bpy.types.Operator): polygon_data = list(self.iter_polygon_data_to_draw(context, meshes)) different_colors = set(color for _, color in polygon_data) if self.modified: - depsgraph = context.evaluated_depsgraph_get() - for obj in self.iter_objects_to_export(context): - obj_eval = obj.evaluated_get(depsgraph) - obj_eval.to_mesh_clear() + depsgraph = context.evaluated_depsgraph_get() + for obj in self.iter_objects_to_export(context): + obj_eval = obj.evaluated_get(depsgraph) + obj_eval.to_mesh_clear() export = self.get_exporter() export(filepath, polygon_data, different_colors, self.size[0], self.size[1], self.opacity) diff --git a/io_mesh_uv_layout/export_uv_eps.py b/io_mesh_uv_layout/export_uv_eps.py index 04b8a38e..9e013e13 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 958bac8e..b051c980 100644 --- a/io_mesh_uv_layout/export_uv_png.py +++ b/io_mesh_uv_layout/export_uv_png.py @@ -2,31 +2,30 @@ import bpy import gpu -import bgl 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() try: - bgl.glClearColor(0.0, 0.0, 0.0, 0.0) - bgl.glClear(bgl.GL_COLOR_BUFFER_BIT) + fb = gpu.state.active_framebuffer_get() + fb.clear(color=(0.0, 0.0, 0.0, 0.0)) draw_image(face_data, opacity) - pixel_data = get_pixel_data_from_current_back_buffer(width, height) + pixel_data = fb.read_color(0, 0, width, height, 4, 0, 'UBYTE') + pixel_data.dimensions = width * height * 4 save_pixels(filepath, pixel_data, width, height) finally: offscreen.unbind() offscreen.free() + def draw_image(face_data, opacity): - bgl.glLineWidth(1) - bgl.glEnable(bgl.GL_BLEND) - bgl.glEnable(bgl.GL_LINE_SMOOTH) - bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) + gpu.state.blend_set('ALPHA_PREMULT') with gpu.matrix.push_pop(): gpu.matrix.load_matrix(get_normalize_uvs_matrix()) @@ -35,8 +34,8 @@ def draw_image(face_data, opacity): draw_background_colors(face_data, opacity) draw_lines(face_data) - bgl.glDisable(bgl.GL_BLEND) - bgl.glDisable(bgl.GL_LINE_SMOOTH) + gpu.state.blend_set('NONE') + def get_normalize_uvs_matrix(): '''matrix maps x and y coordinates from [0, 1] to [-1, 1]''' @@ -47,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))] @@ -59,35 +59,38 @@ 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])) - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') - batch = batch_for_shader(shader, 'LINES', {"pos" : coords}) + # 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}) + shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') shader.bind() + shader.uniform_float("viewportSize", gpu.state.viewport_get()[2:]) + shader.uniform_float("lineWidth", 0.5) shader.uniform_float("color", (0, 0, 0, 1)) batch.draw(shader) -def get_pixel_data_from_current_back_buffer(width, height): - buffer = bgl.Buffer(bgl.GL_BYTE, width * height * 4) - bgl.glReadBuffer(bgl.GL_BACK) - bgl.glReadPixels(0, 0, width, height, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buffer) - return buffer def save_pixels(filepath, pixel_data, width, height): image = bpy.data.images.new("temp", width, height, alpha=True) diff --git a/io_mesh_uv_layout/export_uv_svg.py b/io_mesh_uv_layout/export_uv_svg.py index d8e2b5a4..a4b6a3c3 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' |