Skip to content

pre_copy_last_published_workfile

CopyLastPublishedWorkfile

Bases: PreLaunchHook

Copy last published workfile as first workfile.

Prelaunch hook works only if last workfile leads to not existing file. - That is possible only if it's first version.

Source code in client/ayon_sitesync/launch_hooks/pre_copy_last_published_workfile.py
 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
class CopyLastPublishedWorkfile(PreLaunchHook):
    """Copy last published workfile as first workfile.

    Prelaunch hook works only if last workfile leads to not existing file.
        - That is possible only if it's first version.
    """

    # Before `AddLastWorkfileToLaunchArgs`
    order = -1
    # any DCC could be used but TrayPublisher and other specials
    app_groups = ["blender", "photoshop", "tvpaint", "aftereffects",
                  "nuke", "nukeassist", "nukex", "hiero", "nukestudio",
                  "maya", "harmony", "celaction", "flame", "fusion",
                  "houdini", "tvpaint"]

    def execute(self):
        """Check if local workfile doesn't exist, else copy it.

        1- Check if setting for this feature is enabled
        2- Check if workfile in work area doesn't exist
        3- Check if published workfile exists and is copied locally in publish
        4- Substitute copied published workfile as first workfile
           with incremented version by +1

        Returns:
            None: This is a void method.
        """
        project_name = self.data["project_name"]
        sitesync_addon = self.addons_manager.get("sitesync")
        if (
            not sitesync_addon
            or not sitesync_addon.enabled
            or not sitesync_addon.is_project_enabled(project_name, True)
        ):
            self.log.debug("Sync server module is not enabled or available")
            return

        # Check there is no workfile available
        last_workfile = self.data.get("last_workfile_path")
        if os.path.exists(last_workfile):
            self.log.debug(
                "Last workfile exists. Skipping {} process.".format(
                    self.__class__.__name__
                )
            )
            return

        project_settings = self.data["project_settings"]

        # Get data
        anatomy = self.data["anatomy"]
        task_id = self.data["task_entity"]["id"]
        folder_entity = self.data["folder_entity"]
        folder_id = folder_entity["id"]
        task_name = self.data["task_name"]
        task_type = self.data["task_type"]
        host_name = self.application.host_name
        project_entity = self.data["project_entity"]
        task_entity = self.data["task_entity"]

        use_last_published_workfile = should_use_last_workfile_on_launch(
            project_name, host_name, task_name, task_type,
            project_settings=project_settings
        )

        if use_last_published_workfile is None:
            self.log.info(
                (
                    "Seems like old version of settings is used."
                    ' Can\'t access custom templates in host "{}".'.format(
                        host_name
                    )
                )
            )
            return
        elif use_last_published_workfile is False:
            self.log.info(
                (
                    'Project "{}" has turned off to use last published'
                    ' workfile as first workfile for host "{}"'.format(
                        project_name, host_name
                    )
                )
            )
            return

        self.log.info("Trying to fetch last published workfile...")

        workfile_representation = (
            self._get_last_published_workfile_representation(
                project_name, folder_id, task_id, host_name
            )
        )

        if not workfile_representation:
            self.log.info("Couldn't find published workfile representation")
            return

        max_retries = int(
            sitesync_addon.sync_project_settings
            [project_name]
            ["config"]
            ["retry_cnt"]
        )

        # Copy file and substitute path
        last_published_workfile_path = download_last_published_workfile(
            host_name,
            project_name,
            task_name,
            workfile_representation,
            max_retries,
            anatomy=anatomy,
            sitesync_addon=sitesync_addon,
        )
        if not last_published_workfile_path:
            self.log.debug(
                "Couldn't download {}".format(last_published_workfile_path)
            )
            return

        # Get workfile data
        workfile_data = get_template_data(
            project_entity, folder_entity, task_entity, host_name,
            project_settings
        )

        extension = last_published_workfile_path.split(".")[-1]
        workfile_data["version"] = (
                workfile_representation["context"]["version"] + 1)
        workfile_data["ext"] = extension

        template_key = get_workfile_template_key(
            task_name, host_name, project_name, project_settings
        )
        template = anatomy.get_template_item("work", template_key, "path")
        local_workfile_path = template.format_strict(workfile_data)

        # Copy last published workfile to local workfile directory
        shutil.copy(
            last_published_workfile_path,
            local_workfile_path,
        )

        self.data["last_workfile_path"] = local_workfile_path
        # Keep source filepath for further path conformation
        self.data["source_filepath"] = last_published_workfile_path

    def _get_last_published_workfile_representation(self,
            project_name, folder_id, task_id, host_name):
        """Looks for last published representation for host and context"""
        product_entities = get_products(
            project_name,
            folder_ids={folder_id},
            product_types={"workfile"}
        )
        product_ids = {
            product_entity["id"]
            for product_entity in product_entities
        }
        if not product_ids:
            return
        versions_by_product_id = get_last_versions(
            project_name,
            product_ids
        )
        version_ids = {
            version_entity["id"]
            for version_entity in versions_by_product_id.values()
            if version_entity["taskId"] == task_id
        }
        if not version_ids:
            return

        addons_manager = self.addons_manager
        host_addon = addons_manager[host_name]
        workfile_extensions = host_addon.get_workfile_extensions()

        for representation_entity in get_representations(
            project_name,
            version_ids=version_ids,
        ):
            ext = representation_entity["context"].get("ext")
            ext = f".{ext}"
            if ext in workfile_extensions:
                return representation_entity

