Skip to content

load_batch

LoadBatchgroup

Bases: LoaderPlugin

Source code in client/ayon_flame/plugins/load/load_batch.py
 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class LoadBatchgroup(LoaderPlugin):
    product_types = {"workfile"}
    representations = {"*"}
    extensions = ("json",)

    label = "Load batch"
    order = -10
    icon = "code-fork"
    color = "orange"

    def load(
            self,
            context: Dict,
            name: Optional[str] = None,
            namespace: Optional[str] = None,
            options: Optional[Dict] = None,
    ):
        """Load all published versions as native Flame batch iterations.

        Each AYON version is loaded as an iteration inside a single batch
        group.
        If a batch group with the same name already exists in the workspace,
        a unique name is generated automatically (e.g. "MyBatch (2)").
        """
        import flame

        project_name = context["project"]["name"]
        product_id = context["version"]["productId"]
        requested_version_entity = context["version"]
        batch_name = (
            requested_version_entity.get("data", {}).get("batch_name")
            or context["representation"]["context"].get("asset")
        )
        unique_batch_name = _unique_batch_name(batch_name)
        if unique_batch_name != batch_name:
            self.log.warning(
                f"Batch group '{batch_name}' already exists. "
                f"Loading as '{unique_batch_name}'."
            )
            flame.messages.show_in_dialog(
                "Existing Batch Group",
                f"A batch group with the name '{batch_name}' already exists. "
                f"Loading as '{unique_batch_name}'.",
                "warning",
                ["OK"],
            )
        batch_name = unique_batch_name
        flame.batch.create_batch_group(batch_name)

        # Collect all versions sorted oldest → newest.
        all_versions = sorted(
            ayon_api.get_versions(project_name, product_ids=[product_id]),
            key=lambda v: v["version"],
        )

        # Collect matching representations per version.
        repres_by_version_id = {
            r["versionId"]: r
            for r in ayon_api.get_representations(
                project_name,
                representation_names={context["representation"]["name"]},
                version_ids=[v["id"] for v in all_versions],
            )
        }

        if not repres_by_version_id:
            raise RuntimeError(
                f"No representations found for product '{product_id}' "
                f"in project '{project_name}'."
            )

        current_iteration = None
        iterations = 0

        for version in all_versions:
            repre = repres_by_version_id.get(version["id"])
            if not repre:
                continue

            iterations += 1
            version_context = deepcopy(context)
            version_context["version"] = version
            version_context["representation"] = repre

            filepath = self.filepath_from_context(version_context)
            batch_utils.load_batch_from_consolidated_json(
                filepath,
                name=batch_name,
            )
            flame.batch.iterate()

            if requested_version_entity["id"] == version["id"]:
                current_iteration = flame.batch.batch_iterations[-1]

        self.log.debug(
            f"Loaded batch group with {iterations} versions as iterations."
        )

        # Set requested version as current iteration.
        if current_iteration:
            flame.batch.replace_setup(current_iteration)
            self.log.debug(
                f'Set {requested_version_entity["version"]} '
                f"as current batch iteration."
            )

    def update(self, container, context):
        raise NotImplementedError(
            "Version management rely on Flame "
            "native batch iteration implementation."
        )

    def remove(self, container):
        raise NotImplementedError(
            "Version management rely on Flame "
            "native batch iteration implementation."
        )

load(context, name=None, namespace=None, options=None)

Load all published versions as native Flame batch iterations.

Each AYON version is loaded as an iteration inside a single batch group. If a batch group with the same name already exists in the workspace, a unique name is generated automatically (e.g. "MyBatch (2)").

Source code in client/ayon_flame/plugins/load/load_batch.py
 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def load(
        self,
        context: Dict,
        name: Optional[str] = None,
        namespace: Optional[str] = None,
        options: Optional[Dict] = None,
):
    """Load all published versions as native Flame batch iterations.

    Each AYON version is loaded as an iteration inside a single batch
    group.
    If a batch group with the same name already exists in the workspace,
    a unique name is generated automatically (e.g. "MyBatch (2)").
    """
    import flame

    project_name = context["project"]["name"]
    product_id = context["version"]["productId"]
    requested_version_entity = context["version"]
    batch_name = (
        requested_version_entity.get("data", {}).get("batch_name")
        or context["representation"]["context"].get("asset")
    )
    unique_batch_name = _unique_batch_name(batch_name)
    if unique_batch_name != batch_name:
        self.log.warning(
            f"Batch group '{batch_name}' already exists. "
            f"Loading as '{unique_batch_name}'."
        )
        flame.messages.show_in_dialog(
            "Existing Batch Group",
            f"A batch group with the name '{batch_name}' already exists. "
            f"Loading as '{unique_batch_name}'.",
            "warning",
            ["OK"],
        )
    batch_name = unique_batch_name
    flame.batch.create_batch_group(batch_name)

    # Collect all versions sorted oldest → newest.
    all_versions = sorted(
        ayon_api.get_versions(project_name, product_ids=[product_id]),
        key=lambda v: v["version"],
    )

    # Collect matching representations per version.
    repres_by_version_id = {
        r["versionId"]: r
        for r in ayon_api.get_representations(
            project_name,
            representation_names={context["representation"]["name"]},
            version_ids=[v["id"] for v in all_versions],
        )
    }

    if not repres_by_version_id:
        raise RuntimeError(
            f"No representations found for product '{product_id}' "
            f"in project '{project_name}'."
        )

    current_iteration = None
    iterations = 0

    for version in all_versions:
        repre = repres_by_version_id.get(version["id"])
        if not repre:
            continue

        iterations += 1
        version_context = deepcopy(context)
        version_context["version"] = version
        version_context["representation"] = repre

        filepath = self.filepath_from_context(version_context)
        batch_utils.load_batch_from_consolidated_json(
            filepath,
            name=batch_name,
        )
        flame.batch.iterate()

        if requested_version_entity["id"] == version["id"]:
            current_iteration = flame.batch.batch_iterations[-1]

    self.log.debug(
        f"Loaded batch group with {iterations} versions as iterations."
    )

    # Set requested version as current iteration.
    if current_iteration:
        flame.batch.replace_setup(current_iteration)
        self.log.debug(
            f'Set {requested_version_entity["version"]} '
            f"as current batch iteration."
        )