diff --git a/openpype/hosts/blender/plugins/load/import_workfile.py b/openpype/hosts/blender/plugins/load/import_workfile.py index 2849031f7e..ed24140a59 100644 --- a/openpype/hosts/blender/plugins/load/import_workfile.py +++ b/openpype/hosts/blender/plugins/load/import_workfile.py @@ -1,8 +1,20 @@ +from pathlib import Path + import bpy from openpype.hosts.blender.api import plugin +def get_unique_number(asset, subset): + count = 1 + name = f"{asset}_{count:0>2}_{subset}" + collection_names = [coll.name for coll in bpy.data.collections] + while name in collection_names: + count += 1 + name = f"{asset}_{count:0>2}_{subset}" + return f"{count:0>2}" + + class AppendBlendLoader(plugin.AssetLoader): """Append workfile in Blender (unmanaged) @@ -28,6 +40,7 @@ class AppendBlendLoader(plugin.AssetLoader): # We do not containerize imported content, it remains unmanaged return + class ImportBlendLoader(plugin.AssetLoader): """Import workfile in the current Blender scene (unmanaged) @@ -46,16 +59,26 @@ class ImportBlendLoader(plugin.AssetLoader): color = "#775555" def load(self, context, name=None, namespace=None, data=None): + asset = context['asset']['name'] + subset = context['subset']['name'] + + unique_number = get_unique_number(asset, subset) + group_name = plugin.asset_name(asset, subset, unique_number) + with bpy.data.libraries.load(self.fname) as (data_from, data_to): for attr in dir(data_to): - if attr == "scenes": - continue setattr(data_to, attr, getattr(data_from, attr)) - # Add objects to current scene - scene = bpy.context.scene - for obj in data_to.objects: - scene.collection.objects.link(obj) + current_scene = bpy.context.scene + + for scene in data_to.scenes: + # scene.name = group_name + collection = bpy.data.collections.new(name=group_name) + for obj in scene.objects: + collection.objects.link(obj) + current_scene.collection.children.link(collection) + for coll in scene.collection.children: + collection.children.link(coll) # We do not containerize imported content, it remains unmanaged return