From 9646862bcd3943a64e9c8ca6b1facbbb70d6d0f4 Mon Sep 17 00:00:00 2001 From: CodeFHD Date: Tue, 7 Jan 2025 23:32:35 +0100 Subject: [PATCH] Update LoL to Blender API changes --- draw/lol/viewport.py | 50 +++++++++++---------------------- handlers/draw_imageeditor.py | 4 +-- operators/lol/add_local.py | 6 ++-- operators/lol/update_ToC.py | 2 +- operators/utils.py | 2 +- scripts/LOL/render_thumbnail.py | 4 +-- utils/lol/utils.py | 16 +++++++---- 7 files changed, 37 insertions(+), 47 deletions(-) diff --git a/draw/lol/viewport.py b/draw/lol/viewport.py index 64706bfa..9e126cc7 100644 --- a/draw/lol/viewport.py +++ b/draw/lol/viewport.py @@ -22,7 +22,7 @@ # # ##### -import bgl, blf +import blf import bpy import gpu import math @@ -49,6 +49,7 @@ def draw_progress(x, y, text='', percent=None, color=(0, 1, 0, 1)): def draw_rect(x, y, width, height, color): + gpu.state.blend_set('ALPHA') xmax = x + width ymax = y + height points = [[x, y], # [x, y] @@ -58,13 +59,13 @@ def draw_rect(x, y, width, height, color): ] indices = ((0, 1, 2), (2, 3, 0)) - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') + shader = gpu.shader.from_builtin('UNIFORM_COLOR') batch = batch_for_shader(shader, 'TRIS', {"pos": points}, indices=indices) shader.bind() shader.uniform_float("color", color) - bgl.glEnable(bgl.GL_BLEND) batch.draw(shader) + gpu.state.blend_set('NONE') def draw_line2d(x1, y1, x2, y2, width, color): @@ -73,31 +74,28 @@ def draw_line2d(x1, y1, x2, y2, width, color): indices = ( (0, 1),) - bgl.glEnable(bgl.GL_BLEND) - bgl.glEnable(bgl.GL_LINE_SMOOTH) - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') + gpu.state.blend_set('ALPHA') + shader = gpu.shader.from_builtin('UNIFORM_COLOR') batch = batch_for_shader(shader, 'LINES', {"pos": coords}, indices=indices) shader.bind() shader.uniform_float("color", color) batch.draw(shader) + gpu.state.blend_set('NONE') def draw_lines(vertices, indices, color): - bgl.glEnable(bgl.GL_BLEND) - bgl.glEnable(bgl.GL_LINE_SMOOTH) - bgl.glLineWidth(2) - - shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') + gpu.state.blend_set('ALPHA') + shader = gpu.shader.from_builtin('UNIFORM_COLOR') batch = batch_for_shader(shader, 'LINES', {"pos": vertices}, indices=indices) shader.bind() shader.uniform_float("color", color) batch.draw(shader) - + gpu.state.blend_set('NONE') def draw_rect_3d(coords, color): indices = [(0, 1, 2), (2, 3, 0)] - shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR') + shader = gpu.shader.from_builtin('UNIFORM_COLOR') batch = batch_for_shader(shader, 'TRIS', {"pos": coords}, indices=indices) shader.uniform_float("color", color) batch.draw(shader) @@ -146,8 +144,6 @@ def draw_bbox(location, rotation, bbox_min, bbox_max, progress=None, color=(0, 1 def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)): - # draw_rect(x,y, width, height, (.5,0,0,.5)) - coords = [ (x, y), (x + width, y), (x, y + height), (x + width, y + height)] @@ -160,32 +156,20 @@ def draw_image(x, y, width, height, image, transparency, crop=(0, 0, 1, 1)): indices = [(0, 1, 2), (2, 1, 3)] - shader = gpu.shader.from_builtin('2D_IMAGE') + gpu.state.blend_set('ALPHA') + shader = gpu.shader.from_builtin('IMAGE') batch = batch_for_shader(shader, 'TRIS', {"pos": coords, "texCoord": uvs}, indices=indices) - # send image to gpu if it isn't there already - if image.gl_load(): - raise Exception() - - # texture identifier on gpu - texture_id = image.bindcode - - # in case someone disabled it before - bgl.glEnable(bgl.GL_BLEND) - - # bind texture to image unit 0 - bgl.glActiveTexture(bgl.GL_TEXTURE0) - bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture_id) + texture = gpu.texture.from_image(image) shader.bind() - # tell shader to use the image that is bound to image unit 0 - shader.uniform_int("image", 0) + shader.uniform_sampler("image", texture) batch.draw(shader) - bgl.glDisable(bgl.GL_TEXTURE_2D) + gpu.state.blend_set('NONE') def draw_downloader(x, y, percent=0, img=None): @@ -205,7 +189,7 @@ def draw_text(text, x, y, size, color=(1, 1, 1, 0.5)): # bgl.glColor4f(*color) blf.color(font_id, color[0], color[1], color[2], color[3]) blf.position(font_id, x, y, 0) - blf.size(font_id, size, 72) + blf.size(font_id, size) blf.draw(font_id, text) diff --git a/handlers/draw_imageeditor.py b/handlers/draw_imageeditor.py index 3481e485..86a5dea5 100644 --- a/handlers/draw_imageeditor.py +++ b/handlers/draw_imageeditor.py @@ -88,7 +88,7 @@ def _draw_rect(x, y, width, height, color, view_to_region): co = ((x1, y1), (x2, y2), (x3, y3), (x4, y4)) indices = ((0, 1), (1, 2), (2, 3), (3, 0)) - shader = gpu.shader.from_builtin('2D_UNIFORM_COLOR') + shader = gpu.shader.from_builtin('UNIFORM_COLOR') batch = batch_for_shader(shader, 'LINES', {"pos": co}, indices=indices) shader.bind() shader.uniform_float("color", color) @@ -105,5 +105,5 @@ def _draw_text(text, x, y, color, view_to_region): r, g, b, a = color blf.position(font_id, pixelpos_x + offset, pixelpos_y + offset, 0) blf.color(font_id, r,g,b,a) - blf.size(font_id, text_size, dpi) + blf.size(font_id, text_size) blf.draw(font_id, text) diff --git a/operators/lol/add_local.py b/operators/lol/add_local.py index 6dffafdd..bda93fcd 100644 --- a/operators/lol/add_local.py +++ b/operators/lol/add_local.py @@ -77,7 +77,7 @@ def render_thumbnail(args): from ...utils.compatibility import run (context, assetfile, asset_type) = args - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences @@ -131,7 +131,7 @@ def execute(self, context): ui_props = scene.luxcoreOL.ui upload_props = scene.luxcoreOL.upload - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences if len(context.selected_objects) == 0: @@ -243,7 +243,7 @@ def execute(self, context): ui_props = scene.luxcoreOL.ui upload_props = scene.luxcoreOL.upload - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences assetpath = join(user_preferences.global_dir, ui_props.asset_type.lower(), 'local') diff --git a/operators/lol/update_ToC.py b/operators/lol/update_ToC.py index c97cef10..e1a23fb0 100644 --- a/operators/lol/update_ToC.py +++ b/operators/lol/update_ToC.py @@ -45,7 +45,7 @@ def execute(self, context): scene = context.scene ui_props = scene.luxcoreOL.ui - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences filepath = join(user_preferences.global_dir, 'assets_model_blendermarket.json') diff --git a/operators/utils.py b/operators/utils.py index efada6c5..e6d9b965 100644 --- a/operators/utils.py +++ b/operators/utils.py @@ -136,7 +136,7 @@ class LUXCORE_MT_node_tree: bl_idname = "LUXCORE_MT_node_tree" bl_label = "Select Node Tree" bl_description = "Select a node tree" - bl_options = {"UNDO"} + # bl_options = {"UNDO"} def draw(self, context): # Has to be present for class registration diff --git a/scripts/LOL/render_thumbnail.py b/scripts/LOL/render_thumbnail.py index 70cbc9ac..dc79ce8a 100644 --- a/scripts/LOL/render_thumbnail.py +++ b/scripts/LOL/render_thumbnail.py @@ -40,7 +40,7 @@ def calc_bbox(context, objects): def render_material_thumbnail(assetname, blendfile, thumbnail, samples): context = bpy.context scene = context.scene - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences with bpy.data.libraries.load(blendfile, link=True) as (mat_from, mat_to): @@ -69,7 +69,7 @@ def render_material_thumbnail(assetname, blendfile, thumbnail, samples): def render_model_thumbnail(assetname, blendfile, thumbnail, samples): context = bpy.context scene = context.scene - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences with bpy.data.libraries.load(blendfile, link=True) as (data_from, data_to): diff --git a/utils/lol/utils.py b/utils/lol/utils.py index 1f62e9f9..d718e8b4 100644 --- a/utils/lol/utils.py +++ b/utils/lol/utils.py @@ -42,7 +42,9 @@ from ...utils import get_addon_preferences, compatibility -LOL_HOST_URL = "https://luxcorerender.org/lol" +#LOL_HOST_URL = "https://luxcorerender.org/lol" +# Temporary alternative host +LOL_HOST_URL = "https://www.sciencehooligans.de/lol" LOL_VERSION = "v2.5" download_threads = [] @@ -54,7 +56,7 @@ def load_local_TOC(context, asset_type): assets = [] - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences filepath = join(user_preferences.global_dir, 'local_assets_' + asset_type.lower() + '.json') @@ -72,7 +74,7 @@ def load_local_TOC(context, asset_type): def load_patreon_assets(context): - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences #check if local file is available @@ -120,7 +122,7 @@ def download_table_of_contents(context): global bg_threads scene = context.scene ui_props = context.scene.luxcoreOL.ui - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences try: @@ -605,10 +607,14 @@ def clean_previmg(fullsize=False): def load_previews(context, asset_type): + bg_load_previews(context, asset_type) + # The following code did not seem to be thread safe regarding deleting and accessing preview images. Leaving in here for future reference regarding performance optimization + """ global bg_threads bg_task = Thread(target=bg_load_previews, args=(context, asset_type)) bg_threads.append(["bg_load_previews", bg_task]) bg_task.start() + """ def bg_load_previews(context, asset_type): @@ -689,7 +695,7 @@ def bg_download_thumbnails(context, download_queue): from requests import Session session = Session() - name = basename(dirname(dirname(dirname(__file__)))) + name = 'bl_ext.user_default.' + basename(dirname(dirname(dirname(__file__)))) user_preferences = context.preferences.addons[name].preferences while not download_queue.empty():