2020-03-20 14:56:50 +01:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
|
|
|
|
2019-08-08 23:42:02 +02:00
|
|
|
import bpy
|
|
|
|
import mathutils
|
|
|
|
|
|
|
|
from .. import utils
|
2020-08-24 17:49:17 +02:00
|
|
|
from .dump_anything import Loader, Dumper
|
2021-03-26 12:30:15 +01:00
|
|
|
from replication.protocol import ReplicatedDatablock
|
|
|
|
from replication.objects import Node
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-08-28 14:10:09 +02:00
|
|
|
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
|
|
|
|
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-08-28 14:10:09 +02:00
|
|
|
def dump_collection_objects(collection):
|
|
|
|
collection_objects = []
|
|
|
|
for object in collection.objects:
|
|
|
|
if object not in collection_objects:
|
|
|
|
collection_objects.append(object.uuid)
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-08-28 14:10:09 +02:00
|
|
|
return collection_objects
|
|
|
|
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-08-28 14:10:09 +02:00
|
|
|
def load_collection_objects(dumped_objects, collection):
|
|
|
|
for object in dumped_objects:
|
2020-09-02 16:44:11 +02:00
|
|
|
object_ref = utils.find_from_attr('uuid', object, bpy.data.objects)
|
2020-08-28 14:10:09 +02:00
|
|
|
|
2020-09-02 16:44:11 +02:00
|
|
|
if object_ref is None:
|
|
|
|
continue
|
|
|
|
elif object_ref.name not in collection.objects.keys():
|
|
|
|
collection.objects.link(object_ref)
|
2020-08-28 14:10:09 +02:00
|
|
|
|
|
|
|
for object in collection.objects:
|
|
|
|
if object.uuid not in dumped_objects:
|
|
|
|
collection.objects.unlink(object)
|
|
|
|
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-08-28 14:10:09 +02:00
|
|
|
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)
|
|
|
|
|
2020-10-30 16:58:18 +01:00
|
|
|
def resolve_collection_dependencies(collection):
|
|
|
|
deps = []
|
|
|
|
|
|
|
|
for child in collection.children:
|
|
|
|
deps.append(child)
|
|
|
|
for object in collection.objects:
|
|
|
|
deps.append(object)
|
|
|
|
|
|
|
|
return deps
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2021-03-26 12:30:15 +01:00
|
|
|
class BlCollection(ReplicatedDatablock):
|
2020-01-13 18:51:31 +01:00
|
|
|
bl_id = "collections"
|
|
|
|
bl_icon = 'FILE_FOLDER'
|
|
|
|
bl_class = bpy.types.Collection
|
2020-09-17 22:47:11 +02:00
|
|
|
bl_check_common = True
|
2020-12-09 14:49:26 +01:00
|
|
|
bl_reload_parent = False
|
2020-09-17 22:47:11 +02:00
|
|
|
|
2021-03-26 12:30:15 +01:00
|
|
|
@staticmethod
|
|
|
|
def construct(data: dict) -> object:
|
|
|
|
datablock = bpy.data.collections.new(node.data["name"])
|
|
|
|
return datablock
|
2019-08-08 23:42:02 +02:00
|
|
|
|
2021-03-26 12:30:15 +01:00
|
|
|
@staticmethod
|
|
|
|
def load(data: dict, datablock: object):
|
|
|
|
data = node.data
|
2020-08-24 17:49:17 +02:00
|
|
|
loader = Loader()
|
2021-03-26 12:30:15 +01:00
|
|
|
loader.load(datablock, data)
|
2020-09-02 16:44:11 +02:00
|
|
|
|
2020-04-07 14:49:43 +02:00
|
|
|
# Objects
|
2021-03-26 12:30:15 +01:00
|
|
|
load_collection_objects(data['objects'], datablock)
|
2019-08-08 23:42:02 +02:00
|
|
|
|
|
|
|
# Link childrens
|
2021-03-26 12:30:15 +01:00
|
|
|
load_collection_childrens(data['children'], datablock)
|
2019-08-08 23:42:02 +02:00
|
|
|
|
2020-12-23 18:46:29 +01:00
|
|
|
# FIXME: Find a better way after the replication big refacotoring
|
|
|
|
# Keep other user from deleting collection object by flushing their history
|
|
|
|
utils.flush_history()
|
|
|
|
|
2021-03-26 12:30:15 +01:00
|
|
|
@staticmethod
|
|
|
|
def dump(datablock: object) -> dict:
|
|
|
|
assert(datablock)
|
2020-08-24 17:49:17 +02:00
|
|
|
|
|
|
|
dumper = Dumper()
|
|
|
|
dumper.depth = 1
|
|
|
|
dumper.include_filter = [
|
|
|
|
"name",
|
|
|
|
"instance_offset"
|
|
|
|
]
|
2021-03-26 12:30:15 +01:00
|
|
|
data = dumper.dump(datablock)
|
2019-10-08 22:24:58 +02:00
|
|
|
|
|
|
|
# dump objects
|
2021-03-26 12:30:15 +01:00
|
|
|
data['objects'] = dump_collection_objects(datablock)
|
2019-10-08 22:24:58 +02:00
|
|
|
|
|
|
|
# dump children collections
|
2021-03-26 12:30:15 +01:00
|
|
|
data['children'] = dump_collection_children(datablock)
|
2019-10-07 17:32:56 +02:00
|
|
|
|
2019-10-08 22:24:58 +02:00
|
|
|
return data
|
2019-10-14 14:14:11 +02:00
|
|
|
|
2021-03-25 14:55:53 +01:00
|
|
|
@staticmethod
|
2021-03-26 12:30:15 +01:00
|
|
|
def resolve_deps(datablock: object) -> [object]:
|
2021-03-25 14:55:53 +01:00
|
|
|
return resolve_collection_dependencies(datablock)
|
2021-04-21 11:22:53 +02:00
|
|
|
|
|
|
|
_type = bpy.types.Collection
|
|
|
|
_class = BlCollection
|