Useful AYON utility functions

Hello,

As I take a deeper look at AYON code base, I found out that there are many useful utility functions such as

get_folder_by_path and get_task_by_name from ayon_api

However, there might be situation where we need a bit of customization that would help with plugins development. For example, I used the following code to find the episode of a given task:

def getEpisodeFolderName(ayon_project_name, ayon_folder_path):
    """A recursive function to get the episode name from the folder path.

    Each recursive call will either return the episode name
    or run the function again with the parent folder path.

    Args:
        ayon_project_name (str): the name of the project.
        ayon_folder_path (str): the relative ayon folder path.

    Returns:
        str: the name of the episode folder
    """

    folder = get_folder_by_path(ayon_project_name, ayon_folder_path)

    folder_type = folder.get("folderType", None)
    if folder_type.lower() == "episode":
        return folder.get("name", None)
    
    if ayon_folder_path == "":
        return None
    
    parent_folder = os.path.dirname(ayon_folder_path)
    return getEpisodeFolderName(ayon_project_name, parent_folder)

As there will be many utility functions that would be extremely helpful during development, it would be great if we can have a place where we share quick snippets like this to make our lives easier.

Thus, I’m creating this post as an initial place for AYON enthusiasts and developers to share their utility snippets.

Resource:
AYON Python API Doc: Welcome to AYON Python API documentation! — ayon-python-api 1.0.9 documentation

2 Likes

Hello,
Thanks for your initiative.
Currently, we don’t have a Hub for community snippets.


At present there are scattered snippets around the forums and there are also some on GH, for instance:

1 Like

Hello guys,

Is there a function that will get the AYON entity given the absolute path.

For example, given

absolute_path = r"K:\40_rnd\ayon_root\Ayon_Dev_aydev2434\01_assets\main\pacman

a function that convert this to the AYON folder entity.

I think ServerAPI.get_folder_by_path() is close. However, it requires converting the path to relative path (i.e. 01_assets/main/pacman). And it might requires some knowledge on the Anatomy of the project also.

Regards,
Tu

Unfortunately, no.

Anatomy templates are highly dynamic, allows dynamic depth of folders and allows optional keys so ‘parsing’ the keys from a filepath is not trivial.

The other option would be to not parse it fully - but instead parse e.g. a rootless path e.g. {root}/Ayon_Dev_aydev2434/01_assets/main/pacman and ‘search’ against some stored database value lookup.

However, there’s no such lookup for each directory in a project in the database. You could ‘mock’ an index like that by formatting all paths of all entities and building your own lookup.

So at best what you’re left with is some ‘optimal guessing’ based on some assumptions about how your paths are structured and if absolutely needed - then proceed to parse it for your own needs.

Wow thank you @BigRoy for the very quick response <3.