replace bgl with gpu calls update for 4.0

3.0.0

- Update for Blender 4.0 (Breaking release, removed bgl to use gpu)
- fixed: openGL draw camera frame and passepartout
gpv2
pullusb 2024-02-20 16:07:20 +01:00
parent 4fadf35163
commit cf2ba8448a
5 changed files with 32 additions and 35 deletions

View File

@ -1,5 +1,10 @@
# Changelog # Changelog
3.0.0
- Update for Blender 4.0 (Breaking release, removed bgl to use gpu)
- fixed: openGL draw camera frame and passepartout
2.5.0 2.5.0
- added: Animation manager new button `Frame Select Step` (sort of a checker deselect, but in GP dopesheet) - added: Animation manager new button `Frame Select Step` (sort of a checker deselect, but in GP dopesheet)

View File

@ -1,6 +1,5 @@
import bpy import bpy
from bpy.types import Operator from bpy.types import Operator
import bgl
from gpu_extras.presets import draw_circle_2d from gpu_extras.presets import draw_circle_2d
from gpu_extras.batch import batch_for_shader from gpu_extras.batch import batch_for_shader
import gpu import gpu
@ -190,7 +189,7 @@ class GPTB_OT_eraser(Operator):
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
def draw_callback_px(self): def draw_callback_px(self):
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
#bgl.glBlendFunc(bgl.GL_CONSTANT_ALPHA, bgl.GL_ONE_MINUS_CONSTANT_ALPHA) #bgl.glBlendFunc(bgl.GL_CONSTANT_ALPHA, bgl.GL_ONE_MINUS_CONSTANT_ALPHA)
#bgl.glBlendColor(1.0, 1.0, 1.0, 0.1) #bgl.glBlendColor(1.0, 1.0, 1.0, 0.1)
@ -201,7 +200,7 @@ class GPTB_OT_eraser(Operator):
bg_color = area.spaces.active.shading.background_color bg_color = area.spaces.active.shading.background_color
#print(bg_color) #print(bg_color)
shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') shader = gpu.shader.from_builtin('POLYLINE_UNIFORM_COLOR')
shader.bind() shader.bind()
shader.uniform_float("color", (1, 1, 1, 1)) shader.uniform_float("color", (1, 1, 1, 1))
for mouse, radius in self.mouse_path: for mouse, radius in self.mouse_path:
@ -210,7 +209,7 @@ class GPTB_OT_eraser(Operator):
batch.draw(shader) batch.draw(shader)
draw_circle_2d(self.mouse, (0.75, 0.25, 0.35, 1.0), self.radius, 24) draw_circle_2d(self.mouse, (0.75, 0.25, 0.35, 1.0), self.radius, 24)
bgl.glDisable(bgl.GL_BLEND) gpu.state.blend_set('NONE')

View File

