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

Merged
3 changed files with 78 additions and 63 deletions
Showing only changes of commit 68c40c2a74 - Show all commits

View File

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

View File

@ -764,7 +764,7 @@ class CMSendReport(Operator):
if length > max_len:
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):
self.report({'INFO'}, self.message)

View File

@ -5,7 +5,6 @@
import time
from math import cos, sin, pi, floor
import bpy
import bgl
import blf
import gpu
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())
br = round(br * scale_factor())
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set('ALPHA')
if outline:
thickness = round(2 * scale_factor())
thickness = max(thickness, 2)
bgl.glLineWidth(thickness)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
thickness = round(1 * scale_factor())
thickness = max(thickness, 1)

The thickness value has changed. Intentional?

The thickness value has changed. Intentional?

It was intentional, because the gpu line shader gave different results to what I had with bgl, but on second thought I'm going to revert the outline thickness and draw order changes.

It was intentional, because the gpu line shader gave different results to what I had with bgl, but on second thought I'm going to revert the outline thickness and draw order changes.
shader.uniform_float("lineWidth", thickness)
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
vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
if not outline:
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()

shader.bind() is no longer needed here.

`shader.bind()` is no longer needed here.
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
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
vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
if not outline:
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()

shader.bind() is no longer needed here.

shader.bind() is no longer needed here.
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
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
vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
if not outline:
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
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
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
vertices.append((cosine,sine))
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
if not outline:
batch = batch_for_shader(shader, draw_type, {"pos": vertices})
shader.bind()
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:
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)
shader.bind()
shader.uniform_float("color", color)
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))
]
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)
# 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))
]
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)
# 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)
]
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)
# 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)
]
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)
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):
x = mouse_pos[0]
@ -741,7 +761,7 @@ def draw_callback_px(self, context):
allocate_main_ui(self, context)
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
@ -749,8 +769,8 @@ def draw_callback_px(self, context):
main_window = self.areas["Main Window"]
outline_color = addon_prefs.qcd_ogl_widget_menu_back_outline
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, shader, background_color)
draw_rounded_rect(main_window, line_shader, outline_color[:] + (1,), outline=True)
# draw window title
window_title = self.areas["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.draw(font_id, text)
# refresh shader - not sure why this is needed
shader.bind()
in_tooltip_area = False
tooltip_slot_idx = None
@ -833,22 +850,23 @@ def draw_callback_px(self, context):
# draw button
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, shader, button_color, tl, tr, bl, br)
draw_rounded_rect(button_area, line_shader, outline_color[:] + (1,), tl, tr, bl, br, outline=True)
# ACTIVE OBJECT
if active_object and active_object in selected_objects and active_object.name in collection_objects:
active_object_indicator = self.areas[f"Button {slot_num} 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})
shader.bind()
shader.uniform_float("color", icon_color[:] + (1,))
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set('ALPHA')
batch.draw(shader)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
# SELECTED 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
vertices = get_circle_coords(selected_object_indicator)
shader.uniform_float("color", icon_color[:] + (alpha,))
batch = batch_for_shader(shader, 'LINE_STRIP', {"pos": vertices})
line_shader.uniform_float("lineWidth", 2 * scale_factor())
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())
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glHint(bgl.GL_LINE_SMOOTH_HINT, bgl.GL_NICEST)
gpu.state.blend_set('ALPHA')
batch.draw(shader)
batch.draw(line_shader)
bgl.glDisable(bgl.GL_LINE_SMOOTH)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
# OBJECTS
elif collection_objects:
@ -875,14 +891,15 @@ def draw_callback_px(self, context):
alpha = addon_prefs.qcd_ogl_objects_icon_alpha
vertices, indices = get_coords(object_indicator)
shader.uniform_float("color", icon_color[:] + (alpha,))
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)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
# X ICON
@ -891,17 +908,15 @@ def draw_callback_px(self, context):
X_icon_color = addon_prefs.qcd_ogl_widget_menu_back_text
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)
shader.bind()
shader.uniform_float("color", X_icon_color[:] + (1,))
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_POLYGON_SMOOTH)
bgl.glHint(bgl.GL_POLYGON_SMOOTH_HINT, bgl.GL_NICEST)
gpu.state.blend_set('ALPHA')
batch.draw(shader)
bgl.glDisable(bgl.GL_POLYGON_SMOOTH)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
if in_tooltip_area:
if self.draw_tooltip:
@ -912,7 +927,7 @@ def draw_callback_px(self, context):
" * 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
@ -921,7 +936,7 @@ def draw_callback_px(self, context):
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
font_id = 0
@ -962,8 +977,8 @@ def draw_tooltip(self, context, shader, message):
outline_color = addon_prefs.qcd_ogl_widget_tooltip_outline
background_color = addon_prefs.qcd_ogl_widget_tooltip_inner
draw_rounded_rect(tooltip, shader, outline_color[:] + (1,), outline=True)
draw_rounded_rect(tooltip, shader, background_color)
draw_rounded_rect(tooltip, line_shader, outline_color[:] + (1,), outline=True)
line_pos = padding + line_height
# draw text