D54: Added a filepath attribute to the read_homefile operator.
authorgaiaclary <gaia.clary@machinimatrix.org>
Wed, 27 Nov 2013 19:48:07 +0000 (20:48 +0100)
committergaiaclary <gaia.clary@machinimatrix.org>
Fri, 29 Nov 2013 22:13:55 +0000 (23:13 +0100)
This attribute allows to open a blend file as an alternative
start-up file. The attribute is only available from python.
This is an example call:

    bpy.ops.wm.read_homefile(filepath='path/to/a/file.blend')

This patch also changes readfile.c to ensure that unintentionally
stored cursor states are not imported from the loaded file.

source/blender/blenloader/intern/readfile.c
source/blender/windowmanager/intern/wm_files.c
source/blender/windowmanager/intern/wm_init_exit.c
source/blender/windowmanager/intern/wm_operators.c
source/blender/windowmanager/wm_files.h

index cb1d2a0b98f417b899393c5bd546f797d3d237b3..c6cf32c9477455480e90c25012b1b86dfeec23cf 100644 (file)
@@ -5531,6 +5531,10 @@ static void direct_link_windowmanager(FileData *fd, wmWindowManager *wm)
                win->drawmethod = -1;
                win->drawfail = 0;
                win->active = 0;
+
+               win->cursor      = 0;
+               win->lastcursor  = 0;
+               win->modalcursor = 0;
        }
        
        wm->timers.first = wm->timers.last = NULL;
index b3be3b4cc0c83a692f04e2883678883f33d37883..ec86977cab959d062d116bf9a7aaabceabdbd17b 100644 (file)
@@ -517,7 +517,9 @@ void WM_file_read(bContext *C, const char *filepath, ReportList *reports)
 /* called on startup,  (context entirely filled with NULLs) */
 /* or called for 'New File' */
 /* both startup.blend and userpref.blend are checked */
