From 602ca108f19f7a46469afb7aab8ca9cf0fa3111e Mon Sep 17 00:00:00 2001 From: Simone Barbieri Date: Wed, 21 Apr 2021 17:27:17 +0100 Subject: [PATCH] Added support to Blender for JSON layout loading and publishing --- .../hosts/blender/plugins/load/load_layout.py | 8 +- .../hosts/blender/plugins/load/load_model.py | 14 +-- .../hosts/blender/plugins/load/load_rig.py | 14 +-- .../blender/plugins/publish/extract_layout.py | 93 +++++++++++++++++++ .../unreal/plugins/publish/extract_layout.py | 6 +- 5 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 openpype/hosts/blender/plugins/publish/extract_layout.py diff --git a/openpype/hosts/blender/plugins/load/load_layout.py b/openpype/hosts/blender/plugins/load/load_layout.py index f1f2fdcddd..87ef9670a6 100644 --- a/openpype/hosts/blender/plugins/load/load_layout.py +++ b/openpype/hosts/blender/plugins/load/load_layout.py @@ -367,13 +367,13 @@ class UnrealLayoutLoader(plugin.AssetLoader): # Y axis mirrored obj.location = ( location.get('x'), - -location.get('y'), + location.get('y'), location.get('z') ) obj.rotation_euler = ( - rotation.get('x') + math.pi / 2, - -rotation.get('y'), - -rotation.get('z') + rotation.get('x'), + rotation.get('y'), + rotation.get('z') ) obj.scale = ( scale.get('x'), diff --git a/openpype/hosts/blender/plugins/load/load_model.py b/openpype/hosts/blender/plugins/load/load_model.py index 7297e459a6..ed0f2faf17 100644 --- a/openpype/hosts/blender/plugins/load/load_model.py +++ b/openpype/hosts/blender/plugins/load/load_model.py @@ -108,19 +108,21 @@ class BlendModelLoader(plugin.AssetLoader): self.__class__.__name__, ) - container_metadata = container.get( - blender.pipeline.AVALON_PROPERTY) + metadata = container.get(blender.pipeline.AVALON_PROPERTY) - container_metadata["libpath"] = libpath - container_metadata["lib_container"] = lib_container + metadata["libpath"] = libpath + metadata["lib_container"] = lib_container obj_container = self._process( libpath, lib_container, container_name, None) - container_metadata["obj_container"] = obj_container + metadata["obj_container"] = obj_container # Save the list of objects in the metadata container - container_metadata["objects"] = obj_container.all_objects + metadata["objects"] = obj_container.all_objects + + metadata["parent"] = str(context["representation"]["parent"]) + metadata["family"] = context["representation"]["context"]["family"] nodes = list(container.objects) nodes.append(container) diff --git a/openpype/hosts/blender/plugins/load/load_rig.py b/openpype/hosts/blender/plugins/load/load_rig.py index c5690a6ab8..9035458c12 100644 --- a/openpype/hosts/blender/plugins/load/load_rig.py +++ b/openpype/hosts/blender/plugins/load/load_rig.py @@ -155,18 +155,20 @@ class BlendRigLoader(plugin.AssetLoader): self.__class__.__name__, ) - container_metadata = container.get( - blender.pipeline.AVALON_PROPERTY) + metadata = container.get(blender.pipeline.AVALON_PROPERTY) - container_metadata["libpath"] = libpath - container_metadata["lib_container"] = lib_container + metadata["libpath"] = libpath + metadata["lib_container"] = lib_container obj_container = self._process( libpath, lib_container, collection_name, None, None) - container_metadata["obj_container"] = obj_container + metadata["obj_container"] = obj_container # Save the list of objects in the metadata container - container_metadata["objects"] = obj_container.all_objects + metadata["objects"] = obj_container.all_objects + + metadata["parent"] = str(context["representation"]["parent"]) + metadata["family"] = context["representation"]["context"]["family"] nodes = list(container.objects) nodes.append(container) diff --git a/openpype/hosts/blender/plugins/publish/extract_layout.py b/openpype/hosts/blender/plugins/publish/extract_layout.py new file mode 100644 index 0000000000..09fae2fc12 --- /dev/null +++ b/openpype/hosts/blender/plugins/publish/extract_layout.py @@ -0,0 +1,93 @@ +import os +import json +import math + +import bpy + +from avalon import blender, io +import openpype.api + + +class ExtractLayout(openpype.api.Extractor): + """Extract a layout.""" + + label = "Extract Layout" + hosts = ["blender"] + families = ["layout"] + optional = True + + def process(self, instance): + # Define extract output file path + stagingdir = self.staging_dir(instance) + + # Perform extraction + self.log.info("Performing extraction..") + + json_data = [] + + for collection in instance: + for asset in collection.children: + collection = bpy.data.collections[asset.name] + container = bpy.data.collections[asset.name + '_CON'] + metadata = container.get(blender.pipeline.AVALON_PROPERTY) + + parent = metadata["parent"] + family = metadata["family"] + + self.log.debug("Parent: {}".format(parent)) + blend = io.find_one( + { + "type": "representation", + "parent": io.ObjectId(parent), + "name": "blend" + }, + projection={"_id": True}) + blend_id = blend["_id"] + + json_element = {} + json_element["reference"] = str(blend_id) + json_element["family"] = family + json_element["instance_name"] = asset.name + json_element["asset_name"] = metadata["lib_container"] + json_element["file_path"] = metadata["libpath"] + + obj = collection.objects[0] + + json_element["transform"] = { + "translation": { + "x": obj.location.x, + "y": obj.location.y, + "z": obj.location.z + }, + "rotation": { + "x": obj.rotation_euler.x, + "y": obj.rotation_euler.y, + "z": obj.rotation_euler.z, + }, + "scale": { + "x": obj.scale.x, + "y": obj.scale.y, + "z": obj.scale.z + } + } + json_data.append(json_element) + + json_filename = "{}.json".format(instance.name) + json_path = os.path.join(stagingdir, json_filename) + + with open(json_path, "w+") as file: + json.dump(json_data, fp=file, indent=2) + + if "representations" not in instance.data: + instance.data["representations"] = [] + + representation = { + 'name': 'json', + 'ext': 'json', + 'files': json_filename, + "stagingDir": stagingdir, + } + instance.data["representations"].append(representation) + + self.log.info("Extracted instance '%s' to: %s", + instance.name, representation) diff --git a/openpype/hosts/unreal/plugins/publish/extract_layout.py b/openpype/hosts/unreal/plugins/publish/extract_layout.py index 5924221f51..2d9f6eb3d1 100644 --- a/openpype/hosts/unreal/plugins/publish/extract_layout.py +++ b/openpype/hosts/unreal/plugins/publish/extract_layout.py @@ -78,14 +78,14 @@ class ExtractLayout(openpype.api.Extractor): json_element["transform"] = { "translation": { - "x": transform.translation.x, + "x": -transform.translation.x, "y": transform.translation.y, "z": transform.translation.z }, "rotation": { - "x": math.radians(transform.rotation.euler().x), + "x": math.radians(transform.rotation.euler().x + 90.0), "y": math.radians(transform.rotation.euler().y), - "z": math.radians(transform.rotation.euler().z), + "z": math.radians(180.0 - transform.rotation.euler().z) }, "scale": { "x": transform.scale3d.x,