UV Layout: replace deprecated bgl module

Part of T80730
This commit is contained in:
Germano Cavalcante 2022-07-21 13:03:25 -03:00
parent 294e899bf7
commit b56a6acb9f
Notes: blender-bot 2023-02-14 18:14:59 +01:00
Referenced by commit e6dcc0b1: Fix T101134: Regression: Export UV Layout is blurry
Referenced by commit 563ea27e: UV Layout: bring back smooth lines
Referenced by commit e6dcc0b1, Fix T101134: Regression: Export UV Layout is blurry
Referenced by commit 563ea27e, UV Layout: bring back smooth lines
Referenced by issue #101134, Regression: Export UV Layout is blurry
2 changed files with 12 additions and 18 deletions

View File

@ -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": "",

View File

@ -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