Skip to content

action_create_folders

CreateFolders

Bases: LocalAction

Source code in client/ayon_ftrack/event_handlers_user/action_create_folders.py
 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
 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
class CreateFolders(LocalAction):
    identifier = "ayon.create.folders"
    label = "Create Folders"
    icon = get_ftrack_icon_url("CreateFolders.svg")

    def discover(self, session, entities, event):
        for entity_item in event["data"]["selection"]:
            if entity_item.get("entityType").lower() in ("task", "show"):
                return True
        return False

    def interface(self, session, entities, event):
        if event["data"].get("values", {}):
            return

        with_interface = False
        for entity in entities:
            if entity.entity_type.lower() != "task":
                with_interface = True
                break

        if "values" not in event["data"]:
            event["data"]["values"] = {}

        event["data"]["values"]["with_interface"] = with_interface
        if not with_interface:
            return

        title = "Create folders"

        entity_name = entity["name"]
        msg = (
            "<h2>Do you want create folders also"
            " for all children of your selection?</h2>"
        )
        if entity.entity_type.lower() == "project":
            entity_name = entity["full_name"]
            msg = msg.replace(" also", "")
            msg += "<h3>(Project root won't be created if not checked)</h3>"
        items = [
            {
                "type": "label",
                "value": msg.format(entity_name)
            },
            {
                "type": "label",
                "value": "With all chilren entities"
            },
            {
                "name": "children_included",
                "type": "boolean",
                "value": False
            },
            {
                "type": "hidden",
                "name": "with_interface",
                "value": with_interface
            }
        ]

        return {
            "items": items,
            "title": title
        }

    def launch(self, session, entities, event):
        if "values" not in event["data"]:
            return

        with_interface = event["data"]["values"]["with_interface"]
        with_childrens = True
        if with_interface:
            with_childrens = event["data"]["values"]["children_included"]

        filtered_entities = []
        for entity in entities:
            low_context_type = entity["context_type"].lower()
            if low_context_type in ("task", "show"):
                if not with_childrens and low_context_type == "show":
                    continue
                filtered_entities.append(entity)

        if not filtered_entities:
            return {
                "success": True,
                "message": "Nothing was created"
            }

        project_entity = self.get_project_from_entity(filtered_entities[0])

        project_name = project_entity["full_name"]
        ayon_project = ayon_api.get_project(project_name)
        if not ayon_project:
            return {
                "success": False,
                "message": f"Project '{project_name}' was not found in AYON.",
            }

        project_code = project_entity["name"]

        task_entities, other_entities = self.get_all_entities(
            session, entities
        )
        hierarchy = self.get_entities_hierarchy(
            session, task_entities, other_entities
        )
        task_types = session.query("select id, name from Type").all()
        task_type_names_by_id = {
            task_type["id"]: task_type["name"]
            for task_type in task_types
        }

        anatomy = Anatomy(project_name, project_entity=ayon_project)

        work_template = anatomy.get_template_item(
            "work", "default", "directory"
        )
        publish_template = anatomy.get_template_item(
            "publish", "default", "directory"
        )

        project_data = {
            "project": {
                "name": project_name,
                "code": project_code
            }
        }

        collected_paths = []
        for item in hierarchy:
            parent_entity, task_entities = item

            parent_data = copy.deepcopy(project_data)

            parents = parent_entity["link"][1:-1]
            hierarchy_names = [p["name"] for p in parents]
            hierarchy = "/".join(hierarchy_names)

            if hierarchy_names:
                parent_name = hierarchy_names[-1]
            else:
                parent_name = project_name

            parent_data.update({
                "asset": parent_entity["name"],
                "hierarchy": hierarchy,
                "parent": parent_name,
                "folder": {
                    "name": parent_entity["name"]
                }
            })

            if not task_entities:
                # create path for entity
                collected_paths.append(self.compute_template(
                    parent_data, work_template
                ))
                collected_paths.append(self.compute_template(
                    parent_data, publish_template
                ))
                continue

            for task_entity in task_entities:
                task_type_id = task_entity["type_id"]
                task_type_name = task_type_names_by_id[task_type_id]
                task_data = copy.deepcopy(parent_data)
                task_data["task"] = {
                    "name": task_entity["name"],
                    "type": task_type_name
                }

                # Template wok
                collected_paths.append(self.compute_template(
                    task_data, work_template
                ))

                # Template publish
                collected_paths.append(self.compute_template(
                    task_data, publish_template
                ))

        if len(collected_paths) == 0:
            return {
                "success": True,
                "message": "No project folders to create."
            }

        self.log.info("Creating folders:")

        for path in set(collected_paths):
            self.log.info(path)
            if not os.path.exists(path):
                os.makedirs(path)

        return {
            "success": True,
            "message": "Successfully created project folders."
        }

    def get_all_entities(self, session, entities):
        """

        Args:
            session (ftrack_api.session.Session): ftrack session.
            entities (list[ftrack_api.entity.base.Entity]): List of entities.

        Returns:
            tuple[list, list]: Tuple where first item is list of task entities
                and second item is list of entities that are not task
                entities. All are entities that were passed in and
                their children.
        """

        task_entities = []
        other_entities = []

        query_queue = collections.deque()
        query_queue.append(entities)
        while query_queue:
            entities = query_queue.popleft()
            if not entities:
                continue

            no_task_entities = []
            for entity in entities:
                if entity.entity_type.lower() == "task":
                    task_entities.append(entity)
                else:
                    no_task_entities.append(entity)

            if not no_task_entities:
                continue

            other_entities.extend(no_task_entities)

            no_task_entity_ids = {entity["id"] for entity in no_task_entities}
            next_entities = session.query(
                (
                    "select id, parent_id"
                    " from TypedContext where parent_id in ({})"
                ).format(self.join_query_keys(no_task_entity_ids))
            ).all()
            query_queue.append(next_entities)
        return task_entities, other_entities

    def get_entities_hierarchy(self, session, task_entities, other_entities):
        """

        Args:
            session (ftrack_api.session.Session): ftrack session.
            task_entities (list[ftrack_api.entity.base.Entity]): List of task
                entities.
            other_entities (list[ftrack_api.entity.base.Entity]): List of
                entities that are not task entities.

        Returns:
            list[tuple[ftrack_api.entity.base.Entity, list]]: List of tuples
                where first item is parent entity and second item is list of
                task entities that are children of parent entity.
        """

        output = []
        task_entity_ids = {entity["id"] for entity in task_entities}
        if not task_entity_ids:
            return output

        full_task_entities = session.query(
            (
                "select id, name, type_id, parent_id"
                " from TypedContext where id in ({})"
            ).format(self.join_query_keys(task_entity_ids))
        ).all()
        task_entities_by_parent_id = collections.defaultdict(list)
        for entity in full_task_entities:
            parent_id = entity["parent_id"]
            task_entities_by_parent_id[parent_id].append(entity)

        if not task_entities_by_parent_id:
            return output

        parent_ids = set(task_entities_by_parent_id.keys())

        other_entities_by_id = {
            entity["id"]: entity
            for entity in other_entities
        }
        parent_ids -= set(other_entities_by_id.keys())

        if parent_ids:
            parent_entities = session.query(
                (
                    "select id, name from TypedContext where id in ({})"
                ).format(self.join_query_keys(parent_ids))
            ).all()
            other_entities_by_id.update({
                entity["id"]: entity
                for entity in parent_entities
            })

        for parent_id, parent_entity in other_entities_by_id.items():
            output.append((
                parent_entity,
                task_entities_by_parent_id[parent_id]
            ))

        return output

    def compute_template(self, data, template):
        filled_template = template.format(data)
        if filled_template.solved:
            return os.path.normpath(filled_template)

        self.log.warning(
            "Template \"{}\" was not fully filled \"{}\"".format(
                filled_template.template, filled_template
            )
        )
        return os.path.normpath(filled_template.split("{")[0])

