diff --git a/src/deadline/keyshot_submitter/Submit to AWS Deadline Cloud.py b/src/deadline/keyshot_submitter/Submit to AWS Deadline Cloud.py index 0b0368a..1743e4b 100644 --- a/src/deadline/keyshot_submitter/Submit to AWS Deadline Cloud.py +++ b/src/deadline/keyshot_submitter/Submit to AWS Deadline Cloud.py @@ -357,7 +357,19 @@ def gui_submit(bundle_directory: str) -> Optional[dict[str, Any]]: return None -def main(): +def main(lux): + if lux.isSceneChanged(): + result = lux.getInputDialog( + title="Unsaved changes", + values=[(lux.DIALOG_LABEL, "You have unsaved changes. Do you want to save your file?")], + ) + # result is {} if the user clicks Ok and None if the user clicks cancel + if result is None: + # Raise an exception so Keyshot shows the script's result status as "Failure" instead of "Success" + raise Exception("Changes must be saved before submitting.") + else: + lux.saveFile() + scene_file = lux.getSceneInfo()["file"] external_files = lux.getExternalFiles() current_frame = lux.getAnimationFrame() @@ -413,4 +425,4 @@ def main(): if __name__ == "__main__": - main() + main(lux) diff --git a/test/keyshot_submitter/test_keyshot_submitter.py b/test/keyshot_submitter/test_keyshot_submitter.py index 9328f94..1c1cc41 100644 --- a/test/keyshot_submitter/test_keyshot_submitter.py +++ b/test/keyshot_submitter/test_keyshot_submitter.py @@ -1,9 +1,11 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +from unittest.mock import Mock import mock_lux # type: ignore[import-not-found] # noqa: F401 import json import os import tempfile +import pytest deadline = __import__("deadline.keyshot_submitter.Submit to AWS Deadline Cloud") @@ -242,3 +244,22 @@ def test_settings_apply_submitter_settings(): assert sorted(settings.input_directories) == ["test_directory_2"] assert sorted(settings.output_directories) == ["test_directory_2", "test_directory_4"] assert sorted(settings.referenced_paths) == ["test_ref_path_2"] + + +def test_unsaved_changes_prompt(): + local_mock_lux = Mock() + local_mock_lux.isSceneChanged.return_value = True + + local_mock_lux.getInputDialog.return_value = None # emulate clicking Cancel + with pytest.raises(Exception): + submitter.main(local_mock_lux) + local_mock_lux.saveFile.assert_not_called() + + local_mock_lux.getInputDialog.return_value = {} # emulate clicking Ok + # Raise an exception so the main() handler exits after the file save operation is called. + # We want to verify that the saveFile call is made, but don't want to run the rest of the + # submitter. + local_mock_lux.saveFile.side_effect = Exception() + with pytest.raises(Exception): + submitter.main(local_mock_lux) + local_mock_lux.saveFile.assert_called()