Autosave preferences can be read after Nuke opens the script

This commit is contained in:
Jiri Sindelar 2023-07-13 13:15:23 +02:00
parent 4f4e1ac105
commit 6644f4c5f8

View file

@ -1,6 +1,7 @@
"""Host API required Work Files tool"""
import os
import nuke
import shutil
from .utils import is_headless
@ -21,21 +22,40 @@ def save_file(filepath):
def open_file(filepath):
def read_script(nuke_script):
nuke.scriptClear()
if int(nuke.NUKE_VERSION_MAJOR) > 12:
nuke.scriptReadFile(nuke_script)
else:
nuke.scriptOpen(nuke_script)
nuke.Root()["name"].setValue(nuke_script)
nuke.Root()["project_directory"].setValue(os.path.dirname(nuke_script))
nuke.Root().setModified(False)
filepath = filepath.replace("\\", "/")
# To remain in the same window, we have to clear the script and read
# in the contents of the workfile.
nuke.scriptClear()
# Nuke Preferences can be read after the script is read.
read_script(filepath)
if not is_headless():
autosave = nuke.toNode("preferences")["AutoSaveName"].evaluate()
autosave_prmpt = "Autosave detected.\nWould you like to load the autosave file?" # noqa
autosave_prmpt = "Autosave detected.\n" \
"Would you like to load the autosave file?" # noqa
if os.path.isfile(autosave) and nuke.ask(autosave_prmpt):
filepath = autosave
try:
# Overwrite the filepath with autosave
shutil.copy(autosave, filepath)
# Now read the (auto-saved) script again
read_script(filepath)
except shutil.Error as err:
nuke.message(
"Detected autosave file could not be used.\n{}"
.format(err))
nuke.scriptReadFile(filepath)
nuke.Root()["name"].setValue(filepath)
nuke.Root()["project_directory"].setValue(os.path.dirname(filepath))
nuke.Root().setModified(False)
return True