From 5e54d09ca1ce64d3f6cd5a9dd2248315330002f8 Mon Sep 17 00:00:00 2001 From: Simone Barbieri Date: Wed, 14 Apr 2021 11:37:36 +0100 Subject: [PATCH] Alembic loader is now more consistent with loaders for other formats --- .../hosts/blender/plugins/load/load_model.py | 69 +++++++++++-------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/openpype/hosts/blender/plugins/load/load_model.py b/openpype/hosts/blender/plugins/load/load_model.py index d679807534..f587a20fa1 100644 --- a/openpype/hosts/blender/plugins/load/load_model.py +++ b/openpype/hosts/blender/plugins/load/load_model.py @@ -7,7 +7,6 @@ from typing import Dict, List, Optional from avalon import api, blender import bpy -import openpype.api import openpype.hosts.blender.api.plugin as plugin @@ -109,10 +108,6 @@ class BlendModelLoader(plugin.AssetLoader): self.__class__.__name__, ) - print(lib_container) - print(container) - - container_metadata = container.get( blender.pipeline.AVALON_PROPERTY) @@ -265,6 +260,41 @@ class CacheModelLoader(plugin.AssetLoader): icon = "code-fork" color = "orange" + def _process(self, libpath, container_name, parent_collection): + bpy.ops.object.select_all(action='DESELECT') + + view_layer = bpy.context.view_layer + view_layer_collection = view_layer.active_layer_collection.collection + + relative = bpy.context.preferences.filepaths.use_relative_paths + bpy.ops.wm.alembic_import( + filepath=libpath, + relative_path=relative + ) + + parent = parent_collection + + if parent is None: + parent = bpy.context.scene.collection + + model_container = bpy.data.collections.new(container_name) + parent.children.link(model_container) + for obj in bpy.context.selected_objects: + model_container.objects.link(obj) + view_layer_collection.objects.unlink(obj) + obj.name = f"{obj.name}:{container_name}" + obj.data.name = f"{obj.data.name}:{container_name}" + + if not obj.get(blender.pipeline.AVALON_PROPERTY): + obj[blender.pipeline.AVALON_PROPERTY] = dict() + + avalon_info = obj[blender.pipeline.AVALON_PROPERTY] + avalon_info.update({"container_name": container_name}) + + bpy.ops.object.select_all(action='DESELECT') + + return model_container + def process_asset( self, context: dict, name: str, namespace: Optional[str] = None, options: Optional[Dict] = None @@ -308,30 +338,15 @@ class CacheModelLoader(plugin.AssetLoader): container_metadata["libpath"] = libpath container_metadata["lib_container"] = lib_container - bpy.ops.object.select_all(action='DESELECT') + obj_container = self._process( + libpath, container_name, None) - view_layer = bpy.context.view_layer - view_layer_collection = view_layer.active_layer_collection.collection + container_metadata["obj_container"] = obj_container - relative = bpy.context.preferences.filepaths.use_relative_paths - bpy.ops.wm.alembic_import( - filepath=libpath, - relative_path=relative - ) + # Save the list of objects in the metadata container + container_metadata["objects"] = obj_container.all_objects - parent = bpy.data.collections.new(container_name) - for obj in bpy.context.selected_objects: - parent.objects.link(obj) - view_layer_collection.objects.unlink(obj) - obj.name = f"{obj.name}:{container_name}" - obj.data.name = f"{obj.data.name}:{container_name}" - - bpy.ops.object.select_all(action='DESELECT') - - scene = bpy.context.scene - scene.collection.children.link(parent) - - nodes = list(parent.objects) - nodes.append(parent) + nodes = list(container.objects) + nodes.append(container) self[:] = nodes return nodes