Add --open-last CLI argument that opens the most recent file

Add a CLI argument `--open-last` that opens the most recent file,
effectively doing the same as {key Ctrl Shift O}, {key Enter} after
starting Blender.

When there are no known recent files, print a warning and do nothing,
showing the startup file instead.

Note that this does not try to be smart about restoring the last Blender
session. It just opens the file from disk, as if the user had typed
`blender $(head -n1 ~/.config/blender/2.93/config/recent-files.txt)`.

There is also no smartness when that file cannot be opened; it behaves
exactly the same as typing the most recent filename on the CLI.

Reviewed by: mont29, campbellbarton

Differential Revision: https://developer.blender.org/D10563
This commit is contained in:
Sybren A. Stüvel
2021-03-01 14:36:52 +01:00
parent e06f5f64ae
commit 3649b5b6df

View File

@@ -84,6 +84,8 @@
# include "DEG_depsgraph_build.h"
# include "DEG_depsgraph_debug.h"
# include "WM_types.h"
# include "creator_intern.h" /* own include */
/* -------------------------------------------------------------------- */
@@ -1994,6 +1996,21 @@ static int arg_handle_load_file(int UNUSED(argc), const char **argv, void *data)
return 0;
}
static const char arg_handle_load_last_file_doc[] =
"\n\t"
"Open the most recently opened blend file, instead of the default startup file.";
static int arg_handle_load_last_file(int UNUSED(argc), const char **UNUSED(argv), void *data)
{
if (BLI_listbase_is_empty(&G.recent_files)) {
printf("Warning: no recent files known, opening default startup file instead.\n");
return -1;
}
const RecentFile *recent_file = G.recent_files.first;
const char *fake_argv[] = {recent_file->filepath};
return arg_handle_load_file(1, fake_argv, data);
}
void main_args_setup(bContext *C, bArgs *ba)
{
@@ -2217,6 +2234,8 @@ void main_args_setup(bContext *C, bArgs *ba)
BLI_args_add(ba, "-F", "--render-format", CB(arg_handle_image_type_set), C);
BLI_args_add(ba, "-x", "--use-extension", CB(arg_handle_extension_set), C);
BLI_args_add(ba, NULL, "--open-last", CB(arg_handle_load_last_file), C);
# undef CB
# undef CB_EX
}