Skip to content

Commit

Permalink
Replace BGL with GPU module for 4.0
Browse files Browse the repository at this point in the history
Completely removes BGL module and replaces with the new GPU module so it works with 4.0
  • Loading branch information
CodyWinch committed Nov 15, 2023
1 parent 2e42d44 commit 25192cb
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 104 deletions.
4 changes: 2 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
bl_info = {
"name": "Abnormal",
"author": "Cody Winchester (codywinch)",
"version": (1, 1, 3),
"blender": (3, 60, 0),
"version": (1, 1, 4),
"blender": (4, 0, 0),
"location": "3D View > N Panel/Header > BNPR Abnormal Tab",
"description": "BNPR Normal Editing Tools",
"warning": "",
Expand Down
13 changes: 8 additions & 5 deletions classes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import bpy
import bgl
import gpu
import numpy as np
from gpu_extras.batch import batch_for_shader
Expand Down Expand Up @@ -78,7 +77,11 @@ def __init__(self, mat, alt_shader=False):
return

def create_alt_shader(self):
self.shader = gpu.shader.from_builtin('3D_FLAT_COLOR')
if bpy.app.version[0] >= 4:
shader_3d_str = 'FLAT_COLOR'
else:
shader_3d_str = '3D_FLAT_COLOR'
self.shader = gpu.shader.from_builtin(shader_3d_str)
return

def create_shader(self):
Expand Down Expand Up @@ -395,18 +398,18 @@ def draw(self):
self.shader.bind()
self.batch_tri.draw(self.shader)

bgl.glPointSize(5*self.size)
gpu.state.point_size_set(5*self.size)
# Static Non Sel Points
self.shader.bind()
self.batch_po.draw(self.shader)

# Static Sel Points
bgl.glPointSize(8*self.size)
gpu.state.point_size_set(8*self.size)
self.shader.bind()
self.batch_po_sel.draw(self.shader)

# Static Act Points
bgl.glPointSize(11*self.size)
gpu.state.point_size_set(11*self.size)
self.shader.bind()
self.batch_po_act.draw(self.shader)

Expand Down
43 changes: 22 additions & 21 deletions cui_classes/cui_bezier_items.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bgl
import gpu
from gpu_extras.batch import batch_for_shader
from .cui_functions import *
Expand Down Expand Up @@ -126,19 +125,20 @@ def draw(self, color_override=None, click_down=False):
if self.visible:
super().draw()

bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set('ALPHA')

self.shader.bind()
bgl.glLineWidth(self.line_thickness)

gpu.state.line_width_set(self.line_thickness)
self.shader.uniform_float("color", self.color_axis_lines_render)
self.batch_lines.draw(self.shader)

self.shader.bind()
bgl.glLineWidth(self.inbtwn_thickness)
gpu.state.line_width_set(self.inbtwn_thickness)
self.shader.uniform_float("color", self.color_inbtwn_lines_render)
self.batch_inbtwn_lines.draw(self.shader)

bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
return

#
Expand Down Expand Up @@ -747,13 +747,10 @@ def click_down_move(self, mouse_co, shift, position, arguments=None):

def draw(self):
# Get a currently in use scissor to reenable after the new scissor
cur_scissor = None
if bgl.glIsEnabled(bgl.GL_SCISSOR_TEST) == 1:
cur_scissor = bgl.Buffer(bgl.GL_INT, 4)
bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, cur_scissor)
cur_scissor = gpu.state.scissor_get()

