From e4e82ff8811ab29f948d7757c1af42b3690ba991 Mon Sep 17 00:00:00 2001 From: Swann Martinez Date: Fri, 27 Sep 2019 14:50:00 +0200 Subject: [PATCH] fix: timer not properly exit refactor: cleanup presence --- __init__.py | 7 +++++-- delayable.py | 28 +++++++++++++++++----------- libs/replication | 2 +- operators.py | 13 +++++-------- presence.py | 9 +-------- 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/__init__.py b/__init__.py index 72ee322..88e42aa 100644 --- a/__init__.py +++ b/__init__.py @@ -31,7 +31,7 @@ DEPENDENCIES = { logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.ERROR) #TODO: refactor config # UTILITY FUNCTIONS @@ -187,7 +187,6 @@ class SessionProps(bpy.types.PropertyGroup): def load(self): config = environment.load_config() - logger.info(config) if "username" in config.keys(): self.username = config["username"] self.ip = config["ip"] @@ -255,6 +254,7 @@ def register(): environment.setup(DEPENDENCIES,bpy.app.binary_path_python) + from . import presence from . import operators from . import ui @@ -267,14 +267,17 @@ def register(): bpy.context.window_manager.session.load() + presence.register() operators.register() ui.register() bpy.app.handlers.load_post.append(load_handler) def unregister(): + from . import presence from . import operators from . import ui + presence.unregister() ui.unregister() operators.unregister() diff --git a/delayable.py b/delayable.py index 92eac94..f5c72b1 100644 --- a/delayable.py +++ b/delayable.py @@ -7,6 +7,7 @@ from .libs.replication.replication.constants import FETCHED, RP_COMMON logger = logging.getLogger(__name__) + class Delayable(): """Delayable task interface """ @@ -29,25 +30,32 @@ class Timer(Delayable): def __init__(self, duration=1): self._timeout = duration + self._running = True def register(self): """Register the timer into the blender timer system """ - bpy.app.timers.register(self.execute) + bpy.app.timers.register(self.main) + def main(self): + self.execute() + + if self._running: + return self._timeout + + def execute(self): """Main timer loop """ - return self._timeout + raise NotImplementedError def unregister(self): """Unnegister the timer of the blender timer system """ - try: - bpy.app.timers.unregister(self.execute) - except: - logger.error("timer already unregistered") + if bpy.app.timers.is_registered(self.main): + bpy.app.timers.unregister(self.main) + self._running = False class ApplyTimer(Timer): def __init__(self, timout=1, target_type=None): @@ -64,7 +72,6 @@ class ApplyTimer(Timer): if node_ref.state == FETCHED: operators.client.apply(uuid=node) - return self._timeout class DynamicRightSelectTimer(Timer): def __init__(self, timout=1): @@ -92,7 +99,9 @@ class DynamicRightSelectTimer(Timer): obj_ours = [o for o in current_selection if o not in self.last_selection] for obj in obj_common: - node = operators.client.get(reference=bpy.data.objects[obj]) + _object = bpy.data.objects.get(obj) + + node = operators.client.get(reference=_object) if node: node.owner = settings.username operators.client.change_owner(node.uuid, RP_COMMON) @@ -104,9 +113,7 @@ class DynamicRightSelectTimer(Timer): node.owner = settings.username operators.client.change_owner(node.uuid, settings.username) self.last_selection = current_selection - return self._timeout -# class CheckNewTimer(Timer): class RedrawTimer(Timer): def __init__(self, timout=1, target_type=None): @@ -117,7 +124,6 @@ class RedrawTimer(Timer): if presence.renderer: presence.refresh_3d_view() - return self._timeout class Draw(Delayable): def __init__(self): diff --git a/libs/replication b/libs/replication index 6b50d66..13dce5d 160000 --- a/libs/replication +++ b/libs/replication @@ -1 +1 @@ -Subproject commit 6b50d668faf372314b3ae2caa3a9b12f4deb461f +Subproject commit 13dce5d548d48c8b9d476511ef1b27fe7fe93a1d diff --git a/operators.py b/operators.py index 75c2a99..a0847ca 100644 --- a/operators.py +++ b/operators.py @@ -113,8 +113,6 @@ class SessionStartOperator(bpy.types.Operator): port=settings.port ) - time.sleep(0.5) - if client.state == 0: settings.is_admin = False self.report( @@ -179,10 +177,12 @@ class SessionStopOperator(bpy.types.Operator): client.remove(settings.user_uuid) client.disconnect() - + for d in delayables: - d.unregister() - + try: + d.unregister() + except: + continue presence.renderer.stop() return {"FINISHED"} @@ -341,15 +341,12 @@ def register(): for cls in classes: register_class(cls) - presence.register() bpy.app.handlers.depsgraph_update_post.append(redresh_handler) def unregister(): global client - presence.unregister() - if client and client.state == 2: client.disconnect() client = None diff --git a/presence.py b/presence.py index b4c00ae..b2592a1 100644 --- a/presence.py +++ b/presence.py @@ -115,7 +115,7 @@ class User(): def update_presence(self, context): global renderer - + if renderer and self.enable_presence: renderer.run() else: @@ -191,13 +191,6 @@ class DrawFactory(object): local_username = bpy.context.window_manager.session.username self.flush_selection() - # key_to_remove = [] - # for k in self.d3d_items.keys(): - # if "{}_select".format(client_uuid) in k: - # key_to_remove.append(k) - - # for k in key_to_remove: - # del self.d3d_items[k] if client_selection: