From 5817c9110b2874439e91192f16ff987a998abc13 Mon Sep 17 00:00:00 2001 From: Swann Date: Wed, 7 Apr 2021 10:06:38 +0200 Subject: [PATCH] feat: basic collection loading --- multi_user/operators.py | 154 ++++++++++++++++++++++++++-------------- 1 file changed, 102 insertions(+), 52 deletions(-) diff --git a/multi_user/operators.py b/multi_user/operators.py index 981c7df..902de49 100644 --- a/multi_user/operators.py +++ b/multi_user/operators.py @@ -827,66 +827,116 @@ class SessionLoadSaveOperator(bpy.types.Operator, ImportHelper): maxlen=255, # Max internal buffer length, longer would be clamped. ) + load_to_collection: bpy.props.BoolProperty( + name="Load to collection", + description="Load the snapshot into a collection", + default=False, + ) + + draw_users: bpy.props.BoolProperty( + name="Draw users", + description="Draw a mesh representing each user position and selected object", + default=False, + ) + + clear_datablocks: bpy.props.BoolProperty( + name="Removes existing data", + description="Remove all exisitng datablocks", + default=True, + ) + + files: bpy.props.CollectionProperty( + type=bpy.types.OperatorFileListElement, + options={'HIDDEN', 'SKIP_SAVE'}, + ) + def execute(self, context): from replication.graph import ReplicationGraph - # TODO: add filechecks - - try: - f = gzip.open(self.filepath, "rb") - db = pickle.load(f) - except OSError as e: - f = open(self.filepath, "rb") - db = pickle.load(f) - - if db: - logging.info(f"Reading {self.filepath}") - nodes = db.get("nodes") - - logging.info(f"{len(nodes)} Nodes to load") - - - - # init the factory with supported types - bpy_factory = ReplicatedDataFactory() - for type in bl_types.types_to_register(): - type_module = getattr(bl_types, type) - name = [e.capitalize() for e in type.split('_')[1:]] - type_impl_name = 'Bl'+''.join(name) - type_module_class = getattr(type_module, type_impl_name) + # Initialisation + # init the factory with supported types + bpy_factory = ReplicatedDataFactory() + for type in bl_types.types_to_register(): + type_module = getattr(bl_types, type) + name = [e.capitalize() for e in type.split('_')[1:]] + type_impl_name = 'Bl'+''.join(name) + type_module_class = getattr(type_module, type_impl_name) - bpy_factory.register_type( - type_module_class.bl_class, - type_module_class) - - graph = ReplicationGraph() - - for node, node_data in nodes: - node_type = node_data.get('str_type') - - impl = bpy_factory.get_implementation_from_net(node_type) - - if impl: - logging.info(f"Loading {node}") - instance = impl(owner=node_data['owner'], - uuid=node, - dependencies=node_data['dependencies'], - data=node_data['data']) - instance.store(graph) - instance.state = FETCHED - - logging.info("Graph succefully loaded") - + bpy_factory.register_type( + type_module_class.bl_class, + type_module_class) + # Optionnaly clear the scene + if self.clear_datablocks: utils.clean_scene() - # Step 1: Construct nodes - for node in graph.list_ordered(): - graph[node].resolve() + dir_path = Path(self.filepath).parent - # Step 2: Load nodes - for node in graph.list_ordered(): - graph[node].apply() + for db in self.files: + filepath = os.path.join(dir_path,db.name) + + try: + f = gzip.open(filepath, "rb") + db = pickle.load(f) + except OSError as e: + f = open(filepath, "rb") + db = pickle.load(f) + + if db: + + created = os.path.getctime(filepath) + logging.info(f"Reading {filepath}") + nodes = db.get("nodes") + + logging.info(f"{len(nodes)} Nodes to load") + + graph = ReplicationGraph() + + for node, node_data in nodes: + node_type = node_data.get('str_type') + + impl = bpy_factory.get_implementation_from_net(node_type) + + if impl: + logging.info(f"Loading {node}") + instance = impl(owner=node_data['owner'], + uuid=node, + dependencies=node_data['dependencies'], + data=node_data['data']) + instance.store(graph) + instance.state = FETCHED + + logging.info("Graph succefully loaded") + + # Find scene + scenes = [n for n in graph.values() if isinstance(n, bl_types.bl_scene.BlScene)] + scene = scenes[0] + + collection_data = { + 'instance_offset': [0.0, 0.0, 0.0], + 'name': str(created), + 'objects': scene.data['collection']['objects'], + 'children': scene.data['collection']['children']} + collection_node = bl_types.bl_collection.BlCollection() + collection_node.dependencies = scene.dependencies + collection_node.data = collection_data + graph[collection_node.uuid] = collection_node + del graph[scene.uuid] + + # Step 1: Construct nodes + for node in graph.list_ordered(): + node_inst = graph[node] + try: + node_inst.instance = node_inst._construct(node_inst.data) + node_inst.instance.uuid = node_inst.uuid + except Exception as e: + continue + # Step 2: Load nodes + for node in graph.list_ordered(): + graph[node].state = FETCHED + graph[node].apply() + + bpy.context.scene.collection.children.link(collection_node.instance) return {'FINISHED'}