[Bf-blender-cvs] [72272d2] master: D54: Added a filepath attribute to the read_homefile operator.

gaiaclary noreply at git.blender.org
Fri Nov 29 23:14:45 CET 2013


Commit: 72272d2e4f4676c5c67483f1005251253edef15f
Author: gaiaclary
Date:   Wed Nov 27 20:48:07 2013 +0100
http://developer.blender.org/rB72272d2e4f4676c5c67483f1005251253edef15f

D54: Added a filepath attribute to the read_homefile operator.

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.

===================================================================

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

===================================================================

diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c
index cb1d2a0..c6cf32c 100644
--- a/source/blender/blenloader/intern/readfile.c
+++ b/source/blender/blenloader/intern/readfile.c
@@ -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;
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index b3be3b4..ec86977 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -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)
diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c
index 4e9b849..63aa3ce 100644
--- a/source/blender/windowmanager/intern/wm_init_exit.c
+++ b/source/blender/windowmanager/intern/wm_init_exit.c
@@ -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);
 
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index 5d2edce..f2fb4d6 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -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 */
 }
 
diff --git a/source/blender/windowmanager/wm_files.h b/source/blender/windowmanager/wm_files.h
index 9fda680..0fa0761 100644
--- a/source/blender/windowmanager/wm_files.h
+++ b/source/blender/windowmanager/wm_files.h
@@ -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);




More information about the Bf-blender-cvs mailing list