Skip to content

workio

Host API required for Work Files.

OpenFileCacher

Store information about opening file.

When file is opening QApplcation events should not be processed.

Source code in client/ayon_blender/api/workio.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class OpenFileCacher:
    """Store information about opening file.

    When file is opening QApplcation events should not be processed.
    """
    opening_file = False

    @classmethod
    def post_load(cls):
        cls.opening_file = False

    @classmethod
    def set_opening(cls):
        cls.opening_file = True

current_file()

Return the path of the open scene file.

Source code in client/ayon_blender/api/workio.py
61
62
63
64
65
66
67
def current_file() -> Optional[str]:
    """Return the path of the open scene file."""

    current_filepath = bpy.data.filepath
    if Path(current_filepath).is_file():
        return current_filepath
    return None

file_extensions()

Return the supported file extensions for Blender scene files.

Source code in client/ayon_blender/api/workio.py
76
77
78
79
def file_extensions() -> List[str]:
    """Return the supported file extensions for Blender scene files."""

    return [".blend"]

has_unsaved_changes()

Does the open scene file have unsaved changes?

Source code in client/ayon_blender/api/workio.py
70
71
72
73
def has_unsaved_changes() -> bool:
    """Does the open scene file have unsaved changes?"""

    return bpy.data.is_dirty

open_file(filepath)

Open the scene file in Blender.

Source code in client/ayon_blender/api/workio.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def open_file(filepath: str) -> Optional[str]:
    """Open the scene file in Blender."""
    OpenFileCacher.set_opening()

    preferences = bpy.context.preferences
    load_ui = preferences.filepaths.use_load_ui
    use_scripts = preferences.filepaths.use_scripts_auto_execute
    result = bpy.ops.wm.open_mainfile(
        filepath=filepath,
        load_ui=load_ui,
        use_scripts=use_scripts,
    )

    if result == {'FINISHED'}:
        return filepath
    return None

save_file(filepath, copy=False)

Save the open scene file.

Source code in client/ayon_blender/api/workio.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def save_file(filepath: str, copy: bool = False) -> Optional[str]:
    """Save the open scene file."""

    preferences = bpy.context.preferences
    compress = preferences.filepaths.use_file_compression
    relative_remap = preferences.filepaths.use_relative_paths
    result = bpy.ops.wm.save_as_mainfile(
        filepath=filepath,
        compress=compress,
        relative_remap=relative_remap,
        copy=copy,
    )

    if result == {'FINISHED'}:
        return filepath
    return None

work_root(session)

Return the default root to browse for work files.

Source code in client/ayon_blender/api/workio.py
82
83
84
85
86
87
88
89
def work_root(session: dict) -> str:
    """Return the default root to browse for work files."""

    work_dir = session["AYON_WORKDIR"]
    scene_dir = session.get("AVALON_SCENEDIR")
    if scene_dir:
        return str(Path(work_dir, scene_dir))
    return work_dir