From d70bc810f2b117bcd1aaedc6387fcb6a78ff5392 Mon Sep 17 00:00:00 2001 From: Owen Thomson Date: Thu, 27 Jul 2023 16:32:38 -0700 Subject: [PATCH] Allow the event loop to be restarted after a new file is opened in Blender --- __init__.py | 11 +++++++++++ lib/event_loop.py | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/__init__.py b/__init__.py index b182fac..2f3c3eb 100644 --- a/__init__.py +++ b/__init__.py @@ -55,6 +55,7 @@ importlib.reload(RBX_OT_install_dependencies) import bpy +from bpy.app.handlers import persistent from bpy.types import Panel, AddonPreferences from bpy.props import ( StringProperty, @@ -226,6 +227,13 @@ def poll(cls, context): return rbx.is_logged_in and not rbx.is_processing_login_or_logout +@persistent +def load_post(dummy): + from .lib import event_loop + + event_loop.reset_timer_running() + + def get_classes(): from .lib import ( event_loop, @@ -260,6 +268,7 @@ def register(): from .lib import roblox_properties bpy.types.WindowManager.rbx = PointerProperty(type=roblox_properties.RbxProperties) + bpy.app.handlers.load_post.append(load_post) def unregister(): @@ -268,3 +277,5 @@ def unregister(): for cls in reversed(get_classes()): bpy.utils.unregister_class(cls) del bpy.types.WindowManager.rbx + + bpy.app.handlers.load_post.remove(load_post) diff --git a/lib/event_loop.py b/lib/event_loop.py index 6c6db9e..4b81f5d 100644 --- a/lib/event_loop.py +++ b/lib/event_loop.py @@ -72,6 +72,14 @@ def get_loop(): return loop +# If a new file is opened during the session the modal timer will stop +# running. This function is called from a post load handler in this event +# to reset the timer_running flag so the event loop can be restarted. +def reset_timer_running(): + global timer_running + timer_running = False + + def __ensure_started(): global timer_running