Skip to content

entity_uri

construct_ayon_entity_uri(project_name, folder_path, product, version, representation_name)

Construct AYON entity URI from its components

The folder path may be provided with or without leading slash.

Returns:

Name Type Description
str str

AYON Entity URI to query entity path.

Source code in client/ayon_core/pipeline/entity_uri.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def construct_ayon_entity_uri(
        project_name: str,
        folder_path: str,
        product: str,
        version: Union[int, str],
        representation_name: str
) -> str:
    """Construct AYON entity URI from its components

    The folder path may be provided with or without leading slash.

    Returns:
        str: AYON Entity URI to query entity path.
    """
    if isinstance(version, str):
        if version not in {"latest", "latestDone", "hero"}:
            # Allow digits as string, e.g. coming from settings
            try:
                version = int(version)
            except ValueError:
                raise ValueError(
                    "Version must either be integer, 'latest', 'latestDone'"
                    f" or 'hero'. Got: '{version}'"
                ) from None
    elif not isinstance(version, int):
        raise TypeError(
            "Version must either be integer, 'latest', 'latestDone' or "
            f"'hero'. Got: {version} ({type(version)})"
        )

    if isinstance(version, int) and version < 0:
        version = "hero"

    return (
        "ayon://{project}/{folder_path}?product={product}&version={version}"
        "&representation={representation}".format(
            project=project_name,
            folder_path=folder_path.strip("/"),
            product=product,
            version=version,
            representation=representation_name
        )
    )

parse_ayon_entity_uri(uri)

Parse AYON entity URI into individual components.

URI specification

ayon+entity://{project}/{folder}?product={product} &version={version} &representation={representation}

URI example: ayon+entity://test/hero?product=modelMain&version=2&representation=usd

However - if the netloc is ayon:// it will by default also resolve as ayon+entity:// on AYON server, thus we need to support both. The shorter ayon:// is preferred for user readability.

Example:

parse_ayon_entity_uri( "ayon://test/char/villain?product=modelMain&version=2&representation=usd" ) {'project': 'test', 'folderPath': '/char/villain', 'product': 'modelMain', 'version': 1, 'representation': 'usd'} parse_ayon_entity_uri( "ayon+entity://project/folder?product=renderMain&version=3&representation=exr" ) {'project': 'project', 'folderPath': '/folder', 'product': 'renderMain', 'version': 3, 'representation': 'exr'}

Returns:

Type Description
Optional[dict]

dict[str, Union[str, int]]: The individual key with their values as found in the ayon entity URI.

Source code in client/ayon_core/pipeline/entity_uri.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def parse_ayon_entity_uri(uri: str) -> Optional[dict]:
    """Parse AYON entity URI into individual components.

    URI specification:
        ayon+entity://{project}/{folder}?product={product}
            &version={version}
            &representation={representation}
    URI example:
        ayon+entity://test/hero?product=modelMain&version=2&representation=usd

    However - if the netloc is `ayon://` it will by default also resolve as
    `ayon+entity://` on AYON server, thus we need to support both. The shorter
    `ayon://` is preferred for user readability.

    Example:
    >>> parse_ayon_entity_uri(
    >>>     "ayon://test/char/villain?product=modelMain&version=2&representation=usd"
    >>> )
    {'project': 'test', 'folderPath': '/char/villain',
     'product': 'modelMain', 'version': 1,
     'representation': 'usd'}
    >>> parse_ayon_entity_uri(
    >>>     "ayon+entity://project/folder?product=renderMain&version=3&representation=exr"
    >>> )
    {'project': 'project', 'folderPath': '/folder',
     'product': 'renderMain', 'version': 3,
     'representation': 'exr'}

    Returns:
        dict[str, Union[str, int]]: The individual key with their values as
            found in the ayon entity URI.

    """  # noqa: E501

    if not (uri.startswith("ayon+entity://") or uri.startswith("ayon://")):
        return {}

    parsed = urlparse(uri)
    if parsed.scheme not in {"ayon+entity", "ayon"}:
        return {}

    result = {
        "project": parsed.netloc,
        "folderPath": "/" + parsed.path.strip("/")
    }
    query = parse_qs(parsed.query)
    for key in ["product", "version", "representation"]:
        if key in query:
            result[key] = query[key][0]

    # Convert version to integer if it is a digit
    version = result.get("version")
    if version is not None and version.isdigit():
        result["version"] = int(version)

    return result