implement new 'get_representation_path_v2' function

This commit is contained in:
Jakub Trllo 2025-09-24 12:12:30 +02:00
parent da93a0a364
commit 80ba7ea5ed
2 changed files with 55 additions and 3 deletions

View file

@ -25,6 +25,7 @@ from .utils import (
get_loader_identifier,
get_loaders_by_name,
get_representation_path_v2,
get_representation_path_from_context,
get_representation_path,
get_representation_path_with_anatomy,
@ -85,6 +86,7 @@ __all__ = (
"get_loader_identifier",
"get_loaders_by_name",
"get_representation_path_v2",
"get_representation_path_from_context",
"get_representation_path",
"get_representation_path_with_anatomy",

View file

@ -14,9 +14,8 @@ from ayon_core.lib import (
StringTemplate,
TemplateUnsolved,
)
from ayon_core.pipeline import (
Anatomy,
)
from ayon_core.lib.path_templates import TemplateResult
from ayon_core.pipeline import Anatomy
log = logging.getLogger(__name__)
@ -638,6 +637,57 @@ def _fix_representation_context_compatibility(repre_context):
repre_context["udim"] = udim[0]
def get_representation_path_v2(
project_name: str,
repre_entity: dict[str, Any],
*,
anatomy: Optional[Anatomy] = None,
project_entity: Optional[dict[str, Any]] = None,
) -> TemplateResult:
"""Get filled representation path.
Args:
project_name (str): Project name.
repre_entity (dict[str, Any]): Representation entity.
anatomy (Optional[Anatomy]): Project anatomy.
project_entity (Optional[dict[str, Any]): Project entity. Is used to
initialize Anatomy and is not needed if 'anatomy' is passed in.
Returns:
TemplateResult: Resolved path to representation.
Raises:
InvalidRepresentationContext: When representation data are probably
invalid or not available.
"""
if anatomy is None:
anatomy = Anatomy(project_name, project_entity=project_entity)
try:
template = repre_entity["attrib"]["template"]
except KeyError:
raise InvalidRepresentationContext(
"Representation document does not"
" contain template in data ('data.template')"
)
try:
context = copy.deepcopy(repre_entity["context"])
_fix_representation_context_compatibility(context)
context["root"] = anatomy.roots
path = StringTemplate.format_strict_template(template, context)
except TemplateUnsolved as exc:
raise InvalidRepresentationContext(
"Couldn't resolve representation template with available data."
f" Reason: {str(exc)}"
)
return path.normalized()
def get_representation_path_from_context(context):
"""Preparation wrapper using only context as a argument"""
from ayon_core.pipeline import get_current_project_name