Skip to content

tools

from_published_scene(instance, replace_in_path=True)

Switch work scene for published scene.

If rendering/exporting from published scenes is enabled, this will replace paths from working scene to published scene.

Parameters:

Name Type Description Default
instance Instance

Instance data to process.

required
replace_in_path bool

if True, it will try to find old scene name in path of expected files and replace it with name of published scene.

True

Returns:

Name Type Description
str

Published scene path.

None

if no published scene is found.

Note

Published scene path is actually determined from project Anatomy as at the time this plugin is running the scene can be still un-published.

Source code in client/ayon_core/pipeline/farm/tools.py
 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
 61
 62
 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
def from_published_scene(instance, replace_in_path=True):
    """Switch work scene for published scene.

    If rendering/exporting from published scenes is enabled, this will
    replace paths from working scene to published scene.

    Args:
        instance (pyblish.api.Instance): Instance data to process.
        replace_in_path (bool): if True, it will try to find
            old scene name in path of expected files and replace it
            with name of published scene.

    Returns:
        str: Published scene path.
        None: if no published scene is found.

    Note:
        Published scene path is actually determined from project Anatomy
        as at the time this plugin is running the scene can be still
        un-published.

    """
    workfile_instance = get_published_workfile_instance(instance.context)
    if workfile_instance is None:
        return

    # determine published path from Anatomy.
    template_data = workfile_instance.data.get("anatomyData")
    rep = workfile_instance.data["representations"][0]
    template_data["representation"] = rep.get("name")
    template_data["ext"] = rep.get("ext")
    template_data["comment"] = None

    anatomy = instance.context.data['anatomy']
    template_obj = anatomy.get_template_item("publish", "default", "path")
    template_filled = template_obj.format_strict(template_data)
    file_path = os.path.normpath(template_filled)

    if not os.path.exists(file_path):
        raise

    if not replace_in_path:
        return file_path

    # now we need to switch scene in expected files
    # because <scene> token will now point to published
    # scene file and that might differ from current one
    def _clean_name(path):
        return os.path.splitext(os.path.basename(path))[0]

    new_scene = _clean_name(file_path)
    orig_scene = _clean_name(instance.context.data["currentFile"])
    expected_files = instance.data.get("expectedFiles")

    if isinstance(expected_files[0], dict):
        # we have aovs and we need to iterate over them
        new_exp = {}
        for aov, files in expected_files[0].items():
            replaced_files = []
            for f in files:
                replaced_files.append(
                    str(f).replace(orig_scene, new_scene)
                )
            new_exp[aov] = replaced_files
        # [] might be too much here, TODO
        instance.data["expectedFiles"] = [new_exp]
    else:
        new_exp = []
        for f in expected_files:
            new_exp.append(
                str(f).replace(orig_scene, new_scene)
            )
        instance.data["expectedFiles"] = new_exp

    metadata_folder = instance.data.get("publishRenderMetadataFolder")
    if metadata_folder:
        metadata_folder = metadata_folder.replace(orig_scene,
                                                  new_scene)
        instance.data["publishRenderMetadataFolder"] = metadata_folder

    return file_path

get_published_workfile_instance(context)

Find workfile instance in context

Source code in client/ayon_core/pipeline/farm/tools.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
def get_published_workfile_instance(context):
    """Find workfile instance in context"""
    for i in context:
        is_workfile = (
            i.data["productType"] == "workfile"
            or "workfile" in i.data.get("families", [])

        )
        if not is_workfile:
            continue

        # test if there is instance of workfile waiting
        # to be published.
        if i.data["publish"] is not True:
            continue

        return i