From a8e489f80d3cb6914f18fe6c235e209097eb05c8 Mon Sep 17 00:00:00 2001 From: Swann Martinez Date: Thu, 11 Apr 2019 15:54:15 +0200 Subject: [PATCH] feat(rcf): Added scene init --- client.py | 9 ++-- helpers.py | 3 +- operators.py | 123 ++++++++++++++++++++++++++++++--------------------- 3 files changed, 78 insertions(+), 57 deletions(-) diff --git a/client.py b/client.py index a1b9ff7..56bcbfb 100644 --- a/client.py +++ b/client.py @@ -235,7 +235,7 @@ def rcf_client_agent(ctx, pipe): if agent.state == State.SYNCING: # Store snapshot if rcfmsg.key == "SNAPSHOT_END": - logger.info("snapshot complete") + # logger.info("snapshot complete") agent.state = State.ACTIVE else: helpers.load(rcfmsg.key,rcfmsg.body) @@ -243,10 +243,11 @@ def rcf_client_agent(ctx, pipe): elif agent.state == State.ACTIVE: if rcfmsg.id != agent.id: helpers.load(rcfmsg.key,rcfmsg.body) + # logger.info("load") rcfmsg.store(agent.property_map) - action = "update" if rcfmsg.body else "delete" - logging.info("{}: received from {}:{},{} {}".format(rcfmsg.key, - server.address, rcfmsg.id, server.port, action)) + # action = "update" if rcfmsg.body else "delete" + # logging.info("{}: received from {}:{},{} {}".format(rcfmsg.key, + # server.address, rcfmsg.id, server.port, action)) else: logger.info("{} nothing to do".format(agent.id)) diff --git a/helpers.py b/helpers.py index a9c4e6f..8e9713b 100644 --- a/helpers.py +++ b/helpers.py @@ -14,8 +14,7 @@ def refresh_window(): def load(key, value): target = resolve_bpy_path(key) target_type = key.split('/')[0] - if target: - target.is_updating = True + if target_type == 'Object': load_object(target=target, data=value, diff --git a/operators.py b/operators.py index 413e9f2..602726b 100644 --- a/operators.py +++ b/operators.py @@ -138,9 +138,9 @@ def init_datablocks(): global client_instance for datatype in SUPPORTED_TYPES: - print(datatype) for item in getattr(bpy.data,helpers.CORRESPONDANCE[datatype]): key = "{}/{}".format(datatype,item.name) + print(key) client_instance.set(key) def update_scene(msg): @@ -289,23 +289,24 @@ def default_tick(): # except Exception as e: # print("error on {} {}".format(op.name,e)) - if not push_tasks.empty(): - update = push_tasks.get() - print(update) - try: - push(update[0],update[1]) - except Exception as e: - print("push error: {}".format(e)) + # if not push_tasks.empty(): + # update = push_tasks.get() + # print(update) + # try: + # push(update[0],update[1]) + # except Exception as e: + # print("push error: {}".format(e)) - if not pull_tasks.empty(): - try: - pull(pull_tasks.get()) - except Exception as e: - print("pull error: {}".format(e)) + # if not pull_tasks.empty(): + # try: + # pull(pull_tasks.get()) + # except Exception as e: + # print("pull error: {}".format(e)) - - return 0.1 + bpy.ops.session.refresh() + + return 0.5 def mesh_tick(): @@ -347,19 +348,19 @@ def draw_tick(): def register_ticks(): # REGISTER Updaters - bpy.app.timers.register(draw_tick) - bpy.app.timers.register(mesh_tick) - bpy.app.timers.register(object_tick) + # bpy.app.timers.register(draw_tick) + # bpy.app.timers.register(mesh_tick) + # bpy.app.timers.register(object_tick) bpy.app.timers.register(default_tick) def unregister_ticks(): # REGISTER Updaters - global drawer - drawer.unregister_handlers() - bpy.app.timers.unregister(draw_tick) - bpy.app.timers.unregister(mesh_tick) - bpy.app.timers.unregister(object_tick) + # global drawer + # drawer.unregister_handlers() + # bpy.app.timers.unregister(draw_tick) + # bpy.app.timers.unregister(mesh_tick) + # bpy.app.timers.unregister(object_tick) bpy.app.timers.unregister(default_tick) # OPERATORS @@ -398,7 +399,7 @@ class session_join(bpy.types.Operator): # drawer = net_draw.HUD(client_instance_instance=client_instance) - # register_ticks() + register_ticks() return {"FINISHED"} @@ -500,7 +501,7 @@ class session_stop(bpy.types.Operator): def execute(self, context): global server - global client_instance + global client_instance, client_keys net_settings = context.scene.session_settings @@ -512,10 +513,12 @@ class session_stop(bpy.types.Operator): client_instance.exit() del client_instance client_instance = None + del client_keys + client_keys = None # bpy.ops.asyncio.stop() net_settings.is_running = False - # unregister_ticks() + unregister_ticks() else: logger.debug("No server/client_instance running.") @@ -597,39 +600,53 @@ def ordered(updates): uplist.sort(key=itemgetter(0)) return uplist +def is_dirty(updates): + global client_keys + + if client_keys: + if len(client_keys) > 0: + for u in updates: + key = "{}/{}".format(u.id.bl_rna.name,u.id.name) + + if key not in client_keys: + return True + + return False def depsgraph_update(scene): global client_instance - if client_instance and client_instance.status == net_components.RCFStatus.CONNECTED: + if client_instance and client_instance.agent.is_alive(): updates = bpy.context.depsgraph.updates - update_selected_object(bpy.context) + # update_selected_object(bpy.context) + push = False - push = True # Update selected object - - for update in updates.items(): - updated_data = update[1] + + # for update in updates.items(): + # updated_data = update[1] - if updated_data.id.is_updating: - updated_data.id.is_updating = False - push = False - break + # if updated_data.id.is_updating: + # updated_data.id.is_updating = False + # push = False + # break - if push: + # if push: # if len(updates) is 1: # updated_data = updates[0] # if scene.session_settings.active_object and updated_data.id.name == scene.session_settings.active_object.name: # if updated_data.is_updated_transform: # add_update(updated_data.id.bl_rna.name, updated_data.id.name) # else: + if is_dirty(updates) or push: for update in ordered(updates): if update[2] == "Master Collection": pass elif update[1] in SUPPORTED_TYPES: - push_tasks.put((update[1], update[2])) - + client_instance.set("{}/{}".format(update[1], update[2])) + push = False + # elif scene.session_settings.active_object and updated_data.id.name == scene.session_settings.active_object.name: # if updated_data.is_updated_transform or updated_data.is_updated_geometry: # add_update(updated_data.id.bl_rna.name, updated_data.id.name) @@ -674,36 +691,40 @@ def register(): from bpy.utils import register_class for cls in classes: register_class(cls) - bpy.types.ID.is_updating = bpy.props.BoolProperty(default=False) + bpy.types.ID.is_dirty = bpy.props.BoolProperty(default=False) bpy.types.Scene.session_settings = bpy.props.PointerProperty( type=session_settings) - # bpy.app.handlers.depsgraph_update_post.append(depsgraph_update) + bpy.app.handlers.depsgraph_update_post.append(depsgraph_update) def unregister(): global server - global client_instance + global client_instance, client_keys - # try: - # bpy.app.handlers.depsgraph_update_post.remove(depsgraph_update) - # except: - # pass + try: + bpy.app.handlers.depsgraph_update_post.remove(depsgraph_update) + except: + pass if server: - # server.stop() - del server + server.kill() server = None + del server + if client_instance: - # client_instance.stop() - del client_instance + client_instance.exit() client_instance = None + del client_instance + del client_keys + + from bpy.utils import unregister_class for cls in reversed(classes): unregister_class(cls) del bpy.types.Scene.session_settings - del bpy.types.ID.is_updating + del bpy.types.ID.is_dirty if __name__ == "__main__": register()