move execution_queue in butils, cleanup

This commit is contained in:
themancalledjakob 2024-07-10 17:14:02 +02:00
parent 07c38fcdaf
commit d7a73116fd
2 changed files with 34 additions and 40 deletions

View file

@ -31,7 +31,6 @@ else:
from . import butils from . import butils
import bpy import bpy
import queue
import math import math
import mathutils import mathutils
import io import io
@ -46,19 +45,10 @@ import datetime
import os import os
import re import re
class SharedVariables():
fonts = Font.fonts
def __init__(self, **kv):
self.__dict__.update(kv)
def getPreferences(context): def getPreferences(context):
preferences = context.preferences preferences = context.preferences
return preferences.addons[__name__].preferences return preferences.addons[__name__].preferences
shared = SharedVariables()
class FONT3D_addonPreferences(bpy.types.AddonPreferences): class FONT3D_addonPreferences(bpy.types.AddonPreferences):
"""Font3D Addon Preferences """Font3D Addon Preferences
@ -85,8 +75,6 @@ class FONT3D_addonPreferences(bpy.types.AddonPreferences):
icon="ERROR", icon="ERROR",
message=("Chosen directory is not writable.", message=("Chosen directory is not writable.",
"Please reset to default or chose another one.")) "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}") 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'} bl_options = {'REGISTER', 'UNDO'}
def execute(self, context): def execute(self, context):
global shared
print("Font3d execute()") print("Font3d execute()")
@ -180,7 +167,7 @@ class FONT3D_text_properties(bpy.types.PropertyGroup):
# # print(text_properties.letter_spacing) # # print(text_properties.letter_spacing)
# print(f"is running ---------------------------------->>>>>>> {text_properties.letter_spacing} and {text_properties.get('letter_spacingor')}") # print(f"is running ---------------------------------->>>>>>> {text_properties.letter_spacing} and {text_properties.get('letter_spacingor')}")
# # butils.set_text_on_curve(text_properties) # # 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() text_id: bpy.props.IntProperty()
font_name: bpy.props.StringProperty() font_name: bpy.props.StringProperty()
font_face: bpy.props.StringProperty() font_face: bpy.props.StringProperty()
@ -228,23 +215,6 @@ class FONT3D_UL_texts(bpy.types.UIList):
def invoke(self, context, event): def invoke(self, context, event):
pass 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): class FONT3D_PT_panel(bpy.types.Panel):
bl_label = "Panel for Font3D" bl_label = "Panel for Font3D"
@ -298,12 +268,11 @@ class FONT3D_PT_panel(bpy.types.Panel):
context.active_object.parent == t.text_object): context.active_object.parent == t.text_object):
font3d_data.active_text_index = i font3d_data.active_text_index = i
run_in_main_thread(update) butils.run_in_main_thread(update)
return True return True
def draw(self, context): def draw(self, context):
global shared
layout = self.layout layout = self.layout
wm = context.window_manager wm = context.window_manager
scene = context.scene scene = context.scene
@ -354,7 +323,6 @@ class FONT3D_PT_TextPropertiesPanel(bpy.types.Panel):
return type(self.get_active_text_properties(self)) != type(None) return type(self.get_active_text_properties(self)) != type(None)
def draw(self, context): def draw(self, context):
global shared
layout = self.layout layout = self.layout
wm = context.window_manager wm = context.window_manager
scene = context.scene scene = context.scene
@ -379,9 +347,15 @@ class FONT3D_OT_LoadFont(bpy.types.Operator):
bl_options = {'REGISTER', 'UNDO'} bl_options = {'REGISTER', 'UNDO'}
def execute(self, context): def execute(self, context):
global shared
scene = bpy.context.scene 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}") # print(f"loading da font at path {scene.font3d.font_path}")
if not os.path.exists(scene.font3d.font_path): if not os.path.exists(scene.font3d.font_path):
butils.ShowMessageBox( butils.ShowMessageBox(
@ -740,10 +714,12 @@ classes = (
@persistent @persistent
def load_handler(self, dummy): 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(): 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(): def register():
for cls in classes: for cls in classes:
@ -753,9 +729,11 @@ def register():
# bpy.types.Object.__del__ = lambda self: print(f"Bye {self.name}") # bpy.types.Object.__del__ = lambda self: print(f"Bye {self.name}")
print(f"REGISTER {bl_info['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: if load_handler not in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.append(load_handler) bpy.app.handlers.load_post.append(load_handler)
# and autostart if we reload script
load_handler(None, None)
# clear available fonts # clear available fonts
def clear_available_fonts(): def clear_available_fonts():
@ -817,15 +795,17 @@ def register():
for o in remove_list: for o in remove_list:
bpy.data.objects.remove(o, do_unlink=True) bpy.data.objects.remove(o, do_unlink=True)
run_in_main_thread(clear_available_fonts) butils.run_in_main_thread(clear_available_fonts)
run_in_main_thread(load_available_fonts) butils.run_in_main_thread(load_available_fonts)
def unregister(): def unregister():
for cls in classes: for cls in classes:
bpy.utils.unregister_class(cls) bpy.utils.unregister_class(cls)
# remove autostart when loading blend file
if load_handler in bpy.app.handlers.load_post: if load_handler in bpy.app.handlers.load_post:
bpy.app.handlers.load_post.remove(load_handler) bpy.app.handlers.load_post.remove(load_handler)
# and when reload script
load_handler_unload() load_handler_unload()
del bpy.types.Scene.font3d del bpy.types.Scene.font3d

View file

@ -1,5 +1,6 @@
import bpy import bpy
import mathutils import mathutils
import queue
import importlib import importlib
# then import dependencies for our addon # then import dependencies for our addon
@ -13,6 +14,19 @@ if "utils" in locals():
else: else:
from .common import utils 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): def apply_all_transforms(obj):
mb = obj.matrix_basis mb = obj.matrix_basis
if hasattr(obj.data, "transform"): if hasattr(obj.data, "transform"):