Collection Manager: Replace deprecated bgl module. Task: T69577 #104703

Merged
3 changed files with 76 additions and 61 deletions

View File

@ -6,8 +6,8 @@ bl_info = {
"name": "Collection Manager", "name": "Collection Manager",
"description": "Manage collections and their objects", "description": "Manage collections and their objects",
"author": "Ryan Inch", "author": "Ryan Inch",
"version": (2, 24, 4), "version": (2, 24, 5),
"blender": (2, 80, 0), "blender": (3, 0, 0),
"location": "View3D - Object Mode (Shortcut - M)", "location": "View3D - Object Mode (Shortcut - M)",
"warning": '', # used for warning icon and text in addons panel "warning": '', # used for warning icon and text in addons panel
"doc_url": "{BLENDER_MANUAL_URL}/addons/interface/collection_manager.html", "doc_url": "{BLENDER_MANUAL_URL}/addons/interface/collection_manager.html",

View File

@ -764,7 +764,7 @@ class CMSendReport(Operator):
if length > max_len: if length > max_len:
max_len = length max_len = length
return wm.invoke_popup(self, width=(30 + (max_len*5.5))) return wm.invoke_popup(self, width=int(30 + (max_len*5.5)))
def execute(self, context): def execute(self, context):
self.report({'INFO'}, self.message) self.report({'INFO'}, self.message)

View File

@ -5,7 +5,6 @@
import time import time
from math import cos, sin, pi, floor from math import cos, sin, pi, floor
import bpy import bpy
import bgl
import blf import blf
import gpu import gpu
from gpu_extras.batch import batch_for_shader from gpu_extras.batch import batch_for_shader
@ -91,15 +90,12 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
bl = round(bl * scale_factor()) bl = round(bl * scale_factor())
br = round(br * scale_factor()) br = round(br * scale_factor())
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
if outline: if outline:
thickness = round(2 * scale_factor()) thickness = round(2 * scale_factor())
thickness = max(thickness, 2) thickness = max(thickness, 2)
shader.uniform_float("lineWidth", thickness)
bgl.glLineWidth(thickness)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
draw_type = 'TRI_FAN' if not outline else 'LINE_STRIP' draw_type = 'TRI_FAN' if not outline else 'LINE_STRIP'
@ -115,10 +111,15 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
sine = tl * sin(side * 2 * pi / sides) + vert_y sine = tl * sin(side * 2 * pi / sides) + vert_y
vertices.append((cosine,sine)) vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices}) if not outline:
shader.bind() batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.uniform_float("color", color) shader.bind()

shader.bind() is no longer needed here.

`shader.bind()` is no longer needed here.
batch.draw(shader) shader.uniform_float("color", color)
batch.draw(shader)
else:
batch = batch_for_shader(shader, draw_type, {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})

This looks like a change unrelated to the removal of the bgl module.
It should be in a separate commit.

This looks like a change unrelated to the removal of the bgl module. It should be in a separate commit.
Review

No, this is related. I needed to add a separate line shader to work with the new approved methods, so this handles that.

No, this is related. I needed to add a separate line shader to work with the new approved methods, so this handles that.
shader.bind()

shader.bind() is no needed here.

`shader.bind()` is no needed here.
batch.draw(shader)
# top right corner # top right corner
vert_x = area["vert"][0] + area["width"] - tr vert_x = area["vert"][0] + area["width"] - tr
@ -132,10 +133,15 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
sine = tr * sin(side * 2 * pi / sides) + vert_y sine = tr * sin(side * 2 * pi / sides) + vert_y
vertices.append((cosine,sine)) vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices}) if not outline:
shader.bind() batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.uniform_float("color", color) shader.bind()

shader.bind() is no longer needed here.

shader.bind() is no longer needed here.
batch.draw(shader) shader.uniform_float("color", color)
batch.draw(shader)
else:
batch = batch_for_shader(shader, draw_type, {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()

shader.bind() is no needed here.

shader.bind() is no needed here.
batch.draw(shader)
# bottom left corner # bottom left corner
vert_x = area["vert"][0] + bl vert_x = area["vert"][0] + bl
@ -149,10 +155,15 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
sine = bl * sin(side * 2 * pi / sides) + vert_y sine = bl * sin(side * 2 * pi / sides) + vert_y
vertices.append((cosine,sine)) vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices}) if not outline:
shader.bind() batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.uniform_float("color", color) shader.bind()
batch.draw(shader) shader.uniform_float("color", color)
batch.draw(shader)
else:
batch = batch_for_shader(shader, draw_type, {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
batch.draw(shader)
# bottom right corner # bottom right corner
vert_x = area["vert"][0] + area["width"] - br vert_x = area["vert"][0] + area["width"] - br
@ -166,10 +177,15 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
sine = br * sin(side * 2 * pi / sides) + vert_y sine = br * sin(side * 2 * pi / sides) + vert_y
vertices.append((cosine,sine)) vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices}) if not outline:
shader.bind() batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.uniform_float("color", color) shader.bind()
batch.draw(shader) shader.uniform_float("color", color)
batch.draw(shader)
else:
batch = batch_for_shader(shader, draw_type, {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
batch.draw(shader)
if not outline: if not outline:
vertices = [] vertices = []
@ -249,6 +265,7 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
]) ])
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)
shader.bind()
shader.uniform_float("color", color) shader.uniform_float("color", color)
batch.draw(shader) batch.draw(shader)
@ -263,7 +280,8 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
(le_x, bl_vert[1] - (overlap if bl == 0 else 0)) (le_x, bl_vert[1] - (overlap if bl == 0 else 0))
] ]
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices}) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()