execute()

Check if local workfile doesn't exist, else copy it.

1- Check if setting for this feature is enabled 2- Check if workfile in work area doesn't exist 3- Check if published workfile exists and is copied locally in publish 4- Substitute copied published workfile as first workfile with incremented version by +1

Returns:

Name Type Description
None

This is a void method.

Source code in client/ayon_sitesync/launch_hooks/pre_copy_last_published_workfile.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
def execute(self):
    """Check if local workfile doesn't exist, else copy it.

    1- Check if setting for this feature is enabled
    2- Check if workfile in work area doesn't exist
    3- Check if published workfile exists and is copied locally in publish
    4- Substitute copied published workfile as first workfile
       with incremented version by +1

    Returns:
        None: This is a void method.
    """
    project_name = self.data["project_name"]
    sitesync_addon = self.addons_manager.get("sitesync")
    if (
        not sitesync_addon
        or not sitesync_addon.enabled
        or not sitesync_addon.is_project_enabled(project_name, True)
    ):
        self.log.debug("Sync server module is not enabled or available")
        return

    # Check there is no workfile available
    last_workfile = self.data.get("last_workfile_path")
    if os.path.exists(last_workfile):
        self.log.debug(
            "Last workfile exists. Skipping {} process.".format(
                self.__class__.__name__
            )
        )
        return

    project_settings = self.data["project_settings"]

    # Get data
    anatomy = self.data["anatomy"]
    task_id = self.data["task_entity"]["id"]
    folder_entity = self.data["folder_entity"]
    folder_id = folder_entity["id"]
    task_name = self.data["task_name"]
    task_type = self.data["task_type"]
    host_name = self.application.host_name
    project_entity = self.data["project_entity"]
    task_entity = self.data["task_entity"]

    use_last_published_workfile = should_use_last_workfile_on_launch(
        project_name, host_name, task_name, task_type,
        project_settings=project_settings
    )

    if use_last_published_workfile is None:
        self.log.info(
            (
                "Seems like old version of settings is used."
                ' Can\'t access custom templates in host "{}".'.format(
                    host_name
                )
            )
        )
        return
    elif use_last_published_workfile is False:
        self.log.info(
            (
                'Project "{}" has turned off to use last published'
                ' workfile as first workfile for host "{}"'.format(
                    project_name, host_name
                )
            )
        )
        return

    self.log.info("Trying to fetch last published workfile...")

    workfile_representation = (
        self._get_last_published_workfile_representation(
            project_name, folder_id, task_id, host_name
        )
    )

    if not workfile_representation:
        self.log.info("Couldn't find published workfile representation")
        return

    max_retries = int(
        sitesync_addon.sync_project_settings
        [project_name]
        ["config"]
        ["retry_cnt"]
    )

    # Copy file and substitute path
    last_published_workfile_path = download_last_published_workfile(
        host_name,
        project_name,
        task_name,
        workfile_representation,
        max_retries,
        anatomy=anatomy,
        sitesync_addon=sitesync_addon,
    )
    if not last_published_workfile_path:
        self.log.debug(
            "Couldn't download {}".format(last_published_workfile_path)
        )
        return

    # Get workfile data
    workfile_data = get_template_data(
        project_entity, folder_entity, task_entity, host_name,
        project_settings
    )

    extension = last_published_workfile_path.split(".")[-1]
    workfile_data["version"] = (
            workfile_representation["context"]["version"] + 1)
    workfile_data["ext"] = extension

    template_key = get_workfile_template_key(
        task_name, host_name, project_name, project_settings
    )
    template = anatomy.get_template_item("work", template_key, "path")
    local_workfile_path = template.format_strict(workfile_data)

    # Copy last published workfile to local workfile directory
    shutil.copy(
        last_published_workfile_path,
        local_workfile_path,
    )

    self.data["last_workfile_path"] = local_workfile_path
    # Keep source filepath for further path conformation
    self.data["source_filepath"] = last_published_workfile_path