get_all_entities(session, entities)

Parameters:

Name Type Description Default
session Session

ftrack session.

required
entities list[Entity]

List of entities.

required

Returns:

Type Description

tuple[list, list]: Tuple where first item is list of task entities and second item is list of entities that are not task entities. All are entities that were passed in and their children.

Source code in client/ayon_ftrack/event_handlers_user/action_create_folders.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
def get_all_entities(self, session, entities):
    """

    Args:
        session (ftrack_api.session.Session): ftrack session.
        entities (list[ftrack_api.entity.base.Entity]): List of entities.

    Returns:
        tuple[list, list]: Tuple where first item is list of task entities
            and second item is list of entities that are not task
            entities. All are entities that were passed in and
            their children.
    """

    task_entities = []
    other_entities = []

    query_queue = collections.deque()
    query_queue.append(entities)
    while query_queue:
        entities = query_queue.popleft()
        if not entities:
            continue

        no_task_entities = []
        for entity in entities:
            if entity.entity_type.lower() == "task":
                task_entities.append(entity)
            else:
                no_task_entities.append(entity)

        if not no_task_entities:
            continue

        other_entities.extend(no_task_entities)

        no_task_entity_ids = {entity["id"] for entity in no_task_entities}
        next_entities = session.query(
            (
                "select id, parent_id"
                " from TypedContext where parent_id in ({})"
            ).format(self.join_query_keys(no_task_entity_ids))
        ).all()
        query_queue.append(next_entities)
    return task_entities, other_entities