-int wm_homefile_read(bContext *C, ReportList *UNUSED(reports), short from_memory)
+/* the optional paramater custom_file points to an alterntive startup page */
+/* custom_file can be NULL */
+int wm_homefile_read(bContext *C, ReportList *reports, short from_memory, const char *custom_file)
 {
        ListBase wmbase;
        char startstr[FILE_MAX];
@@ -529,7 +531,17 @@ int wm_homefile_read(bContext *C, ReportList *UNUSED(reports), short from_memory
        G.relbase_valid = 0;
        if (!from_memory) {
                const char * const cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
-               if (cfgdir) {
+               if (custom_file) {
+                       BLI_strncpy(startstr, custom_file, FILE_MAX);
+
+                       if (cfgdir) {
+                               BLI_make_file_string(G.main->name, prefstr, cfgdir, BLENDER_USERPREF_FILE);
+                       }
+                       else {
+                               prefstr[0] = '\0';
+                       }
+               }
+               else if (cfgdir) {
                        BLI_make_file_string(G.main->name, startstr, cfgdir, BLENDER_STARTUP_FILE);
                        BLI_make_file_string(G.main->name, prefstr, cfgdir, BLENDER_USERPREF_FILE);
                }
@@ -544,13 +556,12 @@ int wm_homefile_read(bContext *C, ReportList *UNUSED(reports), short from_memory
        G.fileflags &= ~G_FILE_NO_UI;
        
        /* put aside screens to match with persistent windows later */
-       wm_window_match_init(C, &wmbase); 
+       wm_window_match_init(C, &wmbase);
        
        if (!from_memory) {
-               if (BLI_exists(startstr)) {
+               if (BLI_access(startstr, R_OK) == 0) {
                        success = (BKE_read_file(C, startstr, NULL) != BKE_READ_FILE_FAIL);
                }
-
                if (U.themes.first == NULL) {
                        if (G.debug & G_DEBUG)
                                printf("\nNote: No (valid) '%s' found, fall back to built-in default.\n\n", startstr);
@@ -558,6 +569,11 @@ int wm_homefile_read(bContext *C, ReportList *UNUSED(reports), short from_memory
                }
        }
 
+       if (success == 0 && custom_file && reports) {
+               BKE_reportf(reports, RPT_ERROR, "Could not read '%s'", custom_file);
+               /*We can not return from here because wm is already reset*/
+       }
+
        if (success == 0) {
                success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL, true);
                if (wmbase.first == NULL) wm_clear_default_size(C);
@@ -581,7 +597,7 @@ int wm_homefile_read(bContext *C, ReportList *UNUSED(reports), short from_memory
        G.fileflags &= ~G_FILE_RELATIVE_REMAP;
        
        /* check userdef before open window, keymaps etc */
-       wm_init_userdef(C, from_memory);
+       wm_init_userdef(C, (bool)from_memory);
        
        /* match the read WM with current WM */
        wm_window_match_do(C, &wmbase); 
@@ -636,8 +652,23 @@ int wm_history_read_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
 
 int wm_homefile_read_exec(bContext *C, wmOperator *op)
 {
-       int from_memory = strcmp(op->type->idname, "WM_OT_read_factory_settings") == 0;
-       return wm_homefile_read(C, op->reports, from_memory) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
+       int from_memory = (strcmp(op->type->idname, "WM_OT_read_factory_settings") == 0);
+       char filepath_buffer[FILE_MAX] = "";
+       char *filepath = NULL;
+
+       if (!from_memory)
+       {
+               RNA_string_get(op->ptr, "filepath", filepath_buffer);
+               if (filepath_buffer[0] != '\0') {
+                       filepath = filepath_buffer;
+                       if (BLI_access(filepath, R_OK)) {
+                               BKE_reportf(op->reports, RPT_ERROR, "Can't read alternative start-up file: '%s'", filepath);
+                               return OPERATOR_CANCELLED;
+                       }
+               }
+       }
+
+       return wm_homefile_read(C, op->reports, from_memory, filepath) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
 }
 
 void wm_read_history(void)
index 4e9b849af408d07a06e9141a0c79f9566e819a96..63aa3ce681152a6fcc3e7c190aa45022b76105db 100644 (file)
@@ -159,7 +159,7 @@ void WM_init(bContext *C, int argc, const char **argv)
        BLF_lang_init();
 
        /* get the default database, plus a wm */
-       wm_homefile_read(C, NULL, G.factory_startup);
+       wm_homefile_read(C, NULL, G.factory_startup, NULL);
        
        BLF_lang_set(NULL);
 
index 5d2edcecfbab97cd534e6ae5ea23cfd9025ed821..f2fb4d66300393ff9d007df724d0c3f5478de12f 100644 (file)
@@ -2029,12 +2029,19 @@ static void WM_OT_read_history(wmOperatorType *ot)
 
 static void WM_OT_read_homefile(wmOperatorType *ot)
 {
+       PropertyRNA *prop;
        ot->name = "Reload Start-Up File";
        ot->idname = "WM_OT_read_homefile";
        ot->description = "Open the default file (doesn't save the current file)";
        
        ot->invoke = WM_operator_confirm;
        ot->exec = wm_homefile_read_exec;
+
+       prop = RNA_def_string_file_path(ot->srna, "filepath", "", 
+                                       FILE_MAX, "File Path", 
+                                       "Path to an alternative start-up file");
+       RNA_def_property_flag(prop, PROP_HIDDEN);
+
        /* ommit poll to run in background mode */
 }
 
index 9fda6801925a37677bd320c1a2f9907710ee7fd2..0fa076131ab5359a639b76452facc5b33d880843 100644 (file)
@@ -35,7 +35,7 @@ void          wm_read_history(void);
 int                    wm_file_write(struct bContext *C, const char *target, int fileflags, struct ReportList *reports);
 int                    wm_history_read_exec(bContext *C, wmOperator *op);
 int                    wm_homefile_read_exec(struct bContext *C, struct wmOperator *op);
-int                    wm_homefile_read(struct bContext *C, struct ReportList *reports, short from_memory);
+int                    wm_homefile_read(struct bContext *C, struct ReportList *reports, short from_memory, const char *filepath);
 int                    wm_homefile_write_exec(struct bContext *C, struct wmOperator *op);
 int                    wm_userpref_write_exec(struct bContext *C, struct wmOperator *op);