shader.bind() is no needed here.

shader.bind() is no needed here.
batch.draw(shader) batch.draw(shader)
# right edge # right edge
@ -273,7 +291,8 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
(re_x, br_vert[1] - (overlap if br == 0 else 0)) (re_x, br_vert[1] - (overlap if br == 0 else 0))
] ]
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices}) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
batch.draw(shader) batch.draw(shader)
# top edge # top edge
@ -283,7 +302,8 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
(tr_vert[0] + (overlap if tr == 0 else 0), te_y) (tr_vert[0] + (overlap if tr == 0 else 0), te_y)
] ]
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices}) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
batch.draw(shader) batch.draw(shader)
# bottom edge # bottom edge
@ -293,12 +313,12 @@ def draw_rounded_rect(area, shader, color, tl=5, tr=5, bl=5, br=5, outline=False
(br_vert[0] + (overlap if br == 0 else 0), be_y) (br_vert[0] + (overlap if br == 0 else 0), be_y)
] ]
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices}) batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
batch.draw(shader) batch.draw(shader)
bgl.glDisable(bgl.GL_LINE_SMOOTH)
bgl.glDisable(bgl.GL_BLEND) gpu.state.blend_set('NONE')
def mouse_in_area(mouse_pos, area, buf = 0): def mouse_in_area(mouse_pos, area, buf = 0):
x = mouse_pos[0] x = mouse_pos[0]
@ -741,7 +761,7 @@ def draw_callback_px(self, context):
allocate_main_ui(self, context) allocate_main_ui(self, context)
shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
shader.bind() line_shader = gpu.shader.from_builtin('3D_POLYLINE_SMOOTH_COLOR')
addon_prefs = context.preferences.addons[__package__].preferences addon_prefs = context.preferences.addons[__package__].preferences
@ -749,7 +769,7 @@ def draw_callback_px(self, context):
main_window = self.areas["Main Window"] main_window = self.areas["Main Window"]
outline_color = addon_prefs.qcd_ogl_widget_menu_back_outline outline_color = addon_prefs.qcd_ogl_widget_menu_back_outline
background_color = addon_prefs.qcd_ogl_widget_menu_back_inner background_color = addon_prefs.qcd_ogl_widget_menu_back_inner
draw_rounded_rect(main_window, shader, outline_color[:] + (1,), outline=True) draw_rounded_rect(main_window, line_shader, outline_color[:] + (1,), outline=True)
draw_rounded_rect(main_window, shader, background_color) draw_rounded_rect(main_window, shader, background_color)
# draw window title # draw window title
@ -765,9 +785,6 @@ def draw_callback_px(self, context):
blf.color(font_id, text_color[0], text_color[1], text_color[2], 1) blf.color(font_id, text_color[0], text_color[1], text_color[2], 1)
blf.draw(font_id, text) blf.draw(font_id, text)
# refresh shader - not sure why this is needed
shader.bind()
in_tooltip_area = False in_tooltip_area = False
tooltip_slot_idx = None tooltip_slot_idx = None
@ -833,7 +850,7 @@ def draw_callback_px(self, context):
# draw button # draw button
outline_color = addon_prefs.qcd_ogl_widget_tool_outline outline_color = addon_prefs.qcd_ogl_widget_tool_outline
draw_rounded_rect(button_area, shader, outline_color[:] + (1,), tl, tr, bl, br, outline=True) draw_rounded_rect(button_area, line_shader, outline_color[:] + (1,), tl, tr, bl, br, outline=True)
draw_rounded_rect(button_area, shader, button_color, tl, tr, bl, br) draw_rounded_rect(button_area, shader, button_color, tl, tr, bl, br)
# ACTIVE OBJECT # ACTIVE OBJECT
@ -841,14 +858,15 @@ def draw_callback_px(self, context):
active_object_indicator = self.areas[f"Button {slot_num} Active Object Indicator"] active_object_indicator = self.areas[f"Button {slot_num} Active Object Indicator"]
vertices = get_circle_coords(active_object_indicator) vertices = get_circle_coords(active_object_indicator)
shader.uniform_float("color", icon_color[:] + (1,))
batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices}) batch = batch_for_shader(shader, 'TRI_FAN', {"pos": vertices})
shader.bind()
shader.uniform_float("color", icon_color[:] + (1,))
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
batch.draw(shader) batch.draw(shader)
bgl.glDisable(bgl.GL_BLEND) gpu.state.blend_set('NONE')
# SELECTED OBJECTS # SELECTED OBJECTS
elif not set(selected_objects).isdisjoint(collection_objects): elif not set(selected_objects).isdisjoint(collection_objects):
@ -856,18 +874,16 @@ def draw_callback_px(self, context):
alpha = addon_prefs.qcd_ogl_selected_icon_alpha alpha = addon_prefs.qcd_ogl_selected_icon_alpha
vertices = get_circle_coords(selected_object_indicator) vertices = get_circle_coords(selected_object_indicator)
shader.uniform_float("color", icon_color[:] + (alpha,)) line_shader.uniform_float("lineWidth", 2 * scale_factor())
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices}) color = icon_color[:] + (alpha,)
batch = batch_for_shader(line_shader, 'LINE_STRIP', {"pos": [(v[0], v[1], 0) for v in vertices], "color": [color for v in vertices]})
shader.bind()
bgl.glLineWidth(2 * scale_factor()) gpu.state.blend_set('ALPHA')
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
batch.draw(shader) batch.draw(line_shader)
bgl.glDisable(bgl.GL_LINE_SMOOTH) gpu.state.blend_set('NONE')
bgl.glDisable(bgl.GL_BLEND)
# OBJECTS # OBJECTS
elif collection_objects: elif collection_objects:
@ -875,14 +891,15 @@ def draw_callback_px(self, context):
alpha = addon_prefs.qcd_ogl_objects_icon_alpha alpha = addon_prefs.qcd_ogl_objects_icon_alpha
vertices, indices = get_coords(object_indicator) vertices, indices = get_coords(object_indicator)
shader.uniform_float("color", icon_color[:] + (alpha,))
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)
shader.bind()
shader.uniform_float("color", icon_color[:] + (alpha,))
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
batch.draw(shader) batch.draw(shader)
bgl.glDisable(bgl.GL_BLEND) gpu.state.blend_set('NONE')
# X ICON # X ICON
@ -891,17 +908,15 @@ def draw_callback_px(self, context):
X_icon_color = addon_prefs.qcd_ogl_widget_menu_back_text X_icon_color = addon_prefs.qcd_ogl_widget_menu_back_text
vertices, indices = get_x_coords(X_icon) vertices, indices = get_x_coords(X_icon)
shader.uniform_float("color", X_icon_color[:] + (1,))
batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices) batch = batch_for_shader(shader, 'TRIS', {"pos": vertices}, indices=indices)
shader.bind()
shader.uniform_float("color", X_icon_color[:] + (1,))
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
bgl.glEnable(bgl.GL_POLYGON_SMOOTH)
bgl.glHint(bgl.GL_POLYGON_SMOOTH_HINT, bgl.GL_NICEST)
batch.draw(shader) batch.draw(shader)
bgl.glDisable(bgl.GL_POLYGON_SMOOTH) gpu.state.blend_set('NONE')
bgl.glDisable(bgl.GL_BLEND)
if in_tooltip_area: if in_tooltip_area:
if self.draw_tooltip: if self.draw_tooltip:
@ -912,7 +927,7 @@ def draw_callback_px(self, context):
" * Shift+LMB - Toggle objects\' slot." " * Shift+LMB - Toggle objects\' slot."
) )
draw_tooltip(self, context, shader, f"{slot_string}{hotkey_string}") draw_tooltip(self, context, shader, line_shader, f"{slot_string}{hotkey_string}")
self.hover_time = None self.hover_time = None
@ -921,7 +936,7 @@ def draw_callback_px(self, context):
self.hover_time = time.time() self.hover_time = time.time()
def draw_tooltip(self, context, shader, message): def draw_tooltip(self, context, shader, line_shader, message):
addon_prefs = context.preferences.addons[__package__].preferences addon_prefs = context.preferences.addons[__package__].preferences
font_id = 0 font_id = 0
@ -962,7 +977,7 @@ def draw_tooltip(self, context, shader, message):
outline_color = addon_prefs.qcd_ogl_widget_tooltip_outline outline_color = addon_prefs.qcd_ogl_widget_tooltip_outline
background_color = addon_prefs.qcd_ogl_widget_tooltip_inner background_color = addon_prefs.qcd_ogl_widget_tooltip_inner
draw_rounded_rect(tooltip, shader, outline_color[:] + (1,), outline=True) draw_rounded_rect(tooltip, line_shader, outline_color[:] + (1,), outline=True)
draw_rounded_rect(tooltip, shader, background_color) draw_rounded_rect(tooltip, shader, background_color)
line_pos = padding + line_height line_pos = padding + line_height