From 52c03faf4b31594e67a4da1bd452f8f3fc16fbd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Samohel?= Date: Mon, 12 May 2025 17:51:27 +0200 Subject: [PATCH] :sparkles: add function to get traits from addons --- client/ayon_core/pipeline/traits/utils.py | 46 +++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/client/ayon_core/pipeline/traits/utils.py b/client/ayon_core/pipeline/traits/utils.py index ef22122124..7d579a76ce 100644 --- a/client/ayon_core/pipeline/traits/utils.py +++ b/client/ayon_core/pipeline/traits/utils.py @@ -1,18 +1,21 @@ """Utility functions for traits.""" from __future__ import annotations -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional +import pyblish.api from clique import assemble +from ayon_core.addon import AddonsManager, ITraits from ayon_core.pipeline.traits.temporal import FrameRanged if TYPE_CHECKING: from pathlib import Path + from ayon_core.pipeline.traits.trait import TraitBase def get_sequence_from_files(paths: list[Path]) -> FrameRanged: - """Get original frame range from files. + """Get the original frame range from files. Note that this cannot guess frame rate, so it's set to 25. This will also fail on paths that cannot be assembled into @@ -42,10 +45,47 @@ def get_sequence_from_files(paths: list[Path]) -> FrameRanged: first_frame = sorted_frames[0] # Get last frame for padding last_frame = sorted_frames[-1] - # Use padding from collection of length of last frame as string + # Use padding from a collection of the last frame lengths as string # padding = max(col.padding, len(str(last_frame))) return FrameRanged( frame_start=first_frame, frame_end=last_frame, frames_per_second="25.0" ) + + +def get_available_traits( + addons_manager: Optional[AddonsManager] = None +) -> Optional[list[TraitBase]]: + """Get available traits from active addons. + + Args: + addons_manager (Optional[AddonsManager]): Addons manager instance. + If not provided, a new one will be created. Within pyblish + plugins, you can use an already collected instance of + AddonsManager from context `context.data["ayonAddonsManager"]`. + + Returns: + list[TraitBase]: List of available traits. + + """ + if addons_manager is None: + # Create a new instance of AddonsManager + addons_manager = AddonsManager() + + # Get active addons + enabled_addons = addons_manager.get_enabled_addons() + traits = [] + for addon in enabled_addons: + if not issubclass(type(addon), ITraits): + # Skip addons not providing traits + continue + # Get traits from addon + addon_traits = addon.get_addon_traits() + if addon_traits: + # Add traits to a list + for trait in addon_traits: + if trait not in traits: + traits.append(trait) + + return traits