Added support for layouts in new blend loader

This commit is contained in:
Simone Barbieri 2023-06-16 15:29:09 +01:00
parent 1fe5cb0e0c
commit d6aabc93dd

View file

@ -4,9 +4,11 @@ from pathlib import Path
import bpy
from openpype.pipeline import (
legacy_create,
get_representation_path,
AVALON_CONTAINER_ID,
)
from openpype.pipeline.create import get_legacy_creator_by_name
from openpype.hosts.blender.api import plugin
from openpype.hosts.blender.api.lib import imprint
from openpype.hosts.blender.api.pipeline import (
@ -18,7 +20,7 @@ from openpype.hosts.blender.api.pipeline import (
class BlendLoader(plugin.AssetLoader):
"""Load assets from a .blend file."""
families = ["model", "rig"]
families = ["model", "rig", "layout"]
representations = ["blend"]
label = "Load Blend"
@ -35,6 +37,31 @@ class BlendLoader(plugin.AssetLoader):
return None
def _post_process_layout(self, container, asset, representation):
rigs = [
obj for obj in container.children_recursive
if (
obj.type == 'EMPTY' and
obj.get(AVALON_PROPERTY) and
obj.get(AVALON_PROPERTY).get('family') == 'rig'
)
]
for rig in rigs:
creator_plugin = get_legacy_creator_by_name("CreateAnimation")
legacy_create(
creator_plugin,
name=rig.name.split(':')[-1] + "_animation",
asset=asset,
options={
"useSelection": False,
"asset_group": rig
},
data={
"dependencies": representation
}
)
def _process_data(self, libpath, group_name):
# Append all the data from the .blend file
with bpy.data.libraries.load(
@ -61,6 +88,10 @@ class BlendLoader(plugin.AssetLoader):
for obj in container.children_recursive:
bpy.context.scene.collection.objects.link(obj)
# Remove the library from the blend file
library = bpy.data.libraries.get(bpy.path.basename(libpath))
bpy.data.libraries.remove(library)
return container
def process_asset(
@ -78,6 +109,13 @@ class BlendLoader(plugin.AssetLoader):
asset = context["asset"]["name"]
subset = context["subset"]["name"]
try:
family = context["representation"]["context"]["family"]
except ValueError:
family = "model"
representation = str(context["representation"]["_id"])
asset_name = plugin.asset_name(asset, subset)
unique_number = plugin.get_unique_number(asset, subset)
group_name = plugin.asset_name(asset, subset, unique_number)
@ -90,11 +128,10 @@ class BlendLoader(plugin.AssetLoader):
container = self._process_data(libpath, group_name)
avalon_container.objects.link(container)
if family == "layout":
self._post_process_layout(container, asset, representation)
# Remove the library from the blend file
library = bpy.data.libraries.get(bpy.path.basename(libpath))
bpy.data.libraries.remove(library)
avalon_container.objects.link(container)
data = {
"schema": "openpype:container-2.0",
@ -134,6 +171,7 @@ class BlendLoader(plugin.AssetLoader):
transform = asset_group.matrix_basis.copy()
old_data = dict(asset_group.get(AVALON_PROPERTY))
parent = asset_group.parent
self.exec_remove(container)
@ -142,11 +180,8 @@ class BlendLoader(plugin.AssetLoader):
avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
avalon_container.objects.link(asset_group)
# Remove the library from the blend file
library = bpy.data.libraries.get(bpy.path.basename(libpath))
bpy.data.libraries.remove(library)
asset_group.matrix_basis = transform
asset_group.parent = parent
asset_group[AVALON_PROPERTY] = old_data