Skip to content

match_shotgrid_hierarchy_in_ayon

match_shotgrid_hierarchy_in_ayon(entity_hub, sg_project, sg_session, sg_enabled_entities, project_code_field, custom_attribs_map, addon_settings)

Replicate a Shotgrid project into AYON.

This function creates a "deck" which we keep increasing while traversing the Shotgrid project and finding new children, this is more efficient than creating a dictionary with the while Shotgrid project structure since we popleft the elements when processing them.

Parameters:

Name Type Description Default
entity_hub EntityHub

The AYON EntityHub.

required
sg_project dict

The Shotgrid project.

required
sg_project Shotgun

The Shotgrid session.

required
project_code_field str

The Shotgrid project code field.

required
Source code in services/shotgrid_common/ayon_shotgrid_hub/match_shotgrid_hierarchy_in_ayon.py
 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
def match_shotgrid_hierarchy_in_ayon(
    entity_hub: ayon_api.entity_hub.EntityHub,
    sg_project: Dict,
    sg_session: shotgun_api3.Shotgun,
    sg_enabled_entities: List[str],
    project_code_field: str,
    custom_attribs_map: Dict[str, str],
    addon_settings: Dict[str, str]
):
    """Replicate a Shotgrid project into AYON.

    This function creates a "deck" which we keep increasing while traversing
    the Shotgrid project and finding new children, this is more efficient than
    creating a dictionary with the while Shotgrid project structure since we
    `popleft` the elements when processing them.

    Args:
        entity_hub (ayon_api.entity_hub.EntityHub): The AYON EntityHub.
        sg_project (dict): The Shotgrid project.
        sg_project (shotgun_api3.Shotgun): The Shotgrid session.
        project_code_field (str): The Shotgrid project code field.
    """
    log.info("Getting Shotgrid entities.")
    sg_ay_dicts, sg_ay_dicts_parents = get_sg_entities(
        sg_session,
        sg_project,
        sg_enabled_entities,
        project_code_field,
        custom_attribs_map,
        addon_settings=addon_settings,
    )

    sg_ay_dicts_deck = collections.deque()

    # Append the project's direct children.
    for sg_ay_dict_child_id in sg_ay_dicts_parents[sg_project["id"]]:
        sg_ay_dicts_deck.append(
            (entity_hub.project_entity, sg_ay_dict_child_id)
        )

    sg_project_sync_status = "Synced"
    processed_ids = set()

    while sg_ay_dicts_deck:
        (ay_parent_entity, sg_ay_dict_child_id) = sg_ay_dicts_deck.popleft()
        sg_ay_dict = sg_ay_dicts[sg_ay_dict_child_id]
        sg_entity_id = sg_ay_dict["attribs"][SHOTGRID_ID_ATTRIB]
        if sg_ay_dict_child_id in processed_ids:
            msg = (
                f"Entity {sg_entity_id} already processed, skipping..."
                f"Sg Ay Dict: {sg_ay_dict} - "
                f"Ay Parent Entity: {ay_parent_entity}"
            )
            log.warning(msg)
            continue

        processed_ids.add(sg_ay_dict_child_id)

        log.debug(f"Deck size: {len(sg_ay_dicts_deck)}")

        if sg_ay_dict["type"].lower() == "comment":
            handle_comment(sg_ay_dict, sg_session, entity_hub)
            continue

        if sg_ay_dict["type"].lower() == "entity_list":
            ayon_list_id = sg_ay_dict["data"].get(CUST_FIELD_CODE_ID)
            ay_list_exists = ayon_list_id and any(
                lst["id"] == ayon_list_id
                for lst in ayon_api.get_entity_lists(
                    entity_hub.project_name, fields=["id"]
                )
            )
            sync_ay_entity_list_from_sg_event(
                {
                    "type": "attribute_change" if ay_list_exists else "new_entity",
                    "entity_id": sg_entity_id,
                    "attribute_name": "versions",
                },
                sg_project,
                sg_session,
            )
            continue

        shotgrid_type = sg_ay_dict["attribs"].get(SHOTGRID_TYPE_ATTRIB)
        ay_id = sg_ay_dict["data"].get(CUST_FIELD_CODE_ID)

        ay_entity = None
        sg_entity_sync_status = "Synced"

        if ay_id:
            ay_entity = entity_hub.get_or_query_entity_by_id(
                ay_id, [sg_ay_dict["type"]])

        if shotgrid_type == "Version" and not ay_entity:
            log.warning(
                "Version creation from Flow is not implemented because "
                "Flow entity is much less strict than AYON product with reviewable "
                "(e.g. product name and integer are not mandatory in Flow)."
            )
            continue

        # If we haven't found the ay_entity by its id, check by its name
        # to avoid creating duplicates and erroring out
        if ay_entity is None:

            sg_parent_field = get_sg_entity_parent_field(
                sg_session,
                sg_project,
                shotgrid_type,
                sg_enabled_entities,
            )
            asset_category_parent = sg_parent_field == "sg_asset_type"

            if (
                shotgrid_type == "Asset"
                and asset_category_parent
            ):
                # Parenting to AssetCategory is enabled.
                # reparenting under already set parent (asset category folder).
                log.debug("Reparenting %r under %r.", sg_ay_dict, ay_parent_entity)

            elif shotgrid_type in ("Sequence", "Episode", "Shot", "AssetCategory", "Asset"):
                ay_parent_entity = get_reparenting_from_settings(
                    entity_hub,
                    sg_ay_dict,
                    addon_settings
                ) or ay_parent_entity

            name = slugify_string(sg_ay_dict["name"])
            for child in ay_parent_entity.children:
                if child.name.lower() == name.lower():
                    ay_entity = child
                    break

        # If we couldn't find it we create it.
        if ay_entity is None:
            ay_entity = create_new_ayon_entity(
                sg_session,
                entity_hub,
                ay_parent_entity,
                sg_ay_dict
            )
        else:

            if not _update_ay_entity(
                ay_entity,
                custom_attribs_map,
                entity_hub,
                sg_ay_dict,
                sg_entity_id,
            ):
                sg_entity_sync_status = "Failed"
                sg_project_sync_status = "Failed"


        # skip if no ay_entity is found
        # perhaps due Task with project entity as parent
        if not ay_entity:
            log.error(f"Entity {sg_ay_dict} not found in AYON.")
            continue

        # pass AYON id to SG
        _update_sg_entity(
            ay_entity,
            sg_ay_dict,
            sg_ay_dicts,
            sg_entity_id,
            sg_entity_sync_status,
            sg_session
        )

        # If the entity has children, add it to the deck
        for sg_child_id in sg_ay_dicts_parents.get(sg_ay_dict_child_id, []):
            sg_ay_dicts_deck.append((ay_entity, sg_child_id))

    _sync_project_attributes(entity_hub, custom_attribs_map, sg_project)

    try:
        entity_hub.commit_changes()
    except Exception:
        log.error(
            "Unable to commit all entities to AYON!", exc_info=True)

    log.info(
        "Processed entities successfully!. "
        f"Amount of entities: {len(processed_ids)}"
    )

    # Update Shotgrid project with AYON ID and sync status
    sg_session.update(
        "Project",
        sg_project["id"],
        {
            CUST_FIELD_CODE_ID: entity_hub.project_entity.id,
            CUST_FIELD_CODE_SYNC: sg_project_sync_status
        }
    )