Import templates.

This commit is contained in:
Toke Stuart Jepsen 2020-05-25 11:38:58 +01:00
parent 9e8bb4baa8
commit 06b55cbcaa
2 changed files with 48 additions and 0 deletions

View file

@ -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)