diff options
Diffstat (limited to 'io_mesh_uv_layout/export_uv_png.py')
-rw-r--r-- | io_mesh_uv_layout/export_uv_png.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index 78f9f6a2..b051c980 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,33 +59,39 @@ 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)] - coords.append((start[0], start[1], 0.0)) - coords.append((end[0], end[1], 0.0)) - - # Use '2D_UNIFORM_COLOR' if smooth lines are not required. + 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}) shader = gpu.shader.from_builtin('3D_POLYLINE_UNIFORM_COLOR') - batch = batch_for_shader(shader, 'LINES', {"pos" : coords}) 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 save_pixels(filepath, pixel_data, width, height): image = bpy.data.images.new("temp", width, height, alpha=True) image.filepath = filepath |