Skip to content

load_workfile

LoadWorkfile

Bases: Loader

Load workfile.

Source code in client/ayon_tvpaint/plugins/load/load_workfile.py
 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
class LoadWorkfile(plugin.Loader):
    """Load workfile."""

    product_base_types = {"workfile"}
    product_types = product_base_types
    representations = {"*"}
    extensions = {"tvpp"}

    label = "Load Workfile"

    def load(self, context, name, namespace, options):
        # Load context of current workfile as first thing
        #   - which context and extension has
        filepath = self.filepath_from_context(context)
        filepath = filepath.replace("\\", "/")

        if not os.path.exists(filepath):
            raise FileExistsError(
                "The loaded file does not exist. Try downloading it first."
            )

        host = registered_host()
        current_file = host.get_current_workfile()
        work_context = get_current_workfile_context()

        george_script = "tv_LoadProject '\"'\"{}\"'\"'".format(
            filepath
        )
        execute_george_through_file(george_script)

        # Save workfile.
        host_name = "tvpaint"
        if "project_name" in work_context:
            project_name = context["project"]["name"]
            folder_path = context["folder"]["path"]
            # Get task from version (is not part of representation context)
            version_entity = context["version"]
            task_id = version_entity.get("taskId")
            task_name = None
            if task_id:
                task_entity = ayon_api.get_task_by_id(
                    project_name, task_id, fields={"name"}
                )
                if task_entity:
                    task_name = task_entity["name"]
        else:
            project_name = work_context.get("project_name")
            folder_path = work_context.get("folder_path")
            task_name = work_context.get("task_name")

        # Far cases when there is workfile without work_context
        if not folder_path:
            current_context = get_current_context()
            project_name = current_context.get("project_name")
            folder_path = current_context.get("folder_path")
            task_name = current_context.get("task_name")

        template_key = get_workfile_template_key_from_context(
            project_name,
            folder_path,
            task_name,
            host_name,
        )
        anatomy = Anatomy(project_name)

        data = get_template_data_with_names(
            project_name, folder_path, task_name, host_name
        )
        data["root"] = anatomy.roots

        work_template = anatomy.get_template_item("work", template_key)

        # Define saving file extension
        extensions = host.get_workfile_extensions()
        if current_file:
            # Match the extension of current file
            _, extension = os.path.splitext(current_file)
        else:
            # Fall back to the first extension supported for this host.
            extension = extensions[0]

        data["ext"] = extension.lstrip(".")

        work_root = work_template["directory"].format_strict(data)
        version = get_last_workfile_with_version(
            work_root, work_template["file"].template, data, extensions
        )[1]

        if version is None:
            version = get_versioning_start(
                project_name=project_name,
                host_name="tvpaint",
                task_name=task_name,
                task_type=data["task"]["type"],
                product_base_type="workfile",
            )
        else:
            version += 1

        data["version"] = version

        filename = work_template["file"].format_strict(data)
        path = os.path.join(work_root, filename)
        host.save_workfile(path)