Merge branch 'develop' into enhancement/initial-support-for-folder-in-product-name

# Conflicts:
#	client/ayon_core/pipeline/create/product_name.py
This commit is contained in:
Jakub Trllo 2025-12-05 17:45:11 +01:00
commit b1db949ecc
29 changed files with 1360 additions and 246 deletions

View file

@ -15,6 +15,7 @@ from typing import (
Any,
Callable,
)
from warnings import warn
import pyblish.logic
import pyblish.api
@ -752,13 +753,13 @@ class CreateContext:
manual_creators = {}
report = discover_creator_plugins(return_report=True)
self.creator_discover_result = report
for creator_class in report.plugins:
if inspect.isabstract(creator_class):
self.log.debug(
"Skipping abstract Creator {}".format(str(creator_class))
)
continue
for creator_class in report.abstract_plugins:
self.log.debug(
"Skipping abstract Creator '%s'",
str(creator_class)
)
for creator_class in report.plugins:
creator_identifier = creator_class.identifier
if creator_identifier in creators:
self.log.warning(
@ -772,19 +773,17 @@ class CreateContext:
creator_class.host_name
and creator_class.host_name != self.host_name
):
self.log.info((
"Creator's host name \"{}\""
" is not supported for current host \"{}\""
).format(creator_class.host_name, self.host_name))
self.log.info(
(
'Creator\'s host name "{}"'
' is not supported for current host "{}"'
).format(creator_class.host_name, self.host_name)
)
continue
# TODO report initialization error
try:
creator = creator_class(
project_settings,
self,
self.headless
)
creator = creator_class(project_settings, self, self.headless)
except Exception:
self.log.error(
f"Failed to initialize plugin: {creator_class}",
@ -792,6 +791,19 @@ class CreateContext:
)
continue
if not creator.product_base_type:
message = (
f"Provided creator {creator!r} doesn't have "
"product base type attribute defined. This will be "
"required in future."
)
warn(
message,
DeprecationWarning,
stacklevel=2
)
self.log.warning(message)
if not creator.enabled:
disabled_creators[creator_identifier] = creator
continue
@ -1289,8 +1301,12 @@ class CreateContext:
"folderPath": folder_entity["path"],
"task": task_entity["name"] if task_entity else None,
"productType": creator.product_type,
# Add product base type if supported. Fallback to product type
"productBaseType": (
creator.product_base_type or creator.product_type),
"variant": variant
}
if active is not None:
if not isinstance(active, bool):
self.log.warning(

View file

@ -1,20 +1,21 @@
# -*- coding: utf-8 -*-
import os
import copy
import collections
from typing import TYPE_CHECKING, Optional, Dict, Any
"""Creator plugins for the create process."""
from __future__ import annotations
import collections
import copy
import os
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, Any, Dict, Optional
from ayon_core.lib import Logger, get_version_from_path
from ayon_core.pipeline.plugin_discover import (
deregister_plugin,
deregister_plugin_path,
discover,
register_plugin,
register_plugin_path,
deregister_plugin,
deregister_plugin_path
)
from ayon_core.pipeline.staging_dir import get_staging_dir_info, StagingDir
from ayon_core.pipeline.staging_dir import StagingDir, get_staging_dir_info
from .constants import DEFAULT_VARIANT_VALUE
from .product_name import get_product_name
@ -23,6 +24,7 @@ from .structures import CreatedInstance
if TYPE_CHECKING:
from ayon_core.lib import AbstractAttrDef
# Avoid cyclic imports
from .context import CreateContext, UpdateData # noqa: F401
@ -66,7 +68,6 @@ class ProductConvertorPlugin(ABC):
Returns:
logging.Logger: Logger with name of the plugin.
"""
if self._log is None:
self._log = Logger.get_logger(self.__class__.__name__)
return self._log
@ -82,9 +83,8 @@ class ProductConvertorPlugin(ABC):
Returns:
str: Converted identifier unique for all converters in host.
"""
pass
"""
@abstractmethod
def find_instances(self):
@ -94,14 +94,10 @@ class ProductConvertorPlugin(ABC):
convert.
"""
pass
@abstractmethod
def convert(self):
"""Conversion code."""
pass
@property
def create_context(self):
"""Quick access to create context.
@ -109,7 +105,6 @@ class ProductConvertorPlugin(ABC):
Returns:
CreateContext: Context which initialized the plugin.
"""
return self._create_context
@property
@ -122,7 +117,6 @@ class ProductConvertorPlugin(ABC):
Raises:
UnavailableSharedData: When called out of collection phase.
"""
return self._create_context.collection_shared_data
def add_convertor_item(self, label):
@ -131,12 +125,10 @@ class ProductConvertorPlugin(ABC):
Args:
label (str): Label of item which will show in UI.
"""
self._create_context.add_convertor_item(self.identifier, label)
def remove_convertor_item(self):
"""Remove legacy item from create context when conversion finished."""
self._create_context.remove_convertor_item(self.identifier)
@ -155,7 +147,6 @@ class BaseCreator(ABC):
create_context (CreateContext): Context which initialized creator.
headless (bool): Running in headless mode.
"""
# Label shown in UI
label = None
group_label = None
@ -219,7 +210,6 @@ class BaseCreator(ABC):
Returns:
Optional[dict[str, Any]]: Settings values or None.
"""
settings = project_settings.get(category_name)
if not settings:
return None
@ -265,7 +255,6 @@ class BaseCreator(ABC):
Args:
project_settings (dict[str, Any]): Project settings.
"""
settings_category = self.settings_category
if not settings_category:
return
@ -277,18 +266,17 @@ class BaseCreator(ABC):
project_settings, settings_category, settings_name
)
if settings is None:
self.log.debug("No settings found for {}".format(cls_name))
self.log.debug(f"No settings found for {cls_name}")
return
for key, value in settings.items():
# Log out attributes that are not defined on plugin object
# - those may be potential dangerous typos in settings
if not hasattr(self, key):
self.log.debug((
"Applying settings to unknown attribute '{}' on '{}'."
).format(
self.log.debug(
"Applying settings to unknown attribute '%s' on '%s'.",
key, cls_name
))
)
setattr(self, key, value)
def register_callbacks(self):
@ -297,23 +285,39 @@ class BaseCreator(ABC):
Default implementation does nothing. It can be overridden to register
callbacks for creator.
"""
pass
@property
def identifier(self):
"""Identifier of creator (must be unique).
Default implementation returns plugin's product type.
"""
Default implementation returns plugin's product base type,
or falls back to product type if product base type is not set.
return self.product_type
"""
identifier = self.product_base_type
if not identifier:
identifier = self.product_type
return identifier
@property
@abstractmethod
def product_type(self):
"""Family that plugin represents."""
pass
@property
def product_base_type(self) -> Optional[str]:
"""Base product type that plugin represents.
Todo (antirotor): This should be required in future - it
should be made abstract then.
Returns:
Optional[str]: Base product type that plugin represents.
If not set, it is assumed that the creator plugin is obsolete
and does not support product base type.
"""
return None
@property
def project_name(self):
@ -322,7 +326,6 @@ class BaseCreator(ABC):
Returns:
str: Name of a project.
"""
return self.create_context.project_name
@property
@ -332,7 +335,6 @@ class BaseCreator(ABC):
Returns:
Anatomy: Project anatomy object.
"""
return self.create_context.project_anatomy
@property
@ -344,13 +346,14 @@ class BaseCreator(ABC):
Default implementation use attributes in this order:
- 'group_label' -> 'label' -> 'identifier'
Keep in mind that 'identifier' use 'product_type' by default.
Keep in mind that 'identifier' uses 'product_base_type' by default.
Returns:
str: Group label that can be used for grouping of instances in UI.
Group label can be overridden by instance itself.
"""
Group label can be overridden by the instance itself.
"""
if self._cached_group_label is None:
label = self.identifier
if self.group_label:
@ -367,7 +370,6 @@ class BaseCreator(ABC):
Returns:
logging.Logger: Logger with name of the plugin.
"""
if self._log is None:
self._log = Logger.get_logger(self.__class__.__name__)
return self._log
@ -376,7 +378,8 @@ class BaseCreator(ABC):
self,
product_name: str,
data: Dict[str, Any],
product_type: Optional[str] = None
product_type: Optional[str] = None,
product_base_type: Optional[str] = None
) -> CreatedInstance:
"""Create instance and add instance to context.
@ -385,6 +388,8 @@ class BaseCreator(ABC):
data (Dict[str, Any]): Instance data.
product_type (Optional[str]): Product type, object attribute
'product_type' is used if not passed.
product_base_type (Optional[str]): Product base type, object
attribute 'product_base_type' is used if not passed.
Returns:
CreatedInstance: Created instance.
@ -392,11 +397,16 @@ class BaseCreator(ABC):
"""
if product_type is None:
product_type = self.product_type
if not product_base_type and not self.product_base_type:
product_base_type = product_type
instance = CreatedInstance(
product_type,
product_name,
data,
product_type=product_type,
product_name=product_name,
data=data,
creator=self,
product_base_type=product_base_type,
)
self._add_instance_to_context(instance)
return instance
@ -412,7 +422,6 @@ class BaseCreator(ABC):
Args:
instance (CreatedInstance): New created instance.
"""
self.create_context.creator_adds_instance(instance)
def _remove_instance_from_context(self, instance):
@ -425,7 +434,6 @@ class BaseCreator(ABC):
Args:
instance (CreatedInstance): Instance which should be removed.
"""
self.create_context.creator_removed_instance(instance)
@abstractmethod
@ -437,8 +445,6 @@ class BaseCreator(ABC):
implementation
"""
pass
@abstractmethod
def collect_instances(self):
"""Collect existing instances related to this creator plugin.
@ -464,8 +470,6 @@ class BaseCreator(ABC):
```
"""
pass
@abstractmethod
def update_instances(self, update_list):
"""Store changes of existing instances so they can be recollected.
@ -475,8 +479,6 @@ class BaseCreator(ABC):
contain changed instance and it's changes.
"""
pass
@abstractmethod
def remove_instances(self, instances):
"""Method called on instance removal.
@ -489,14 +491,11 @@ class BaseCreator(ABC):
removed.
"""
pass
def get_icon(self):
"""Icon of creator (product type).
Can return path to image file or awesome icon name.
"""
return self.icon
def get_dynamic_data(
@ -512,19 +511,18 @@ class BaseCreator(ABC):
These may be dynamically created based on current context of workfile.
"""
return {}
def get_product_name(
self,
project_name,
folder_entity,
task_entity,
variant,
host_name=None,
instance=None,
project_entity=None,
):
project_name: str,
folder_entity: dict[str, Any],
task_entity: Optional[dict[str, Any]],
variant: str,
host_name: Optional[str] = None,
instance: Optional[CreatedInstance] = None,
project_entity: Optional[dict[str, Any]] = None,
) -> str:
"""Return product name for passed context.
Method is also called on product name update. In that case origin
@ -563,8 +561,9 @@ class BaseCreator(ABC):
project_name,
folder_entity=folder_entity,
task_entity=task_entity,
host_name=host_name,
product_base_type=self.product_base_type,
product_type=self.product_type,
host_name=host_name,
variant=variant,
dynamic_data=dynamic_data,
project_settings=self.project_settings,
@ -578,15 +577,15 @@ class BaseCreator(ABC):
and values are stored to metadata for future usage and for publishing
purposes.
NOTE:
Convert method should be implemented which should care about updating
keys/values when plugin attributes change.
Note:
Convert method should be implemented which should care about
updating keys/values when plugin attributes change.
Returns:
list[AbstractAttrDef]: Attribute definitions that can be tweaked
for created instance.
"""
"""
return self.instance_attr_defs
def get_attr_defs_for_instance(self, instance):
@ -609,12 +608,10 @@ class BaseCreator(ABC):
Raises:
UnavailableSharedData: When called out of collection phase.
"""
return self.create_context.collection_shared_data
def set_instance_thumbnail_path(self, instance_id, thumbnail_path=None):
"""Set path to thumbnail for instance."""
self.create_context.thumbnail_paths_by_instance_id[instance_id] = (
thumbnail_path
)
@ -635,7 +632,6 @@ class BaseCreator(ABC):
Returns:
dict[str, int]: Next versions by instance id.
"""
return get_next_versions_for_instances(
self.create_context.project_name, instances
)
@ -702,7 +698,6 @@ class Creator(BaseCreator):
int: Order in which is creator shown (less == earlier). By default
is using Creator's 'order' or processing.
"""
return self.order
@abstractmethod
@ -717,11 +712,9 @@ class Creator(BaseCreator):
pre_create_data(dict): Data based on pre creation attributes.
Those may affect how creator works.
"""
# instance = CreatedInstance(
# self.product_type, product_name, instance_data
# )
pass
def get_description(self):
"""Short description of product type and plugin.
@ -729,7 +722,6 @@ class Creator(BaseCreator):
Returns:
str: Short description of product type.
"""
return self.description
def get_detail_description(self):
@ -740,7 +732,6 @@ class Creator(BaseCreator):
Returns:
str: Detailed description of product type for artist.
"""
return self.detailed_description
def get_default_variants(self):
@ -754,7 +745,6 @@ class Creator(BaseCreator):
Returns:
list[str]: Whisper variants for user input.
"""
return copy.deepcopy(self.default_variants)
def get_default_variant(self, only_explicit=False):
@ -774,7 +764,6 @@ class Creator(BaseCreator):
Returns:
str: Variant value.
"""
if only_explicit or self._default_variant:
return self._default_variant
@ -795,7 +784,6 @@ class Creator(BaseCreator):
Returns:
str: Variant value.
"""
return self.get_default_variant()
def _set_default_variant_wrap(self, variant):
@ -807,7 +795,6 @@ class Creator(BaseCreator):
Args:
variant (str): New default variant value.
"""
self._default_variant = variant
default_variant = property(
@ -957,7 +944,6 @@ class AutoCreator(BaseCreator):
def remove_instances(self, instances):
"""Skip removal."""
pass
def discover_creator_plugins(*args, **kwargs):
@ -1015,7 +1001,6 @@ def cache_and_get_instances(creator, shared_key, list_instances_func):
dict[str, dict[str, Any]]: Cached instances by creator identifier from
result of passed function.
"""
if shared_key not in creator.collection_shared_data:
value = collections.defaultdict(list)
for instance in list_instances_func():

View file

@ -1,8 +1,10 @@
"""Functions for handling product names."""
from __future__ import annotations
import warnings
from functools import wraps
from typing import Optional, Any, overload
from typing import Any, Optional, Union, overload
from warnings import warn
import ayon_api
from ayon_core.lib import (
@ -22,14 +24,15 @@ log = Logger.get_logger(__name__)
def get_product_name_template(
project_name,
product_type,
task_name,
task_type,
host_name,
default_template=None,
project_settings=None
):
project_name: str,
product_type: str,
task_name: Optional[str],
task_type: Optional[str],
host_name: str,
default_template: Optional[str] = None,
project_settings: Optional[dict[str, Any]] = None,
product_base_type: Optional[str] = None
) -> str:
"""Get product name template based on passed context.
Args:
@ -37,26 +40,32 @@ def get_product_name_template(
product_type (str): Product type for which the product name is
calculated.
host_name (str): Name of host in which the product name is calculated.
task_name (str): Name of task in which context the product is created.
task_type (str): Type of task in which context the product is created.
task_name (Optional[str]): Name of task in which context the
product is created.
task_type (Optional[str]): Type of task in which context the
product is created.
default_template (Optional[str]): Default template which is used if
settings won't find any matching possibility. Constant
'DEFAULT_PRODUCT_TEMPLATE' is used if not defined.
project_settings (Optional[dict[str, Any]]): Prepared settings for
project. Settings are queried if not passed.
"""
product_base_type (Optional[str]): Base type of product.
Returns:
str: Product name template.
"""
if project_settings is None:
project_settings = get_project_settings(project_name)
tools_settings = project_settings["core"]["tools"]
profiles = tools_settings["creator"]["product_name_profiles"]
filtering_criteria = {
"product_types": product_type,
"hosts": host_name,
"tasks": task_name,
"task_types": task_type
"host_names": host_name,
"task_names": task_name,
"task_types": task_type,
"product_base_types": product_base_type,
}
matching_profile = filter_profiles(profiles, filtering_criteria)
template = None
if matching_profile:
@ -92,6 +101,7 @@ def _get_product_name_old(
project_settings: Optional[dict[str, Any]] = None,
product_type_filter: Optional[str] = None,
project_entity: Optional[dict[str, Any]] = None,
product_base_type: Optional[str] = None,
) -> TemplateResult:
warnings.warn(
"Used deprecated 'task_name' and 'task_type' arguments."
@ -103,13 +113,14 @@ def _get_product_name_old(
return StringTemplate("").format({})
template = get_product_name_template(
project_name,
product_type_filter or product_type,
task_name,
task_type,
host_name,
project_name=project_name,
product_type=product_type_filter or product_type,
task_name=task_name,
task_type=task_type,
host_name=host_name,
default_template=default_template,
project_settings=project_settings
project_settings=project_settings,
product_base_type=product_base_type,
)
template_low = template.lower()
@ -140,12 +151,22 @@ def _get_product_name_old(
task_short = task_types_by_name.get(task_type, {}).get("shortName")
task_value["short"] = task_short
fill_pairs = {
if not product_base_type and "{product[basetype]}" in template.lower():
warn(
"You have Product base type in product name template, "
"but it is not provided by the creator, please update your "
"creation code to include it. It will be required in "
"the future.",
DeprecationWarning,
stacklevel=2)
fill_pairs: dict[str, Union[str, dict[str, str]]] = {
"variant": variant,
"family": product_type,
"task": task_value,
"product": {
"type": product_type
"type": product_type,
"basetype": product_base_type or product_type,
}
}
@ -160,10 +181,11 @@ def _get_product_name_old(
data=prepare_template_data(fill_pairs)
)
except KeyError as exp:
raise TemplateFillError(
"Value for {} key is missing in template '{}'."
" Available values are {}".format(str(exp), template, fill_pairs)
msg = (
f"Value for {exp} key is missing in template '{template}'."
f" Available values are {fill_pairs}"
)
raise TemplateFillError(msg) from exp
def _backwards_compatibility_product_name(func):
@ -198,9 +220,9 @@ def _backwards_compatibility_product_name(func):
if "folder_entity" in kwargs or "task_entity" in kwargs:
return func(*args, **kwargs)
# Using more than 6 positional arguments is not allowed
# Using more than 7 positional arguments is not allowed
# in the new function
if len(args) > 6:
if len(args) > 7:
return _get_product_name_old(*args, **kwargs)
if len(args) > 1:
@ -332,15 +354,16 @@ def get_product_name(
project_name: str,
folder_entity: dict[str, Any],
task_entity: Optional[dict[str, Any]],
host_name: str,
product_base_type: str,
product_type: str,
host_name: str,
variant: str,
*,
default_template: Optional[str] = None,
dynamic_data: Optional[dict[str, Any]] = None,
project_settings: Optional[dict[str, Any]] = None,
product_type_filter: Optional[str] = None,
project_entity: Optional[dict[str, Any]] = None,
default_template: Optional[str] = None,
product_base_type_filter: Optional[str] = None,
) -> TemplateResult:
"""Calculate product name based on passed context and AYON settings.
@ -357,20 +380,21 @@ def get_product_name(
folder_entity (Optional[dict[str, Any]]): Folder entity.
task_entity (Optional[dict[str, Any]]): Task entity.
host_name (str): Host name.
product_base_type (str): Product base type.
product_type (str): Product type.
variant (str): In most of the cases it is user input during creation.
default_template (Optional[str]): Default template if any profile does
not match passed context. Constant 'DEFAULT_PRODUCT_TEMPLATE'
is used if is not passed.
dynamic_data (Optional[dict[str, Any]]): Dynamic data specific for
a creator which creates instance.
project_settings (Optional[dict[str, Any]]): Prepared settings
for project. Settings are queried if not passed.
product_type_filter (Optional[str]): Use different product type for
product template filtering. Value of `product_type` is used when
not passed.
project_entity (Optional[dict[str, Any]]): Project entity used when
task short name is required by template.
default_template (Optional[str]): Default template if any profile does
not match passed context. Constant 'DEFAULT_PRODUCT_TEMPLATE'
is used if is not passed.
product_base_type_filter (Optional[str]): Use different product base
type for product template filtering. Value of
`product_base_type_filter` is used when not passed.
Returns:
TemplateResult: Product name.
@ -390,13 +414,14 @@ def get_product_name(
task_type = task_entity["taskType"]
template = get_product_name_template(
project_name,
product_type_filter or product_type,
task_name,
task_type,
host_name,
project_name=project_name,
product_base_type=product_base_type_filter or product_base_type,
product_type=product_type,
task_name=task_name,
task_type=task_type,
host_name=host_name,
default_template=default_template,
project_settings=project_settings
project_settings=project_settings,
)
template_low = template.lower()
@ -421,8 +446,8 @@ def get_product_name(
if project_entity is None:
project_entity = ayon_api.get_project(project_name)
task_types_by_name = {
task["name"]: task for task in
project_entity["taskTypes"]
task["name"]: task
for task in project_entity["taskTypes"]
}
task_short = task_types_by_name.get(task_type, {}).get("shortName")
task_value["short"] = task_short
@ -433,7 +458,8 @@ def get_product_name(
"family": product_type,
"task": task_value,
"product": {
"type": product_type
"type": product_type,
"basetype": product_base_type,
}
}
if folder_entity:
@ -453,7 +479,8 @@ def get_product_name(
data=prepare_template_data(fill_pairs)
)
except KeyError as exp:
raise TemplateFillError(
msg = (
f"Value for {exp} key is missing in template '{template}'."
f" Available values are {fill_pairs}"
)
raise TemplateFillError(msg)

View file

@ -11,6 +11,8 @@ from ayon_core.lib.attribute_definitions import (
serialize_attr_defs,
deserialize_attr_defs,
)
from ayon_core.pipeline import (
AYON_INSTANCE_ID,
AVALON_INSTANCE_ID,
@ -480,6 +482,10 @@ class CreatedInstance:
data (Dict[str, Any]): Data used for filling product name or override
data from already existing instance.
creator (BaseCreator): Creator responsible for instance.
product_base_type (Optional[str]): Product base type that will be
created. If not provided then product base type is taken from
creator plugin. If creator does not have product base type then
deprecation warning is raised.
"""
# Keys that can't be changed or removed from data after loading using
@ -490,6 +496,7 @@ class CreatedInstance:
"id",
"instance_id",
"productType",
"productBaseType",
"creator_identifier",
"creator_attributes",
"publish_attributes"
@ -509,7 +516,13 @@ class CreatedInstance:
data: Dict[str, Any],
creator: "BaseCreator",
transient_data: Optional[Dict[str, Any]] = None,
product_base_type: Optional[str] = None
):
"""Initialize CreatedInstance."""
# fallback to product type for backward compatibility
if not product_base_type:
product_base_type = creator.product_base_type or product_type
self._creator = creator
creator_identifier = creator.identifier
group_label = creator.get_group_label()
@ -562,6 +575,9 @@ class CreatedInstance:
self._data["id"] = item_id
self._data["productType"] = product_type
self._data["productName"] = product_name
self._data["productBaseType"] = product_base_type
self._data["active"] = data.get("active", True)
self._data["creator_identifier"] = creator_identifier