# coding: utf-8 # Copyright (C) 2023 Ilia Kurochkin # # 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. ''' DESCRIPTION. Various collection tools. ''' __version__ = '0.1' import bpy from collections import defaultdict def copy_objects(from_col, to_col, linked, double_lut): '''Function copying objects from collection to collection.''' for obj in from_col.objects: double = obj.copy() if not linked and obj.data: double.data = double.data.copy() to_col.objects.link(double) double_lut[obj] = double return True def copy_collections_recursive(collection, suffix='copy', linked=False): '''Function recursive copying collection.''' double_lut = defaultdict(lambda: None) parent = [p for p in (bpy.data.collections[:] + [bpy.context.scene.collection]) if collection.name in p.children.keys()][0] def _copy(parent, collection, suffix, linked=False): '''Function copying collection.''' clone_collection = bpy.data.collections.new( '_'.join((collection.name, suffix)) ) copy_objects(collection, clone_collection, linked, double_lut) for _collection in collection.children: _copy(clone_collection, _collection, suffix, linked) parent.children.link(clone_collection) _copy(parent, collection, suffix, linked) for obj, double in tuple(double_lut.items()): parent = double_lut[obj.parent] if parent: double.parent = parent return True