diff --git a/openpype/hosts/unreal/plugins/create/create_look.py b/openpype/hosts/unreal/plugins/create/create_look.py new file mode 100644 index 0000000000..16257f1e3c --- /dev/null +++ b/openpype/hosts/unreal/plugins/create/create_look.py @@ -0,0 +1,66 @@ +import unreal +from openpype.hosts.unreal.api.plugin import Creator +from avalon.unreal import pipeline + + +class CreateLook(Creator): + """Shader connections defining shape look""" + + name = "unrealLook" + label = "Unreal - Look" + family = "look" + icon = "paint-brush" + + root = "/Game/Avalon/Assets" + suffix = "_INS" + + def __init__(self, *args, **kwargs): + super(CreateLook, self).__init__(*args, **kwargs) + + def process(self): + name = self.data["subset"] + + selection = [] + if (self.options or {}).get("useSelection"): + sel_objects = unreal.EditorUtilityLibrary.get_selected_assets() + selection = [a.get_path_name() for a in sel_objects] + + # Create the folder + path = f"{self.root}/{self.data['asset']}" + new_name = pipeline.create_folder(path, name) + full_path = f"{path}/{new_name}" + + # Create a new cube static mesh + ar = unreal.AssetRegistryHelpers.get_asset_registry() + cube = ar.get_asset_by_object_path("/Engine/BasicShapes/Cube.Cube") + + # Create the avalon publish instance object + container_name = f"{name}{self.suffix}" + pipeline.create_publish_instance( + instance=container_name, path=full_path) + + # Get the mesh of the selected object + original_mesh = ar.get_asset_by_object_path(selection[0]).get_asset() + materials = original_mesh.get_editor_property('materials') + + self.data["members"] = [] + + # Add the materials to the cube + for material in materials: + name = material.get_editor_property('material_slot_name') + object_path = f"{full_path}/{name}.{name}" + object = unreal.EditorAssetLibrary.duplicate_loaded_asset( + cube.get_asset(), object_path + ) + + # Remove the default material of the cube object + object.get_editor_property('static_materials').pop() + + object.add_material( + material.get_editor_property('material_interface')) + + self.data["members"].append(object_path) + + unreal.EditorAssetLibrary.save_asset(object_path) + + pipeline.imprint(f"{full_path}/{container_name}", self.data) \ No newline at end of file diff --git a/openpype/hosts/unreal/plugins/publish/extract_look.py b/openpype/hosts/unreal/plugins/publish/extract_look.py new file mode 100644 index 0000000000..6b5fcbac35 --- /dev/null +++ b/openpype/hosts/unreal/plugins/publish/extract_look.py @@ -0,0 +1,85 @@ +import json +import os + +import unreal + +import openpype.api +from avalon import io + + +class ExtractLook(openpype.api.Extractor): + """Extract look.""" + + label = "Extract Look" + hosts = ["unreal"] + families = ["look"] + optional = True + + def process(self, instance): + # Define extract output file path + stagingdir = self.staging_dir(instance) + resources_dir = instance.data["resourcesDir"] + + ar = unreal.AssetRegistryHelpers.get_asset_registry() + + transfers = [] + + json_data = [] + + for member in instance: + asset = ar.get_asset_by_object_path(member) + object = asset.get_asset() + + name = asset.get_editor_property('asset_name') + + fbx_filename = f"{instance.name}_{name}.fbx" + + fbx_exporter = unreal.StaticMeshExporterFBX() + fbx_exporter.set_editor_property('text', False) + + options = unreal.FbxExportOption() + options.set_editor_property('ascii', False) + options.set_editor_property('collision', False) + + task = unreal.AssetExportTask() + task.set_editor_property('exporter', fbx_exporter) + task.set_editor_property('options', options) + task.set_editor_property('automated', True) + task.set_editor_property('object', object) + task.set_editor_property( + 'filename', f"{stagingdir}/{fbx_filename}") + task.set_editor_property('prompt', False) + task.set_editor_property('selected', False) + + unreal.Exporter.run_asset_export_task(task) + + transfers.append(( + f"{stagingdir}/{fbx_filename}", + f"{resources_dir}/{fbx_filename}")) + + json_element = { + 'material': str(name), + 'filename': fbx_filename + } + json_data.append(json_element) + + json_filename = f"{instance.name}.json" + json_path = os.path.join(stagingdir, json_filename) + + with open(json_path, "w+") as file: + json.dump(json_data, fp=file, indent=2) + + if "transfers" not in instance.data: + instance.data["transfers"] = [] + if "representations" not in instance.data: + instance.data["representations"] = [] + + json_representation = { + 'name': 'json', + 'ext': 'json', + 'files': json_filename, + "stagingDir": stagingdir, + } + + instance.data["representations"].append(json_representation) + instance.data["transfers"].extend(transfers)