From f14d0915c88cd27ea150352bfea4101f8d49cd5a Mon Sep 17 00:00:00 2001 From: Swann Date: Fri, 28 Aug 2020 14:10:09 +0200 Subject: [PATCH] feat: same collection management for Scene Master collection --- multi_user/bl_types/bl_collection.py | 83 ++++++++++++++++------------ multi_user/bl_types/bl_scene.py | 35 +++++------- 2 files changed, 63 insertions(+), 55 deletions(-) diff --git a/multi_user/bl_types/bl_collection.py b/multi_user/bl_types/bl_collection.py index 7893167..84beaa8 100644 --- a/multi_user/bl_types/bl_collection.py +++ b/multi_user/bl_types/bl_collection.py @@ -23,6 +23,50 @@ from .. import utils from .bl_datablock import BlDatablock from .dump_anything import Loader, Dumper +def dump_collection_children(collection): + collection_children = [] + for child in collection.children: + if child not in collection_children: + collection_children.append(child.uuid) + return collection_children + +def dump_collection_objects(collection): + collection_objects = [] + for object in collection.objects: + if object not in collection_objects: + collection_objects.append(object.uuid) + + return collection_objects + +def load_collection_objects(dumped_objects, collection): + for object in dumped_objects: + object_ref = utils.find_from_attr('uuid', object, bpy.data.objects) + + if object_ref is None: + continue + elif object_ref.name not in collection.objects.keys(): + collection.objects.link(object_ref) + + for object in collection.objects: + if object.uuid not in dumped_objects: + collection.objects.unlink(object) + +def load_collection_childrens(dumped_childrens, collection): + for child_collection in dumped_childrens: + collection_ref = utils.find_from_attr( + 'uuid', + child_collection, + bpy.data.collections) + + if collection_ref is None: + continue + if collection_ref.name not in collection.children.keys(): + collection.children.link(collection_ref) + + for child_collection in collection.children: + if child_collection.uuid not in dumped_childrens: + collection.children.unlink(child_collection) + class BlCollection(BlDatablock): bl_id = "collections" bl_icon = 'FILE_FOLDER' @@ -31,6 +75,7 @@ class BlCollection(BlDatablock): bl_delay_apply = 1 bl_automatic_push = True + def _construct(self, data): if self.is_library: with bpy.data.libraries.load(filepath=bpy.data.libraries[self.data['library']].filepath, link=True) as (sourceData, targetData): @@ -49,30 +94,10 @@ class BlCollection(BlDatablock): loader.load(target,data) # Objects - for object in data["objects"]: - object_ref = utils.find_from_attr('uuid', object, bpy.data.object) - - if object_ref is None: - continue - elif object not in target.objects.keys(): - target.objects.link(object_ref) - - for object in target.objects: - if object.uuid not in data["objects"]: - target.objects.unlink(object) + load_collection_objects(data['objects'], target) # Link childrens - for collection in data["children"]: - collection_ref = utils.find_from_attr('uuid', collection, bpy.data.collections) - - if collection_ref is None: - continue - if collection_ref.name not in target.children.keys(): - target.children.link(collection_ref) - - for collection in target.children: - if collection.uuid not in data["children"]: - target.children.unlink(collection) + load_collection_childrens(data['children'], target) def _dump_implementation(self, data, instance=None): assert(instance) @@ -86,20 +111,10 @@ class BlCollection(BlDatablock): data = dumper.dump(instance) # dump objects - collection_objects = [] - for object in instance.objects: - if object not in collection_objects: - collection_objects.append(object.uuid) - - data['objects'] = collection_objects + data['objects'] = dump_collection_objects(self.instance) # dump children collections - collection_children = [] - for child in instance.children: - if child not in collection_children: - collection_children.append(child.uuid) - - data['children'] = collection_children + data['children'] = dump_collection_children(self.instance) return data diff --git a/multi_user/bl_types/bl_scene.py b/multi_user/bl_types/bl_scene.py index 4f88c01..fb3fc83 100644 --- a/multi_user/bl_types/bl_scene.py +++ b/multi_user/bl_types/bl_scene.py @@ -21,7 +21,7 @@ import mathutils from .dump_anything import Loader, Dumper from .bl_datablock import BlDatablock - +from .bl_collection import dump_collection_children, dump_collection_objects, load_collection_childrens, load_collection_objects from ..utils import get_preferences class BlScene(BlDatablock): @@ -42,24 +42,8 @@ class BlScene(BlDatablock): loader.load(target, data) # Load master collection - for object in data["collection"]["objects"]: - if object not in target.collection.objects.keys(): - target.collection.objects.link(bpy.data.objects[object]) - - for object in target.collection.objects.keys(): - if object not in data["collection"]["objects"]: - target.collection.objects.unlink(bpy.data.objects[object]) - - # load collections - for collection in data["collection"]["children"]: - if collection not in target.collection.children.keys(): - target.collection.children.link( - bpy.data.collections[collection]) - - for collection in target.collection.children.keys(): - if collection not in data["collection"]["children"]: - target.collection.children.unlink( - bpy.data.collections[collection]) + load_collection_objects(data['collection']['objects'], target.collection) + load_collection_childrens(data['collection']['children'], target.collection) if 'world' in data.keys(): target.world = bpy.data.worlds[data['world']] @@ -74,6 +58,9 @@ class BlScene(BlDatablock): if 'cycles' in data.keys(): loader.load(target.eevee, data['cycles']) + if 'render' in data.keys(): + loader.load(target.render, data['render']) + if 'view_settings' in data.keys(): loader.load(target.view_settings, data['view_settings']) if target.view_settings.use_curve_mapping: @@ -94,13 +81,18 @@ class BlScene(BlDatablock): 'id', 'camera', 'grease_pencil', + 'frame_start', + 'frame_end', + 'frame_step', ] data = scene_dumper.dump(instance) scene_dumper.depth = 3 scene_dumper.include_filter = ['children','objects','name'] - data['collection'] = scene_dumper.dump(instance.collection) + data['collection'] = {} + data['collection']['children'] = dump_collection_children(instance.collection) + data['collection']['objects'] = dump_collection_objects(instance.collection) scene_dumper.depth = 1 scene_dumper.include_filter = None @@ -126,7 +118,8 @@ class BlScene(BlDatablock): data['eevee'] = scene_dumper.dump(instance.eevee) data['cycles'] = scene_dumper.dump(instance.cycles) data['view_settings'] = scene_dumper.dump(instance.view_settings) - + data['render'] = scene_dumper.dump(instance.render) + if instance.view_settings.use_curve_mapping: data['view_settings']['curve_mapping'] = scene_dumper.dump(instance.view_settings.curve_mapping) scene_dumper.depth = 5