From d7a73116fd9218fd9e88216518799f8e2bc686b7 Mon Sep 17 00:00:00 2001 From: themancalledjakob Date: Wed, 10 Jul 2024 17:14:02 +0200 Subject: [PATCH] move execution_queue in butils, cleanup --- __init__.py | 60 ++++++++++++++++++----------------------------------- butils.py | 14 +++++++++++++ 2 files changed, 34 insertions(+), 40 deletions(-) diff --git a/__init__.py b/__init__.py index f0b33d2..9b5d6b7 100644 --- a/__init__.py +++ b/__init__.py @@ -31,7 +31,6 @@ else: from . import butils import bpy -import queue import math import mathutils import io @@ -46,19 +45,10 @@ import datetime import os import re - -class SharedVariables(): - fonts = Font.fonts - - def __init__(self, **kv): - self.__dict__.update(kv) - def getPreferences(context): preferences = context.preferences return preferences.addons[__name__].preferences -shared = SharedVariables() - class FONT3D_addonPreferences(bpy.types.AddonPreferences): """Font3D Addon Preferences @@ -85,8 +75,6 @@ class FONT3D_addonPreferences(bpy.types.AddonPreferences): icon="ERROR", message=("Chosen directory is not writable.", "Please reset to default or chose another one.")) - else: - shared.paths["assets"] = self.assets_dir print(f"{__name__}: change assets_dir to {self.assets_dir}") @@ -110,7 +98,6 @@ class FONT3D_OT_Font3D(bpy.types.Operator): bl_options = {'REGISTER', 'UNDO'} def execute(self, context): - global shared print("Font3d execute()") @@ -180,7 +167,7 @@ class FONT3D_text_properties(bpy.types.PropertyGroup): # # print(text_properties.letter_spacing) # print(f"is running ---------------------------------->>>>>>> {text_properties.letter_spacing} and {text_properties.get('letter_spacingor')}") # # butils.set_text_on_curve(text_properties) - # run_in_main_thread(lambda: fun(self)) + # butils.run_in_main_thread(lambda: fun(self)) text_id: bpy.props.IntProperty() font_name: bpy.props.StringProperty() font_face: bpy.props.StringProperty() @@ -228,23 +215,6 @@ class FONT3D_UL_texts(bpy.types.UIList): def invoke(self, context, event): pass -# TODO: TODO: TODO: TODO: TODO # >>>>>>>>>>>>>>>> -execution_queue = queue.Queue() - - -# This function can safely be called in another thread. -# The function will be executed when the timer runs the next time. -def run_in_main_thread(function): - execution_queue.put(function) - - -def execute_queued_functions(): - while not execution_queue.empty(): - function = execution_queue.get() - function() - return 1.0 - -# <<<<<<<<<<<<<<<<< TODO: TODO: TODO: TODO: TODO # class FONT3D_PT_panel(bpy.types.Panel): bl_label = "Panel for Font3D" @@ -298,12 +268,11 @@ class FONT3D_PT_panel(bpy.types.Panel): context.active_object.parent == t.text_object): font3d_data.active_text_index = i - run_in_main_thread(update) + butils.run_in_main_thread(update) return True def draw(self, context): - global shared layout = self.layout wm = context.window_manager scene = context.scene @@ -354,7 +323,6 @@ class FONT3D_PT_TextPropertiesPanel(bpy.types.Panel): return type(self.get_active_text_properties(self)) != type(None) def draw(self, context): - global shared layout = self.layout wm = context.window_manager scene = context.scene @@ -379,9 +347,15 @@ class FONT3D_OT_LoadFont(bpy.types.Operator): bl_options = {'REGISTER', 'UNDO'} def execute(self, context): - global shared scene = bpy.context.scene + butils.ShowMessageBox( + title=f"{__name__} Warning", + icon="ERROR", + message=f"We believe this functionality is currently not available.", + ) + return {'CANCELLED'} + # print(f"loading da font at path {scene.font3d.font_path}") if not os.path.exists(scene.font3d.font_path): butils.ShowMessageBox( @@ -740,10 +714,12 @@ classes = ( @persistent def load_handler(self, dummy): - bpy.app.timers.register(execute_queued_functions) + if not bpy.app.timers.is_registered(butils.execute_queued_functions): + bpy.app.timers.register(butils.execute_queued_functions) def load_handler_unload(): - bpy.app.timers.unregister(execute_queued_functions) + if bpy.app.timers.is_registered(butils.execute_queued_functions): + bpy.app.timers.unregister(butils.execute_queued_functions) def register(): for cls in classes: @@ -753,9 +729,11 @@ def register(): # bpy.types.Object.__del__ = lambda self: print(f"Bye {self.name}") print(f"REGISTER {bl_info['name']}") - # auto start + # auto start if we load a blend file if load_handler not in bpy.app.handlers.load_post: bpy.app.handlers.load_post.append(load_handler) + # and autostart if we reload script + load_handler(None, None) # clear available fonts def clear_available_fonts(): @@ -817,15 +795,17 @@ def register(): for o in remove_list: bpy.data.objects.remove(o, do_unlink=True) - run_in_main_thread(clear_available_fonts) - run_in_main_thread(load_available_fonts) + butils.run_in_main_thread(clear_available_fonts) + butils.run_in_main_thread(load_available_fonts) def unregister(): for cls in classes: bpy.utils.unregister_class(cls) + # remove autostart when loading blend file if load_handler in bpy.app.handlers.load_post: bpy.app.handlers.load_post.remove(load_handler) + # and when reload script load_handler_unload() del bpy.types.Scene.font3d diff --git a/butils.py b/butils.py index 1b25829..598db01 100644 --- a/butils.py +++ b/butils.py @@ -1,5 +1,6 @@ import bpy import mathutils +import queue import importlib # then import dependencies for our addon @@ -13,6 +14,19 @@ if "utils" in locals(): else: from .common import utils +execution_queue = queue.Queue() + +# This function can safely be called in another thread. +# The function will be executed when the timer runs the next time. +def run_in_main_thread(function): + execution_queue.put(function) + +def execute_queued_functions(): + while not execution_queue.empty(): + function = execution_queue.get() + function() + return 1.0 + def apply_all_transforms(obj): mb = obj.matrix_basis if hasattr(obj.data, "transform"):