From 9e8bb4baa84d2f81a1c9408ce2e932dd7f79c063 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Fri, 22 May 2020 17:24:15 +0100 Subject: [PATCH 1/3] Harmony template publish --- pype/harmony/__init__.py | 22 +++++ pype/plugins/global/publish/integrate_new.py | 5 +- pype/plugins/harmony/create/template.py | 12 +++ .../harmony/publish/collect_current_file.py | 27 ++++++ .../harmony/publish/collect_instances.py | 44 +++++++++ .../harmony/publish/extract_template.py | 90 +++++++++++++++++++ 6 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 pype/harmony/__init__.py create mode 100644 pype/plugins/harmony/create/template.py create mode 100644 pype/plugins/harmony/publish/collect_current_file.py create mode 100644 pype/plugins/harmony/publish/collect_instances.py create mode 100644 pype/plugins/harmony/publish/extract_template.py diff --git a/pype/harmony/__init__.py b/pype/harmony/__init__.py new file mode 100644 index 0000000000..8ac21d3cd3 --- /dev/null +++ b/pype/harmony/__init__.py @@ -0,0 +1,22 @@ +import os + +import avalon.api +import pyblish.api + + +def install(): + print("Installing Pype config...") + + plugins_directory = os.path.join( + os.path.dirname(os.path.dirname(__file__)), "plugins", "harmony" + ) + + pyblish.api.register_plugin_path( + os.path.join(plugins_directory, "publish") + ) + avalon.api.register_plugin_path( + avalon.api.Loader, os.path.join(plugins_directory, "load") + ) + avalon.api.register_plugin_path( + avalon.api.Creator, os.path.join(plugins_directory, "create") + ) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 0cd46d8891..985a1d2ee7 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -81,7 +81,8 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): "assembly", "fbx", "textures", - "action" + "action", + "template" ] exclude_families = ["clip"] db_representation_context_keys = [ @@ -743,7 +744,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): matching_profiles[name] = filters if len(matching_profiles) == 1: - template_name = matching_profiles.keys()[0] + template_name = tuple(matching_profiles.keys())[0] self.log.debug( "Using template name \"{}\".".format(template_name) ) diff --git a/pype/plugins/harmony/create/template.py b/pype/plugins/harmony/create/template.py new file mode 100644 index 0000000000..6268127687 --- /dev/null +++ b/pype/plugins/harmony/create/template.py @@ -0,0 +1,12 @@ +from avalon import harmony + + +class CreateTemplate(harmony.Creator): + """Composite node for publishing to templates.""" + + name = "templateDefault" + label = "Template" + family = "template" + + def __init__(self, *args, **kwargs): + super(CreateTemplate, self).__init__(*args, **kwargs) diff --git a/pype/plugins/harmony/publish/collect_current_file.py b/pype/plugins/harmony/publish/collect_current_file.py new file mode 100644 index 0000000000..aab66c2b62 --- /dev/null +++ b/pype/plugins/harmony/publish/collect_current_file.py @@ -0,0 +1,27 @@ +import os + +import pyblish.api +from avalon import harmony + + +class CollectCurrentFile(pyblish.api.ContextPlugin): + """Inject the current working file into context""" + + order = pyblish.api.CollectorOrder - 0.5 + label = "Current File" + hosts = ["harmony"] + + def process(self, context): + """Inject the current working file""" + func = """function func() + { + return ( + scene.currentProjectPath() + "/" + + scene.currentVersionName() + ".xstage" + ); + } + func + """ + + current_file = harmony.send({"function": func})["result"] + context.data["currentFile"] = os.path.normpath(current_file) diff --git a/pype/plugins/harmony/publish/collect_instances.py b/pype/plugins/harmony/publish/collect_instances.py new file mode 100644 index 0000000000..1ed49e3eb8 --- /dev/null +++ b/pype/plugins/harmony/publish/collect_instances.py @@ -0,0 +1,44 @@ +import pyblish.api +from avalon import harmony + + +class CollectInstances(pyblish.api.ContextPlugin): + """Gather instances by nodes metadata. + + This collector takes into account assets that are associated with + a composite node and marked with a unique identifier; + + Identifier: + id (str): "pyblish.avalon.instance" + """ + + label = "Instances" + order = pyblish.api.CollectorOrder + hosts = ["harmony"] + + def process(self, context): + nodes = harmony.send( + {"function": "node.getNodes", "args": [["COMPOSITE"]]} + )["result"] + + for node in nodes: + data = harmony.read(node) + + # Skip non-tagged nodes. + if not data: + continue + + # Skip containers. + if "container" in data["id"]: + continue + + # Adding families if missing. + data["families"] = data.get("families", []) + + instance = context.create_instance(node.split("/")[-1]) + instance.append(node) + instance.data.update(data) + + # Produce diagnostic message for any graphical + # user interface interested in visualising it. + self.log.info("Found: \"%s\" " % instance.data["name"]) diff --git a/pype/plugins/harmony/publish/extract_template.py b/pype/plugins/harmony/publish/extract_template.py new file mode 100644 index 0000000000..9347620dc2 --- /dev/null +++ b/pype/plugins/harmony/publish/extract_template.py @@ -0,0 +1,90 @@ +import os +import shutil + +import pype.api +from avalon import harmony + + +class ExtractTemplate(pype.api.Extractor): + """Extract the connected nodes to the composite instance.""" + label = "Extract Template" + hosts = ["harmony"] + families = ["template"] + + def process(self, instance): + staging_dir = self.staging_dir(instance) + + self.log.info("Outputting template to %s" % staging_dir) + + self.dependencies = [] + self.get_dependencies(instance[0]) + + func = """function func(args) + { + var nodes = args[0]; + selection.clearSelection(); + for (var i = 0 ; i < nodes.length; i++) + { + selection.addNodeToSelection(nodes[i]); + } + } + func + """ + harmony.send({"function": func, "args": [self.dependencies]}) + func = """function func(args) + { + copyPaste.createTemplateFromSelection(args[0], args[1]); + } + func + """ + harmony.send( + { + "function": func, + "args": ["{}.tpl".format(instance.name), staging_dir] + } + ) + + os.chdir(staging_dir) + shutil.make_archive( + "{}".format(instance.name), + "zip", + os.path.join(staging_dir, "{}.tpl".format(instance.name)) + ) + + representation = { + "name": "tpl", + "ext": "zip", + "files": "{}.zip".format(instance.name), + "stagingDir": staging_dir, + } + instance.data["representations"] = [representation] + + def get_dependencies(self, node): + func = """function func(args) + { + var target_node = args[0]; + var numInput = node.numberOfInputPorts(target_node); + var dependencies = []; + for (var i = 0 ; i < numInput; i++) + { + dependencies.push(node.srcNode(target_node, i)); + } + return dependencies; + } + func + """ + + current_dependencies = harmony.send( + {"function": func, "args": [node]} + )["result"] + + for dependency in current_dependencies: + if not dependency: + continue + + if dependency in self.dependencies: + continue + + self.dependencies.append(dependency) + + self.get_dependencies(dependency) From 06b55cbcaaf2b324ab83a551ab61167722956c24 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 25 May 2020 11:38:58 +0100 Subject: [PATCH 2/3] Import templates. --- .../{template.py => create_template.py} | 0 pype/plugins/harmony/load/load_template.py | 48 +++++++++++++++++++ 2 files changed, 48 insertions(+) rename pype/plugins/harmony/create/{template.py => create_template.py} (100%) create mode 100644 pype/plugins/harmony/load/load_template.py diff --git a/pype/plugins/harmony/create/template.py b/pype/plugins/harmony/create/create_template.py similarity index 100% rename from pype/plugins/harmony/create/template.py rename to pype/plugins/harmony/create/create_template.py diff --git a/pype/plugins/harmony/load/load_template.py b/pype/plugins/harmony/load/load_template.py new file mode 100644 index 0000000000..711c43a913 --- /dev/null +++ b/pype/plugins/harmony/load/load_template.py @@ -0,0 +1,48 @@ +import tempfile +import zipfile +import os +import shutil + +from avalon import api, harmony + + +class ImportTemplateLoader(api.Loader): + """Import templates.""" + + families = ["template"] + representations = ["*"] + label = "Import Template" + + def load(self, context, name=None, namespace=None, data=None): + temp_dir = tempfile.mkdtemp() + zip_file = api.get_representation_path(context["representation"]) + template_path = os.path.join(temp_dir, "temp.tpl") + 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]; + var drag_object = copyPaste.pasteTemplateIntoGroup( + template_path, "Top", 1 + ); + } + func + """ + + harmony.send({"function": func, "args": [template_path]}) + + shutil.rmtree(temp_dir) From 654f7b2f7d738e52e52771283996033e8820c360 Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Mon, 25 May 2020 12:54:09 +0100 Subject: [PATCH 3/3] Change family name. --- pype/plugins/global/publish/integrate_new.py | 2 +- pype/plugins/harmony/create/create_template.py | 2 +- pype/plugins/harmony/load/load_template.py | 2 +- pype/plugins/harmony/publish/extract_template.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pype/plugins/global/publish/integrate_new.py b/pype/plugins/global/publish/integrate_new.py index 3758612767..fd76b19eb9 100644 --- a/pype/plugins/global/publish/integrate_new.py +++ b/pype/plugins/global/publish/integrate_new.py @@ -82,7 +82,7 @@ class IntegrateAssetNew(pyblish.api.InstancePlugin): "fbx", "textures", "action", - "template" + "harmony.template" ] exclude_families = ["clip"] db_representation_context_keys = [ diff --git a/pype/plugins/harmony/create/create_template.py b/pype/plugins/harmony/create/create_template.py index 6268127687..babc3fe8d7 100644 --- a/pype/plugins/harmony/create/create_template.py +++ b/pype/plugins/harmony/create/create_template.py @@ -6,7 +6,7 @@ class CreateTemplate(harmony.Creator): name = "templateDefault" label = "Template" - family = "template" + family = "harmony.template" def __init__(self, *args, **kwargs): super(CreateTemplate, self).__init__(*args, **kwargs) diff --git a/pype/plugins/harmony/load/load_template.py b/pype/plugins/harmony/load/load_template.py index 711c43a913..0bc9706407 100644 --- a/pype/plugins/harmony/load/load_template.py +++ b/pype/plugins/harmony/load/load_template.py @@ -9,7 +9,7 @@ from avalon import api, harmony class ImportTemplateLoader(api.Loader): """Import templates.""" - families = ["template"] + families = ["harmony.template"] representations = ["*"] label = "Import Template" diff --git a/pype/plugins/harmony/publish/extract_template.py b/pype/plugins/harmony/publish/extract_template.py index 9347620dc2..78184217dc 100644 --- a/pype/plugins/harmony/publish/extract_template.py +++ b/pype/plugins/harmony/publish/extract_template.py @@ -9,7 +9,7 @@ class ExtractTemplate(pype.api.Extractor): """Extract the connected nodes to the composite instance.""" label = "Extract Template" hosts = ["harmony"] - families = ["template"] + families = ["harmony.template"] def process(self, instance): staging_dir = self.staging_dir(instance)