[Bf-extensions-cvs] [b56a6acb] master: UV Layout: replace deprecated bgl module

Germano Cavalcante noreply at git.blender.org
Thu Jul 21 19:04:36 CEST 2022


Commit: b56a6acb9f4fe1041ed377202f05ed5a5dd09d91
Author: Germano Cavalcante
Date:   Thu Jul 21 13:03:25 2022 -0300
Branches: master
https://developer.blender.org/rBAb56a6acb9f4fe1041ed377202f05ed5a5dd09d91

UV Layout: replace deprecated bgl module

Part of T80730

===================================================================

M	io_mesh_uv_layout/__init__.py
M	io_mesh_uv_layout/export_uv_png.py

===================================================================

diff --git a/io_mesh_uv_layout/__init__.py b/io_mesh_uv_layout/__init__.py
index 324f2254..ceb5ddbc 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, 2),
+    "blender": (3, 0, 0),
     "location": "Image-Window > UVs > Export UV Layout",
     "description": "Export the UV layout as a 2D graphic",
     "warning": "",
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



More information about the Bf-extensions-cvs mailing list