bgl.glEnable(bgl.GL_SCISSOR_TEST)
bgl.glScissor(
gpu.state.scissor_test_set(True)
gpu.state.scissor_set(
int(round(self.final_pos[0])),
int(round(self.final_pos[1]-self.scale_height)),
int(round(self.scale_width)),
Expand All @@ -768,12 +765,12 @@ def draw(self):

# Disable scissor as there is no previous scissor to continue with
if cur_scissor is None:
bgl.glDisable(bgl.GL_SCISSOR_TEST)
gpu.state.scissor_test_set(False)

# Reenable previous scissor
else:
bgl.glEnable(bgl.GL_SCISSOR_TEST)
bgl.glScissor(
gpu.state.scissor_test_set(True)
gpu.state.scissor_set(
cur_scissor[0], cur_scissor[1], cur_scissor[2], cur_scissor[3])
return

Expand Down Expand Up @@ -1315,7 +1312,11 @@ class CUIBaseSpline:
# Basic spline class to be used for shape splines and fcurves
#
def __init__(self):
self.shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR')
if bpy.app.version[0] >= 4:
shader_2d_str = 'UNIFORM_COLOR'
else:
shader_2d_str = '2D_UNIFORM_COLOR'
self.shader = gpu.shader.from_builtin(shader_2d_str)

self.scale = 1.0

Expand Down Expand Up @@ -1440,14 +1441,14 @@ def update_color_render(self):
return

def draw(self):
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set('ALPHA')

bgl.glLineWidth(self.spline_thickness)
gpu.state.line_width_set(self.spline_thickness)
self.shader.bind()
self.shader.uniform_float("color", self.color_render)
self.batch.draw(self.shader)

bgl.glLineWidth(self.handle_thickness)
gpu.state.line_width_set(self.handle_thickness)
self.shader.bind()
self.shader.uniform_float("color", self.color_handles_render)
self.batch_handles.draw(self.shader)
Expand All @@ -1460,7 +1461,7 @@ def draw(self):
self.shader.uniform_float("color", self.color_pos_sel_render)
self.batch_pos_sel.draw(self.shader)

bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')

return

Expand Down Expand Up @@ -1722,11 +1723,11 @@ def update_color_render(self):
return

def draw(self):
bgl.glEnable(bgl.GL_BLEND)
gpu.state.blend_set('ALPHA')
self.shader.bind()
self.shader.uniform_float("color", self.color_area_render)
self.batch_area.draw(self.shader)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')

super().draw()

Expand Down
19 changes: 7 additions & 12 deletions cui_classes/cui_containers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import bgl
from .cui_shapes import *
from .cui_bezier_items import *
from .cui_items import *
Expand Down Expand Up @@ -239,10 +238,7 @@ def draw(self):
# Some test code is there for multi scissor boxes if I ever intend to try again
if self.scrolling and (self.type == 'PANEL' or 'POPUP' in self.type):
# Get a currently in use scissor to reenable after the new scissor
cur_scissor = None
if bgl.glIsEnabled(bgl.GL_SCISSOR_TEST) == 1:
cur_scissor = bgl.Buffer(bgl.GL_INT, 4)
bgl.glGetIntegerv(bgl.GL_SCISSOR_BOX, cur_scissor)
cur_scissor = gpu.state.scissor_get()

# clip_pos = pos.copy()
# clip_pos[0] += self.horizontal_margin
Expand All @@ -252,11 +248,10 @@ def draw(self):
# if pos[1]+self.scroll_offset > position[1]:
# offset = pos[1]+self.scroll_offset-position[1]

bgl.glEnable(bgl.GL_SCISSOR_TEST)
bgl.glScissor(
gpu.state.scissor_test_set(True)
gpu.state.scissor_set(
int(round(self.final_pos[0]+self.horizontal_margin)),
int(round(
self.final_pos[1]-self.scale_height+self.vertical_margin)),
int(round(self.final_pos[1]-self.scale_height+self.vertical_margin)),
int(round((self.scale_width-self.horizontal_margin*2))),
int(round((self.scale_height-self.vertical_margin*2-offset)))
)
Expand All @@ -268,12 +263,12 @@ def draw(self):
if self.scrolling and (self.type == 'PANEL' or 'POPUP' in self.type):
# Disable scissor as there is no previous scissor to continue with
if cur_scissor is None:
bgl.glDisable(bgl.GL_SCISSOR_TEST)
gpu.state.scissor_test_set(False)

# Reenable previous scissor
else:
bgl.glEnable(bgl.GL_SCISSOR_TEST)
bgl.glScissor(
gpu.state.scissor_test_set(True)
gpu.state.scissor_set(
cur_scissor[0], cur_scissor[1], cur_scissor[2], cur_scissor[3])

return
Expand Down
17 changes: 12 additions & 5 deletions cui_classes/cui_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@


def scale_font_size(text, font_size, font_id, scale):

blf.size(font_id, font_size, 72)
if bpy.app.version[0] >= 4:
blf.size(font_id, font_size)
else:
blf.size(font_id, font_size, 72)
blf.position(font_id, 0, 0, 0)
size_w = blf.dimensions(font_id, text)

Expand All @@ -23,14 +25,19 @@ def scale_font_size(text, font_size, font_id, scale):
# A size is found that is just over the target width of the scaled size
while cur_width <= targ_width:
cur_size += 1

blf.size(font_id, cur_size, 72)
if bpy.app.version[0] >= 4:
blf.size(font_id, cur_size)
else:
blf.size(font_id, cur_size, 72)
cur_width, cur_height = blf.dimensions(
font_id, text)

# When the font size that is just larger is found set the current font size to 1 below that
cur_size -= 1
blf.size(font_id, cur_size, 72)
if bpy.app.version[0] >= 4:
blf.size(font_id, cur_size)
else:
blf.size(font_id, cur_size, 72)
size_w = blf.dimensions(font_id, text)

return cur_size
Expand Down
47 changes: 25 additions & 22 deletions cui_classes/cui_items.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import bpy
import blf
import bgl
import gpu
from gpu_extras.batch import batch_for_shader
from .cui_functions import *
Expand Down Expand Up @@ -224,7 +223,11 @@ def __init__(self, height, text):
self.text_auto_y = True
self.text_margin = 2

self.shader_img = gpu.shader.from_builtin('2D_IMAGE')
if bpy.app.version[0] >= 4:
shader_img_str = 'IMAGE'
else:
shader_img_str = '2D_IMAGE'
self.shader_img = gpu.shader.from_builtin(shader_img_str)

self.icon_pos = np.array([0.0, 0.0, 0.0, 0.0], dtype=np.float32)
self.icon_pos_offset = np.array([0.0, 0.0], dtype=np.float32)
Expand Down Expand Up @@ -275,8 +278,10 @@ def create_shape_data(self, value='', text=None):
if self.text_render != '':
cur_size = scale_font_size(
self.text_render, self.font_size, self.font_id, self.scale)

blf.size(self.font_id, cur_size, 72)
if bpy.app.version[0] >= 4:
blf.size(self.font_id, cur_size)
else:
blf.size(self.font_id, cur_size, 72)
size_w = blf.dimensions(self.font_id, self.text_render)
size_h = blf.dimensions(self.font_id, 'T')

Expand Down Expand Up @@ -426,18 +431,20 @@ def icon_draw(self):
if icon_img.gl_load():
raise Exception()

bgl.glEnable(bgl.GL_BLEND)
bgl.glActiveTexture(bgl.GL_TEXTURE0)
bgl.glBindTexture(bgl.GL_TEXTURE_2D, icon_img.bindcode)
gpu.state.blend_set('ALPHA')
texture = gpu.texture.from_image(icon_img)
self.shader_img.bind()
self.shader_img.uniform_int("image", 0)
self.shader_img.uniform_sampler("image", texture)
self.batch_icon.draw(self.shader_img)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')
return

def text_draw(self):
if self.visible and self.text_render != '':
blf.size(self.font_id, self.scale_font_size, 72)
if bpy.app.version[0] >= 4:
blf.size(self.font_id, self.scale_font_size)
else:
blf.size(self.font_id, self.scale_font_size, 72)
blf.position(self.font_id, self.text_pos[0], self.text_pos[1], 0)
blf.color(
self.font_id, self.color_font_render[0], self.color_font_render[1], self.color_font_render[2], self.color_font_render[3])
Expand Down Expand Up @@ -600,12 +607,12 @@ def draw(self, color_override=None, click_down=False):

if self.bool_val and self.draw_check:

bgl.glEnable(bgl.GL_BLEND)
bgl.glLineWidth(self.bool_thickness)
gpu.state.blend_set('ALPHA')
gpu.state.line_width_set(self.bool_thickness)
self.shader.bind()
self.shader.uniform_float("color", self.color_check_render)
self.batch_check.draw(self.shader)
bgl.glDisable(bgl.GL_BLEND)
gpu.state.blend_set('NONE')

return

Expand Down Expand Up @@ -1734,11 +1741,9 @@ def __init__(self, mat, size, scale):

def draw(self):
if self.visible:
# bgl.glDepthRange(0, 0.01)
for i in range(len(self.gizmos)):
if self.gizmos[i*-1-1].active:
self.gizmos[i*-1-1].draw()
# bgl.glDepthRange(0, 1.0)
return

def create_shape_data(self, matrix):
Expand Down Expand Up @@ -1839,7 +1844,11 @@ def __str__(self):

class CUIGizmo:
def __init__(self, size, scale, axis, giz_type, color):
self.shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
if bpy.app.version[0] >= 4:
shader_3d_str = 'UNIFORM_COLOR'
else:
shader_3d_str = '3D_UNIFORM_COLOR'
self.shader = gpu.shader.from_builtin(shader_3d_str)

self.axis = axis
self.type = giz_type
Expand All @@ -1863,7 +1872,6 @@ def __init__(self, size, scale, axis, giz_type, color):

def draw(self):
if self.active:
# bgl.glDepthRange(0, 0.01)

self.shader.bind()
if self.hover and self.in_use == False:
Expand All @@ -1872,8 +1880,6 @@ def draw(self):
self.shader.uniform_float("color", self.color)
self.batch.draw(self.shader)

# bgl.glDepthRange(0, 1.0)

return

#
Expand Down Expand Up @@ -1961,7 +1967,6 @@ def __init__(self, size, scale, axis, giz_type, color, thickness=6):

def draw(self):
if self.active:
# bgl.glDepthRange(0, 0.01)

if self.in_use:
self.shader.bind()
Expand All @@ -1980,8 +1985,6 @@ def draw(self):
self.shader.uniform_float("color", self.color)
self.batch.draw(self.shader)

# bgl.glDepthRange(0, 1.0)

return

#
Expand Down
Loading

0 comments on commit 25192cb

Please sign in to comment.