get_entities_hierarchy(session, task_entities, other_entities)

Parameters:

Name Type Description Default
session Session

ftrack session.

required
task_entities list[Entity]

List of task entities.

required
other_entities list[Entity]

List of entities that are not task entities.

required

Returns:

Type Description

list[tuple[ftrack_api.entity.base.Entity, list]]: List of tuples where first item is parent entity and second item is list of task entities that are children of parent entity.

Source code in client/ayon_ftrack/event_handlers_user/action_create_folders.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
def get_entities_hierarchy(self, session, task_entities, other_entities):
    """

    Args:
        session (ftrack_api.session.Session): ftrack session.
        task_entities (list[ftrack_api.entity.base.Entity]): List of task
            entities.
        other_entities (list[ftrack_api.entity.base.Entity]): List of
            entities that are not task entities.

    Returns:
        list[tuple[ftrack_api.entity.base.Entity, list]]: List of tuples
            where first item is parent entity and second item is list of
            task entities that are children of parent entity.
    """

    output = []
    task_entity_ids = {entity["id"] for entity in task_entities}
    if not task_entity_ids:
        return output

    full_task_entities = session.query(
        (
            "select id, name, type_id, parent_id"
            " from TypedContext where id in ({})"
        ).format(self.join_query_keys(task_entity_ids))
    ).all()
    task_entities_by_parent_id = collections.defaultdict(list)
    for entity in full_task_entities:
        parent_id = entity["parent_id"]
        task_entities_by_parent_id[parent_id].append(entity)

    if not task_entities_by_parent_id:
        return output

    parent_ids = set(task_entities_by_parent_id.keys())

    other_entities_by_id = {
        entity["id"]: entity
        for entity in other_entities
    }
    parent_ids -= set(other_entities_by_id.keys())

    if parent_ids:
        parent_entities = session.query(
            (
                "select id, name from TypedContext where id in ({})"
            ).format(self.join_query_keys(parent_ids))
        ).all()
        other_entities_by_id.update({
            entity["id"]: entity
            for entity in parent_entities
        })

    for parent_id, parent_entity in other_entities_by_id.items():
        output.append((
            parent_entity,
            task_entities_by_parent_id[parent_id]
        ))

    return output