From c69bf58d3bdc85a8b03a121419f0105f3d107412 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Tue, 16 Jun 2020 12:59:42 +0100 Subject: [PATCH] Fix publishing workfile - Unified template export across workfile and templates. - Fix scene saving per instance. - Cleanup template loader. --- pype/hosts/harmony/__init__.py | 45 +++++++++++++++ .../harmony/load/load_template_workfile.py | 25 --------- .../harmony/publish/extract_save_scene.py | 2 +- .../harmony/publish/extract_template.py | 55 +++---------------- .../harmony/publish/extract_workfile.py | 20 +++++-- 5 files changed, 70 insertions(+), 77 deletions(-) diff --git a/pype/hosts/harmony/__init__.py b/pype/hosts/harmony/__init__.py index cdb8b40777..4105759527 100644 --- a/pype/hosts/harmony/__init__.py +++ b/pype/hosts/harmony/__init__.py @@ -84,6 +84,51 @@ def ensure_scene_settings(): harmony.send({"function": func, "args": [valid_settings]}) +def export_template(backdrops, nodes, filepath): + func = """function func(args) + { + // Add an extra node just so a new group can be created. + var temp_node = node.add("Top", "temp_note", "NOTE", 0, 0, 0); + var template_group = node.createGroup(temp_node, "temp_group"); + node.deleteNode( template_group + "/temp_note" ); + + // This will make Node View to focus on the new group. + selection.clearSelection(); + selection.addNodeToSelection(template_group); + Action.perform("onActionEnterGroup()", "Node View"); + + // Recreate backdrops in group. + for (var i = 0 ; i < args[0].length; i++) + { + Backdrop.addBackdrop(template_group, args[0][i]); + }; + + // Copy-paste the selected nodes into the new group. + var drag_object = copyPaste.copy(args[1], 1, frame.numberOf, ""); + copyPaste.pasteNewNodes(drag_object, template_group, ""); + + // Select all nodes within group and export as template. + Action.perform( "selectAll()", "Node View" ); + copyPaste.createTemplateFromSelection(args[2], args[3]); + + // Unfocus the group in Node view, delete all nodes and backdrops + // created during the process. + Action.perform("onActionUpToParent()", "Node View"); + node.deleteNode(template_group, true, true); + } + func + """ + harmony.send({ + "function": func, + "args": [ + backdrops, + nodes, + os.path.basename(filepath), + os.path.dirname(filepath) + ] + }) + + def install(): print("Installing Pype config...") diff --git a/pype/plugins/harmony/load/load_template_workfile.py b/pype/plugins/harmony/load/load_template_workfile.py index 00d2e63c62..a9dcd0c776 100644 --- a/pype/plugins/harmony/load/load_template_workfile.py +++ b/pype/plugins/harmony/load/load_template_workfile.py @@ -14,18 +14,6 @@ class ImportTemplateLoader(api.Loader): label = "Import Template" def load(self, context, name=None, namespace=None, data=None): - # Make backdrops from metadata. - backdrops = context["representation"]["data"].get("backdrops", []) - - func = """function func(args) - { - Backdrop.addBackdrop("Top", args[0]); - } - func - """ - for backdrop in backdrops: - harmony.send({"function": func, "args": [backdrop]}) - # Import template. temp_dir = tempfile.mkdtemp() zip_file = api.get_representation_path(context["representation"]) @@ -33,19 +21,6 @@ class ImportTemplateLoader(api.Loader): with zipfile.ZipFile(zip_file, "r") as zip_ref: zip_ref.extractall(template_path) - func = """function func(args) - { - var template_path = args[0]; - var drag_object = copyPaste.copyFromTemplate( - template_path, 0, 0, copyPaste.getCurrentCreateOptions() - ); - copyPaste.pasteNewNodes( - drag_object, "", copyPaste.getCurrentPasteOptions() - ); - } - func - """ - func = """function func(args) { var template_path = args[0]; diff --git a/pype/plugins/harmony/publish/extract_save_scene.py b/pype/plugins/harmony/publish/extract_save_scene.py index 1733bdb95c..8b953580a7 100644 --- a/pype/plugins/harmony/publish/extract_save_scene.py +++ b/pype/plugins/harmony/publish/extract_save_scene.py @@ -9,5 +9,5 @@ class ExtractSaveScene(pyblish.api.ContextPlugin): order = pyblish.api.ExtractorOrder - 0.49 hosts = ["harmony"] - def process(self, instance): + def process(self, context): harmony.save_scene() diff --git a/pype/plugins/harmony/publish/extract_template.py b/pype/plugins/harmony/publish/extract_template.py index f7a5e34e67..1ba0befc54 100644 --- a/pype/plugins/harmony/publish/extract_template.py +++ b/pype/plugins/harmony/publish/extract_template.py @@ -2,7 +2,8 @@ import os import shutil import pype.api -from avalon import harmony +import avalon.harmony +import pype.hosts.harmony class ExtractTemplate(pype.api.Extractor): @@ -14,6 +15,7 @@ class ExtractTemplate(pype.api.Extractor): def process(self, instance): staging_dir = self.staging_dir(instance) + filepath = os.path.join(staging_dir, "{}.tpl".format(instance.name)) self.log.info("Outputting template to {}".format(staging_dir)) @@ -28,7 +30,7 @@ class ExtractTemplate(pype.api.Extractor): unique_backdrops = [backdrops[x] for x in set(backdrops.keys())] # Get non-connected nodes within backdrops. - all_nodes = harmony.send( + all_nodes = avalon.harmony.send( {"function": "node.subNodes", "args": ["Top"]} )["result"] for node in [x for x in all_nodes if x not in dependencies]: @@ -43,48 +45,9 @@ class ExtractTemplate(pype.api.Extractor): dependencies.remove(instance[0]) # Export template. - func = """function func(args) - { - // Add an extra node just so a new group can be created. - var temp_node = node.add("Top", "temp_note", "NOTE", 0, 0, 0); - var template_group = node.createGroup(temp_node, "temp_group"); - node.deleteNode( template_group + "/temp_note" ); - - // This will make Node View to focus on the new group. - selection.clearSelection(); - selection.addNodeToSelection(template_group); - Action.perform("onActionEnterGroup()", "Node View"); - - // Recreate backdrops in group. - for (var i = 0 ; i < args[0].length; i++) - { - Backdrop.addBackdrop(template_group, args[0][i]); - }; - - // Copy-paste the selected nodes into the new group. - var drag_object = copyPaste.copy(args[1], 1, frame.numberOf, ""); - copyPaste.pasteNewNodes(drag_object, template_group, ""); - - // Select all nodes within group and export as template. - Action.perform( "selectAll()", "Node View" ); - copyPaste.createTemplateFromSelection(args[2], args[3]); - - // Unfocus the group in Node view, delete all nodes and backdrops - // created during the process. - Action.perform("onActionUpToParent()", "Node View"); - node.deleteNode(template_group, true, true); - } - func - """ - harmony.send({ - "function": func, - "args": [ - unique_backdrops, - dependencies, - "{}.tpl".format(instance.name), - staging_dir - ] - }) + pype.hosts.harmony.export_template( + unique_backdrops, dependencies, filepath + ) # Prep representation. os.chdir(staging_dir) @@ -131,7 +94,7 @@ class ExtractTemplate(pype.api.Extractor): } func """ - return harmony.send( + return avalon.harmony.send( {"function": func, "args": [node]} )["result"] @@ -150,7 +113,7 @@ class ExtractTemplate(pype.api.Extractor): func """ - current_dependencies = harmony.send( + current_dependencies = avalon.harmony.send( {"function": func, "args": [node]} )["result"] diff --git a/pype/plugins/harmony/publish/extract_workfile.py b/pype/plugins/harmony/publish/extract_workfile.py index 7a0a7954dd..304b70e293 100644 --- a/pype/plugins/harmony/publish/extract_workfile.py +++ b/pype/plugins/harmony/publish/extract_workfile.py @@ -2,6 +2,8 @@ import os import shutil import pype.api +import avalon.harmony +import pype.hosts.harmony class ExtractWorkfile(pype.api.Extractor): @@ -12,17 +14,25 @@ class ExtractWorkfile(pype.api.Extractor): families = ["workfile"] def process(self, instance): - file_path = instance.context.data["currentFile"] + # Export template. + backdrops = avalon.harmony.send( + {"function": "Backdrop.backdrops", "args": ["Top"]} + )["result"] + nodes = avalon.harmony.send( + {"function": "node.subNodes", "args": ["Top"]} + )["result"] staging_dir = self.staging_dir(instance) + filepath = os.path.join(staging_dir, "{}.tpl".format(instance.name)) + pype.hosts.harmony.export_template(backdrops, nodes, filepath) + + # Prep representation. os.chdir(staging_dir) shutil.make_archive( - instance.name, + "{}".format(instance.name), "zip", - os.path.dirname(file_path) + os.path.join(staging_dir, "{}.tpl".format(instance.name)) ) - zip_path = os.path.join(staging_dir, instance.name + ".zip") - self.log.info(f"Output zip file: {zip_path}") representation = { "name": "tpl",