From b56a6acb9f4fe1041ed377202f05ed5a5dd09d91 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Thu, 21 Jul 2022 13:03:25 -0300 Subject: UV Layout: replace deprecated bgl module Part of T80730 --- io_mesh_uv_layout/export_uv_png.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'io_mesh_uv_layout/export_uv_png.py') diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index 958bac8e..74cd7b19 100644 --- a/io_mesh_uv_layout/export_uv_png.py +++ b/io_mesh_uv_layout/export_uv_png.py @@ -2,7 +2,6 @@ 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 @@ -12,21 +11,23 @@ def export(filepath, face_data, colors, width, height, opacity): 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.line_width_set(1.0) + gpu.state.blend_set('ALPHA_PREMULT') + # TODO: Use shader that draws a smooth line (Eg. '3D_POLYLINE_UNIFORM_COLOR') + # bgl.glEnable(bgl.GL_LINE_SMOOTH) + # bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) with gpu.matrix.push_pop(): gpu.matrix.load_matrix(get_normalize_uvs_matrix()) @@ -35,8 +36,7 @@ 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]''' @@ -83,12 +83,6 @@ def draw_lines(face_data): 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) image.filepath = filepath -- cgit v1.2.3 From 563ea27eb1a8843fdc05b2fe3944dfbdcd4d5a1f Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Thu, 21 Jul 2022 23:35:50 -0300 Subject: UV Layout: bring back smooth lines Feature removed in rBAb56a6acb9f4f. --- io_mesh_uv_layout/export_uv_png.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'io_mesh_uv_layout/export_uv_png.py') diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index 74cd7b19..78f9f6a2 100644 --- a/io_mesh_uv_layout/export_uv_png.py +++ b/io_mesh_uv_layout/export_uv_png.py @@ -23,11 +23,7 @@ def export(filepath, face_data, colors, width, height, opacity): offscreen.free() def draw_image(face_data, opacity): - gpu.state.line_width_set(1.0) gpu.state.blend_set('ALPHA_PREMULT') - # TODO: Use shader that draws a smooth line (Eg. '3D_POLYLINE_UNIFORM_COLOR') - # bgl.glEnable(bgl.GL_LINE_SMOOTH) - # bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST) with gpu.matrix.push_pop(): gpu.matrix.load_matrix(get_normalize_uvs_matrix()) @@ -74,12 +70,15 @@ def draw_lines(face_data): for i in range(len(uvs)): start = uvs[i] end = uvs[(i+1) % len(uvs)] - coords.append((start[0], start[1])) - coords.append((end[0], end[1])) + coords.append((start[0], start[1], 0.0)) + coords.append((end[0], end[1], 0.0)) - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') + # Use '2D_UNIFORM_COLOR' if smooth lines are not required. + 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) -- cgit v1.2.3 From fdebdd681f0bee5fb1914eaa8b1c2c022b73a2f1 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Thu, 28 Jul 2022 16:12:06 -0300 Subject: UV Layout: don't convert line coordinates to 3d Although shader uses 3D attribute, it fills missing components with vec4(0,0,0,1). This was changed in rBA563ea27eb1a8 --- io_mesh_uv_layout/export_uv_png.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'io_mesh_uv_layout/export_uv_png.py') diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index 78f9f6a2..b7110c73 100644 --- a/io_mesh_uv_layout/export_uv_png.py +++ b/io_mesh_uv_layout/export_uv_png.py @@ -70,12 +70,14 @@ def draw_lines(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)) + coords.append((start[0], start[1])) + coords.append((end[0], end[1])) - # Use '2D_UNIFORM_COLOR' if smooth lines are not required. + # 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) -- cgit v1.2.3 From 8669c66177eab49871e9ae758c74da05cce89adf Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 29 Jul 2022 15:33:17 +1000 Subject: Cleanup: run autopep8 on io_mesh_uv_layout --- io_mesh_uv_layout/export_uv_png.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'io_mesh_uv_layout/export_uv_png.py') diff --git a/io_mesh_uv_layout/export_uv_png.py b/io_mesh_uv_layout/export_uv_png.py index b7110c73..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,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 -- cgit v1.2.3