@ -4,8 +4,8 @@ bl_info = {
"name": "GP toolbox", "name": "GP toolbox",
"description": "Tool set for Grease Pencil in animation production", "description": "Tool set for Grease Pencil in animation production",
"author": "Samuel Bernou, Christophe Seux", "author": "Samuel Bernou, Christophe Seux",
"version": (2, 5, 0), "version": (3, 0, 0),
"blender": (3, 0, 0), "blender": (4, 0, 0),
"location": "Sidebar (N menu) > Gpencil > Toolbox / Gpencil properties", "location": "Sidebar (N menu) > Gpencil > Toolbox / Gpencil properties",
"warning": "", "warning": "",
"doc_url": "https://gitlab.com/autour-de-minuit/blender/gp_toolbox", "doc_url": "https://gitlab.com/autour-de-minuit/blender/gp_toolbox",

View File

@ -165,7 +165,6 @@ def randomise_points(mat, points, attr, strength) :
setattr(point,attr,value+random*strength) setattr(point,attr,value+random*strength)
def zoom_to_object(cam, resolution, box, margin=0.01) : def zoom_to_object(cam, resolution, box, margin=0.01) :
min_x= box[0] min_x= box[0]
max_x= box[1] max_x= box[1]
@ -216,25 +215,6 @@ def zoom_to_object(cam, resolution, box, margin=0.01) :
#print(matrix,resolution) #print(matrix,resolution)
return modelview_matrix,projection_matrix,frame,resolution return modelview_matrix,projection_matrix,frame,resolution
def set_viewport_matrix(width, height, mat):
from bgl import glViewport,glMatrixMode,GL_PROJECTION,glLoadMatrixf,Buffer,GL_FLOAT,glMatrixMode,GL_MODELVIEW,glLoadIdentity
glViewport(0,0,width,height)
#glLoadIdentity()
glMatrixMode(GL_PROJECTION)
projection = [mat[j][i] for i in range(4) for j in range(4)]
glLoadMatrixf(Buffer(GL_FLOAT, 16, projection))
#glMatrixMode( GL_MODELVIEW )
#glLoadIdentity()
# get object info # get object info
def get_object_info(mesh_groups, order_list = []) : def get_object_info(mesh_groups, order_list = []) :
scene = bpy.context.scene scene = bpy.context.scene

View File

@ -1,6 +1,5 @@
import bpy import bpy
import gpu import gpu
import bgl
# import blf # import blf
from gpu_extras.batch import batch_for_shader from gpu_extras.batch import batch_for_shader
from bpy_extras.view3d_utils import location_3d_to_region_2d from bpy_extras.view3d_utils import location_3d_to_region_2d
@ -30,6 +29,20 @@ def view3d_camera_border_2d(context, cam):
frame_px = [location_3d_to_region_2d(region, rv3d, v) for v in frame] frame_px = [location_3d_to_region_2d(region, rv3d, v) for v in frame]
return frame_px return frame_px
def vertices_to_line_loop(v_list, closed=True) -> list:
'''Take a sequence of vertices
return a position lists of segments to create a line loop passing in all points
the result is usable with gpu_shader 'LINES'
ex: vlist = [a,b,c] -> closed=True return [a,b,b,c,c,a], closed=False return [a,b,b,c]
'''
loop = []
for i in range(len(v_list) - 1):
loop += [v_list[i], v_list[i + 1]]
if closed:
# Add segment between last and first to close loop
loop += [v_list[-1], v_list[0]]
return loop
def draw_cam_frame_callback_2d(): def draw_cam_frame_callback_2d():
context = bpy.context context = bpy.context
if context.region_data.view_perspective != 'CAMERA': if context.region_data.view_perspective != 'CAMERA':
@ -41,11 +54,12 @@ def draw_cam_frame_callback_2d():
if not main_cam: if not main_cam:
return return
bgl.glEnable(bgl.GL_BLEND) gpu.state.blend_set('ALPHA')
frame_point = view3d_camera_border_2d( frame_point = view3d_camera_border_2d(
context, context.scene.camera.parent) context, context.scene.camera.parent)
shader_2d = gpu.shader.from_builtin('2D_UNIFORM_COLOR') shader_2d = gpu.shader.from_builtin('UNIFORM_COLOR') # POLYLINE_FLAT_COLOR
# gpu.shader.from_builtin('2D_UNIFORM_COLOR')
if context.scene.gptoolprops.drawcam_passepartout: if context.scene.gptoolprops.drawcam_passepartout:
### PASSEPARTOUT ### PASSEPARTOUT
@ -109,8 +123,8 @@ def draw_cam_frame_callback_2d():
### Camera framing trace over ### Camera framing trace over
bgl.glLineWidth(1) gpu.state.line_width_set(1.0)
bgl.glEnable(bgl.GL_LINE_SMOOTH) # bgl.glEnable(bgl.GL_LINE_SMOOTH) # old smooth
""" """
## need to accurately detect viewport background color (difficult) ## need to accurately detect viewport background color (difficult)
@ -135,15 +149,14 @@ def draw_cam_frame_callback_2d():
frame_color = (0.0, 0.0, 0.25, 1.0) frame_color = (0.0, 0.0, 0.25, 1.0)
screen_framing = batch_for_shader( screen_framing = batch_for_shader(
shader_2d, 'LINE_LOOP', {"pos": frame_point}) shader_2d, 'LINES', {"pos": vertices_to_line_loop(frame_point)})
shader_2d.bind() shader_2d.bind()
shader_2d.uniform_float("color", frame_color) shader_2d.uniform_float("color", frame_color)
screen_framing.draw(shader_2d) screen_framing.draw(shader_2d)
# bgl.glLineWidth(1) # bgl.glDisable(bgl.GL_LINE_SMOOTH) # old smooth
bgl.glDisable(bgl.GL_LINE_SMOOTH) gpu.state.blend_set('NONE')
bgl.glDisable(bgl.GL_BLEND)
draw_handle = None draw_handle = None