From 369e35c6ef17f60c1b9e380c4b19c2646a641d9e Mon Sep 17 00:00:00 2001 From: Toke Stuart Jepsen Date: Thu, 11 Jun 2020 23:39:53 +0100 Subject: [PATCH] Loader for templates and workfiles --- .../harmony/load/load_template_workfile.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 pype/plugins/harmony/load/load_template_workfile.py diff --git a/pype/plugins/harmony/load/load_template_workfile.py b/pype/plugins/harmony/load/load_template_workfile.py new file mode 100644 index 0000000000..00d2e63c62 --- /dev/null +++ b/pype/plugins/harmony/load/load_template_workfile.py @@ -0,0 +1,69 @@ +import tempfile +import zipfile +import os +import shutil + +from avalon import api, harmony + + +class ImportTemplateLoader(api.Loader): + """Import templates.""" + + families = ["harmony.template"] + representations = ["*"] + 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"]) + 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) + + +class ImportWorkfileLoader(ImportTemplateLoader): + """Import workfiles.""" + + families = ["workfile"] + representations = ["*"] + label = "Import Workfile"