From c855fdaf53c76380487008b48130edbb749df06c Mon Sep 17 00:00:00 2001 From: Swann Martinez Date: Mon, 30 Sep 2019 13:35:50 +0200 Subject: [PATCH] refactor: code factoring --- bl_types/bl_action.py | 2 ++ bl_types/bl_armature.py | 2 +- delayable.py | 41 ++++++++++++++++++++++++++--------------- libs/replication | 2 +- operators.py | 22 +++++++--------------- presence.py | 11 +++++------ ui.py | 7 ++++--- utils.py | 24 ++++++++++++------------ 8 files changed, 58 insertions(+), 53 deletions(-) diff --git a/bl_types/bl_action.py b/bl_types/bl_action.py index 52bbf13..423b913 100644 --- a/bl_types/bl_action.py +++ b/bl_types/bl_action.py @@ -5,6 +5,8 @@ from jsondiff import diff from .. import utils from .bl_datablock import BlDatablock +# WIP + class BlAction(BlDatablock): def load(self, data, target): utils.dump_anything.load(target, data) diff --git a/bl_types/bl_armature.py b/bl_types/bl_armature.py index 7678ddd..c6621a0 100644 --- a/bl_types/bl_armature.py +++ b/bl_types/bl_armature.py @@ -7,7 +7,7 @@ from .. import utils from .. import presence from .bl_datablock import BlDatablock - +# WIP class BlArmature(BlDatablock): def construct(self, data): return bpy.data.armatures.new(data["name"]) diff --git a/delayable.py b/delayable.py index e14b5c1..53f547d 100644 --- a/delayable.py +++ b/delayable.py @@ -1,7 +1,8 @@ -import bpy import logging -from . import operators, utils, presence +import bpy + +from . import operators, presence, utils from .bl_types.bl_user import BlUser from .libs.replication.replication.constants import FETCHED, RP_COMMON @@ -43,7 +44,6 @@ class Timer(Delayable): if self._running: return self._timeout - def execute(self): """Main timer loop """ @@ -57,6 +57,7 @@ class Timer(Delayable): self._running = False + class ApplyTimer(Timer): def __init__(self, timout=1, target_type=None): self._type = target_type @@ -76,7 +77,7 @@ class ApplyTimer(Timer): class DynamicRightSelectTimer(Timer): def __init__(self, timout=1): super().__init__(timout) - self.last_selection=[] + self.last_selection = [] def execute(self): if operators.client: @@ -85,32 +86,42 @@ class DynamicRightSelectTimer(Timer): for user in users: user_ref = operators.client.get(uuid=user) settings = bpy.context.window_manager.session - + + # Other user if user_ref.buffer['name'] != settings.username: + user_selection = user_ref.buffer['selected_objects'] for obj in bpy.data.objects: - obj.hide_select = obj.name in user_ref.buffer['selected_objects'] + obj.hide_select = obj.name in user_selection + # Local user elif user_ref.pointer: - current_selection = utils.get_selected_objects(bpy.context.scene) + current_selection = utils.get_selected_objects( + bpy.context.scene) if current_selection != self.last_selection: user_ref.pointer.update_selected_objects(bpy.context) - - if operators.client.get_config()['right_strategy'] == RP_COMMON: - obj_common = [o for o in self.last_selection if o not in current_selection] - obj_ours = [o for o in current_selection if o not in self.last_selection] + right_strategy = operators.client.get_config()[ + 'right_strategy'] + if right_strategy == RP_COMMON: + obj_common = [ + o for o in self.last_selection if o not in current_selection] + obj_ours = [ + o for o in current_selection if o not in self.last_selection] # change old selection right to common for obj in obj_common: _object = bpy.data.objects.get(obj) - + node = operators.client.get(reference=_object) if node and node.owner == settings.username: - operators.client.change_owner(node.uuid, RP_COMMON) + operators.client.change_owner( + node.uuid, RP_COMMON) # change new selection to our for obj in obj_ours: - node = operators.client.get(reference=bpy.data.objects[obj]) + node = operators.client.get( + reference=bpy.data.objects[obj]) if node and node.owner == RP_COMMON: - operators.client.change_owner(node.uuid, settings.username) + operators.client.change_owner( + node.uuid, settings.username) self.last_selection = current_selection diff --git a/libs/replication b/libs/replication index 15ec1f7..7eecb7f 160000 --- a/libs/replication +++ b/libs/replication @@ -1 +1 @@ -Subproject commit 15ec1f7f5b111b696d6185f200220e708746e797 +Subproject commit 7eecb7fe4689649741aa69fff7ef6ec7c0a6ec52 diff --git a/operators.py b/operators.py index a0847ca..8e29b22 100644 --- a/operators.py +++ b/operators.py @@ -9,34 +9,26 @@ import time from operator import itemgetter from pathlib import Path +import msgpack + import bpy import mathutils -from bpy_extras.io_utils import ExportHelper from bpy.app.handlers import persistent +from bpy_extras.io_utils import ExportHelper -from . import environment, presence, ui, utils, delayable -import msgpack +from . import bl_types, delayable, environment, presence, ui, utils from .libs.replication.replication.data import ReplicatedDataFactory -from .libs.replication.replication.interface import Session from .libs.replication.replication.exception import NonAuthorizedOperationError -from . import bl_types +from .libs.replication.replication.interface import Session logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.ERROR) client = None delayables = [] ui_context = None -def add_datablock(datablock): - global client - - new_uuid = client.add(datablock) - return new_uuid - - -# TODO: cleanup def init_supported_datablocks(supported_types_id): global client @@ -177,7 +169,7 @@ class SessionStopOperator(bpy.types.Operator): client.remove(settings.user_uuid) client.disconnect() - + for d in delayables: try: d.unregister() diff --git a/presence.py b/presence.py index b2592a1..848c889 100644 --- a/presence.py +++ b/presence.py @@ -1,13 +1,12 @@ -import bpy -import bgl -import blf -import gpu -import mathutils import copy import logging import math - +import bgl +import blf +import bpy +import gpu +import mathutils from bpy_extras import view3d_utils from gpu_extras.batch import batch_for_shader diff --git a/ui.py b/ui.py index 65568f6..63a8d61 100644 --- a/ui.py +++ b/ui.py @@ -1,8 +1,9 @@ import bpy -from . import operators -from .libs.replication.replication.constants import FETCHED, ERROR, MODIFIED, UP, ADDED, RP_COMMON -from .bl_types.bl_user import BlUser +from . import operators +from .bl_types.bl_user import BlUser +from .libs.replication.replication.constants import (ADDED, ERROR, FETCHED, + MODIFIED, RP_COMMON, UP) ICONS_PROP_STATES = ['TRIA_DOWN', # ADDED 'TRIA_UP', # COMMITED diff --git a/utils.py b/utils.py index 0ac56fa..6ce2e14 100644 --- a/utils.py +++ b/utils.py @@ -1,36 +1,36 @@ +import json import logging +import os +import random +import string import sys from uuid import uuid4 -import json -import os -import string -import random import bpy import mathutils -from . import presence, environment +from . import environment, presence from .libs import dump_anything logger = logging.getLogger(__name__) -logger.setLevel(logging.DEBUG) +logger.setLevel(logging.ERROR) def get_datablock_users(datablock): users = [] supported_types = bpy.context.window_manager.session.supported_datablock - if hasattr(datablock, 'users_collection') and datablock.users_collection: + if hasattr(datablock, 'users_collection') and datablock.users_collection: users.extend(list(datablock.users_collection)) - if hasattr(datablock, 'users_scene') and datablock.users_scene: + if hasattr(datablock, 'users_scene') and datablock.users_scene: users.extend(list(datablock.users_scene)) - if hasattr(datablock, 'users_group') and datablock.users_scene: + if hasattr(datablock, 'users_group') and datablock.users_scene: users.extend(list(datablock.users_scene)) for datatype in supported_types: if datatype.bl_name != 'users': - root = getattr(bpy.data,datatype.bl_name) + root = getattr(bpy.data, datatype.bl_name) for item in root: if hasattr(item, 'data') and datablock == item.data or \ - datatype.bl_name != 'collections' and hasattr(item, 'children') and datablock in item.children: + datatype.bl_name != 'collections' and hasattr(item, 'children') and datablock in item.children: users.append(item) return users @@ -131,4 +131,4 @@ def dump_datablock_attibutes(datablock=None, attributes=[], depth=1, dickt=None) except: pass - return data \ No newline at end of file + return data