Skip to content

Commit

Permalink
Add abstract template loader to OP lib
Browse files Browse the repository at this point in the history
Also add posibility to find template loader child class
by heritance instead of by name
  • Loading branch information
BenoitConnan authored and ClementHector committed Feb 8, 2022
1 parent 8c65d01 commit 721701f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 26 deletions.
7 changes: 3 additions & 4 deletions openpype/hosts/maya/api/template_loader.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from openpype.lib.abstract_load_template import AbstractTemplateLoader,\
AbstractPlaceholder
import openpype.lib
from maya import cmds

PLACEHOLDER_SET = 'PLACEHOLDERS_SET'


class TemplateLoader(AbstractTemplateLoader):
class MayaTemplateLoader(openpype.lib.AbstractTemplateLoader):
"""Concrete implementation of AbstractTemplateLoader for maya
"""
Expand Down Expand Up @@ -34,7 +33,7 @@ def get_template_nodes():
return [attribute.rpartition('.')[0] for attribute in attributes]


class Placeholder(AbstractPlaceholder):
class MayaPlaceholder(openpype.lib.AbstractPlaceholder):
"""Concrete implementation of AbstractPlaceholder for maya
"""
Expand Down
8 changes: 8 additions & 0 deletions openpype/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@
is_current_version_higher_than_expected
)

from .abstract_template_loader import (
AbstractPlaceholder,
AbstractTemplateLoader
)

terminal = Terminal

__all__ = [
Expand Down Expand Up @@ -317,4 +322,7 @@
"is_running_from_build",
"is_running_staging",
"is_current_version_studio_latest",

"AbstractPlaceholder",
"AbstractTemplateLoader"
]
File renamed without changes.
58 changes: 36 additions & 22 deletions openpype/lib/build_template.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,43 @@
import avalon
import openpype
from openpype.lib import AbstractPlaceholder, AbstractTemplateLoader

import importlib

concrete_loaders_modules = {
'maya': 'openpype.hosts.maya.api.template_loader'
}
module_path_format = 'openpype.hosts.{host}.api.template_loader'


def build_workfile_template(self):
host_name = avalon.io.Session['AVALON_APP']
module_path = concrete_loaders_modules.get(host_name, None)
host_name = openpype.avalon.registered_host().__name__.partition('.')[2]
module_path = module_path_format.format(host=host_name)
module = importlib.import_module(module_path)
if not module:
raise MissingHostTemplateModule(
"No template loader found for host {}".format(host_name))

if not module_path:
raise NotImplementedError(
"Template not found for host '{}'".format(host_name)
)
template_loader_class = openpype.lib.classes_from_module(
AbstractTemplateLoader, module)
template_placeholder_class = openpype.lib.classes_from_module(
AbstractPlaceholder, module)

module = importlib.import_module(module_path)
if not hasattr(module, 'TemplateLoader'):
raise NotImplementedError(
"Linked module '{}' does not "
"implement a template loader".format(module_path))
if not hasattr(module, 'Placeholder'):
raise NotImplementedError(
"Linked module '{}' does not "
"implement a placeholder template".format(module_path))
concrete_loader = module.TemplateLoader
concrete_template_loader = concrete_loader(module.Placeholder)
concrete_template_loader.process()
if not template_loader_class:
raise MissingTemplateLoaderClass()
template_loader_class = template_loader_class[0]

if not template_placeholder_class:
raise MissingTemplatePlaceholderClass()
template_placeholder_class = template_placeholder_class[0]

template_loader = template_loader_class(template_placeholder_class)
template_loader.process()

class MissingHostTemplateModule(Exception):
"""Error raised when expected module does not exists"""
pass

class MissingTemplatePlaceholderClass(Exception):
""" """
pass

class MissingTemplateLoaderClass(Exception):
""" """
pass

0 comments on commit 721701f

Please sign in to comment.