Skip to content

workio

Host API required Work Files tool

open_file(filepath)

Loading project

Source code in client/ayon_resolve/api/workio.py
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
def open_file(filepath):
    """
    Loading project
    """

    from . import bmdvr

    project_manager = get_project_manager()
    page = bmdvr.GetCurrentPage()
    if page is not None:
        # Save current project only if Resolve has an active page, otherwise
        # we consider Resolve being in a pre-launch state (no open UI yet)
        resolve_project = get_current_resolve_project()
        # check if resolve_project is string or binary name
        if isinstance(resolve_project, str):
            print(f"Saving current resolve project: {resolve_project}")
            project_manager.SaveProject()

    file = os.path.basename(filepath)
    fname, _ = os.path.splitext(file)

    # handle project db override if set
    project_name = get_current_project_name()
    settings = get_project_settings(project_name)
    override_is_valid = True
    if settings["resolve"]["project_db"].get("enabled", False):
        log.info("Handling project database override...")
        overrides = settings["resolve"]["project_db"]
        override_is_valid = handle_project_db_override(
            project_name, overrides
        )
        if not override_is_valid:
            return False

        if settings["resolve"]["project_db"]["db_type"] == "Disk":
            handle_local_vs_exported_project(
                settings, project_name, filepath
            )

    try:
        # load project from input path
        resolve_project = project_manager.LoadProject(fname)
        log.info(f"Project {resolve_project.GetName()} opened...")

    except AttributeError:
        log.warning((f"Project with name `{fname}` does not exist! It will "
                     f"be imported from {filepath} and then loaded..."))
        if project_manager.ImportProject(filepath):
            # load project from input path
            resolve_project = project_manager.LoadProject(fname)
            log.info(f"Project imported/loaded {resolve_project.GetName()}...")
            return True
        return False
    return True

save_file(filepath)

Save and export a Resolve workfile.

Raises:

Type Description
RuntimeError

If saving, exporting or the project database override fails. Callers such as save_next_version rely on the host save raising instead of returning False.

Source code in client/ayon_resolve/api/workio.py
 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
def save_file(filepath):
    """Save and export a Resolve workfile.

    Raises:
        RuntimeError: If saving, exporting or the project database override
            fails. Callers such as ``save_next_version`` rely on the host
            save raising instead of returning ``False``.
    """
    project_manager = get_project_manager()
    project_saved = project_manager.SaveProject()
    if not project_saved:
        log.error("Failed to save current project!")
        raise RuntimeError("Failed to save current Resolve project.")

    resolve_project = get_current_resolve_project()
    incoming_wf = Path(filepath)
    current_wf = incoming_wf.with_name(
        resolve_project.GetName() + ".drp")

    # handle project db override if set
    project_name = get_current_project_name()
    settings = get_project_settings(project_name)
    override_is_valid = True
    if settings["resolve"]["project_db"].get("enabled", False):
        log.info("Handling project database override...")
        overrides = settings["resolve"]["project_db"]
        override_is_valid = handle_project_db_override(
            project_name, overrides
        )
        if not override_is_valid:
            raise RuntimeError(
                "Failed to save Resolve workfile: invalid project "
                "database override."
            )

    rename_db_project = settings["resolve"].get("rename_db_project_on_increment", True)
    if (
        "Untitled Project" not in current_wf.stem
        and not _is_project_in_current_folder(project_manager, resolve_project)
    ):
        # 'ExportProject' finds projects by name in the current folder only
        folder_name = project_manager.GetCurrentFolder()
        ProjectFolderMismatchWarning(
            resolve_project.GetName(), folder_name
        ).exec_()
        raise RuntimeError(
            f"Resolve project '{resolve_project.GetName()}' is not in the"
            f" current Project Manager folder '{folder_name}'."
            " Open a workfile of this context or start a new project"
            " before saving."
        )

    if "Untitled Project" in current_wf.stem:
        # saving initial workfile from currently opened project
        new_project = project_manager.CreateProject(incoming_wf.stem)
        if not new_project:
            raise RuntimeError(
                f"Failed to create new Resolve project '{incoming_wf.stem}'."
            )
        if not project_manager.SaveProject():
            raise RuntimeError(
                f"Failed to save new Resolve project '{incoming_wf.stem}'."
            )
        exported = _export_project(
            project_manager, incoming_wf.stem, incoming_wf.as_posix()
        )
        log.info(f"New project {incoming_wf.stem} exported: {exported}")
    elif current_wf.stem != incoming_wf.stem:
        # workfile shall be incremented
        if rename_db_project:
            # increment with local renaming
            if not resolve_project.SetName(incoming_wf.stem):
                raise RuntimeError(
                    f"Failed to rename Resolve project from "
                    f"'{current_wf.stem}' to '{incoming_wf.stem}'. "
                    "A project with that name may already exist."
                )
            try:
                exported = _export_project(
                    project_manager, incoming_wf.stem, incoming_wf.as_posix()
                )
            except Exception:
                if not resolve_project.SetName(current_wf.stem):
                    log.error(
                        "Failed to restore Resolve project name to '%s'.",
                        current_wf.stem,
                    )
                raise
            log.info(f"Incremented workfile with local rename to {incoming_wf.as_posix()}: {exported}")
        else:
            # increment without local renaming but reimport
            _export_project(
                project_manager, current_wf.stem, current_wf.as_posix()
            )
            exported = _export_project(
                project_manager, current_wf.stem, incoming_wf.as_posix()
            )
            if not project_manager.ImportProject(incoming_wf.as_posix()):
                raise RuntimeError(
                    f"Failed to import Resolve project from '{incoming_wf.as_posix()}'."
                )
            if project_manager.LoadProject(incoming_wf.stem) is None:
                raise RuntimeError(
                    f"Failed to load Resolve project '{incoming_wf.stem}'."
                )
            log.info(f"Incremented workfile with reimport to {incoming_wf.as_posix()}: {exported}")
    else:
        # workfile export without increment
        exported = _export_project(
            project_manager, incoming_wf.stem, incoming_wf.as_posix()
        )
        log.info(f"Project exported without increment to {incoming_wf.as_posix()}: {exported}")