[Bf-blender-cvs] [83d4832a85] app-templates: Support both factory and user startup for templates

Campbell Barton noreply at git.blender.org
Wed Mar 22 23:56:52 CET 2017


Commit: 83d4832a8551a388f1f9de2adc507c2e28257f65
Author: Campbell Barton
Date:   Thu Mar 23 07:24:12 2017 +1100
Branches: app-templates
https://developer.blender.org/rB83d4832a8551a388f1f9de2adc507c2e28257f65

Support both factory and user startup for templates

This supports a split between factory and user startups for templates
(as we have for default startup.blend)

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

M	release/scripts/startup/bl_ui/space_info.py
M	release/scripts/startup/bl_ui/space_userpref.py
M	source/blender/windowmanager/intern/wm_files.c

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

diff --git a/release/scripts/startup/bl_ui/space_info.py b/release/scripts/startup/bl_ui/space_info.py
index 5077995319..489fb27f14 100644
--- a/release/scripts/startup/bl_ui/space_info.py
+++ b/release/scripts/startup/bl_ui/space_info.py
@@ -126,6 +126,16 @@ class INFO_MT_file(Menu):
         layout.operator_context = 'INVOKE_AREA'
         layout.operator("wm.save_homefile", icon='SAVE_PREFS')
         layout.operator("wm.read_factory_settings", icon='LOAD_FACTORY')
+
+        app_template = context.user_preferences.app_template
+        if app_template:
+            layout.operator(
+                "wm.read_factory_settings",
+                text="Load Template Settings",
+                icon='LOAD_FACTORY',
+            ).app_template = app_template
+        del app_template
+
         layout.menu("USERPREF_MT_app_templates", icon='FILE_BLEND')
 
         layout.separator()
diff --git a/release/scripts/startup/bl_ui/space_userpref.py b/release/scripts/startup/bl_ui/space_userpref.py
index f50e9f3c87..86ff255dab 100644
--- a/release/scripts/startup/bl_ui/space_userpref.py
+++ b/release/scripts/startup/bl_ui/space_userpref.py
@@ -105,31 +105,27 @@ class USERPREF_MT_app_templates(Menu):
         if use_default:
             props = layout.operator("wm.read_homefile", text="Default")
             props.use_splash = True
-            props.use_template = True
+            props.app_template = ""
             layout.separator()
 
         template_paths = bpy.utils.app_template_paths()
 
         # expand template paths
-        template_paths_expand = []
+        app_templates = []
         for path in template_paths:
             for d in os.listdir(path):
                 template = os.path.join(path, d)
                 if os.path.isdir(template):
-                    template_paths_expand.append(template)
-
-        # Would use Menu.path_menu, except we need a little more control
-        self.path_menu(
-            template_paths_expand,
-            "wm.read_homefile",
-            props_default={
-                "use_splash": use_splash,
-                "use_template": True,
-            },
-            filter_path=lambda path: os.path.basename(path) == "startup.blend",
-            # name of directory the file is in.
-            display_name=lambda f: bpy.path.display_name(f.rsplit(os.sep, 2)[-2]),
-        )
+                    # template_paths_expand.append(template)
+                    app_templates.append(d)
+
+        for d in sorted(app_templates):
+            props = layout.operator(
+                "wm.read_homefile",
+                text=bpy.path.display_name(d),
+            )
+            props.use_splash = True
+            props.app_template = d;
 
     def draw(self, context):
         self.draw_ex(context, use_splash=False, use_default=True)
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 4e8a6c2409..2b9d8d9a3f 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -421,20 +421,6 @@ void WM_file_autoexec_init(const char *filepath)
 	}
 }
 
-static bool wm_file_template_from_path(char app_template[sizeof(U.app_template)], const char *filepath)
-{
-	int offset, len;
-	if (BLI_path_name_at_index(filepath, -2, &offset, &len)) {
-		if (len < sizeof(U.app_template)) {
-			memcpy(app_template, &filepath[offset], len);
-			app_template[len] = '\0';
-			printf("APP '%s'\n", app_template);
-			return true;
-		}
-	}
-	return false;
-}
-
 void wm_file_read_report(bContext *C)
 {
 	ReportList *reports = NULL;
@@ -651,6 +637,7 @@ bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
  * \param from_memory: Ignore on-disk startup file, use bundled ``datatoc_startup_blend`` instead.
  * Used for "Restore Factory Settings".
  * \param filepath_startup_override: Optional path pointing to an alternative blend file (may be NULL).
+ * \param custom_app_template: Template to use instead of the template defined in user-preferences.
  */
 int wm_homefile_read(
         bContext *C, ReportList *reports, bool from_memory,
@@ -660,7 +647,7 @@ int wm_homefile_read(
 	char filepath_startup[FILE_MAX];
 	char filepath_userdef[FILE_MAX];
 	bool success = false;
-
+	bool force_load_startup = false;
 	/* Indicates whether user preferences were really load from memory.
 	 *
 	 * This is used for versioning code, and for this we can not rely on from_memory
@@ -685,19 +672,10 @@ int wm_homefile_read(
 	UI_view2d_zoom_cache_reset();
 
 	G.relbase_valid = 0;
+
 	if (!from_memory) {
 		const char * const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
-		if (filepath_startup_override) {
-			BLI_strncpy(filepath_startup, filepath_startup_override, FILE_MAX);
-
-			if (cfgdir) {
-				BLI_make_file_string("/", filepath_userdef, cfgdir, BLENDER_USERPREF_FILE);
-			}
-			else {
-				filepath_userdef[0] = '\0';
-			}
-		}
-		else if (cfgdir) {
+		if (cfgdir) {
 			BLI_make_file_string("/", filepath_startup, cfgdir, BLENDER_STARTUP_FILE);
 			BLI_make_file_string("/", filepath_userdef, cfgdir, BLENDER_USERPREF_FILE);
 		}
@@ -706,6 +684,10 @@ int wm_homefile_read(
 			filepath_userdef[0] = '\0';
 			from_memory = true;
 		}
+
+		if (filepath_startup_override) {
+			BLI_strncpy(filepath_startup, filepath_startup_override, FILE_MAX);
+		}
 	}
 	
 	/* put aside screens to match with persistent windows later */
@@ -721,12 +703,40 @@ int wm_homefile_read(
 			read_userdef_from_memory = false;
 			skip_flags |= BLO_READ_SKIP_USERDEF;
 			printf("Read prefs: %s\n", filepath_userdef);
+
 		}
 	}
 
-	if (!from_memory) {
+	/* insert template name into startup file */
+	if (!filepath_startup_override) {
+		const char *app_template =
+		        custom_app_template ? custom_app_template :
+		                              ((!from_memory && U.app_template[0]) ? U.app_template : NULL);
+		if (app_template != NULL) {
+			if (!from_memory) {
+				BLI_path_suffix(filepath_startup, sizeof(filepath_startup), app_template, "_");
+			}
+
+			if (from_memory || BLI_access(filepath_startup, R_OK)) {
+				char temp_path[FILE_MAX];
+				BLI_join_dirfile(temp_path, sizeof(temp_path), "app_templates", U.app_template);
+				const char *filepath_startup_template = BKE_appdir_folder_id(BLENDER_DATAFILES, temp_path);
+				if (filepath_startup_template) {
+					BLI_join_dirfile(
+					        filepath_startup, sizeof(filepath_startup),
+					        filepath_startup_template, BLENDER_STARTUP_FILE);
+					force_load_startup = true;
+				}
+			}
+		}
+	}
+
+	if (!from_memory || force_load_startup) {
 		if (BLI_access(filepath_startup, R_OK) == 0) {
 			success = (BKE_blendfile_read(C, filepath_startup, NULL, skip_flags) != BKE_BLENDFILE_READ_FAIL);
+			if (success) {
+				printf("Read startup: %s\n", filepath_startup);
+			}
 		}
 		if (BLI_listbase_is_empty(&U.themes)) {
 			if (G.debug & G_DEBUG)
@@ -1331,6 +1341,10 @@ static int wm_homefile_write_exec(bContext *C, wmOperator *op)
 	WM_keyconfig_update(wm);
 
 	BLI_make_file_string("/", filepath, BKE_appdir_folder_id_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
+	if (U.app_template[0]) {
+		BLI_path_suffix(filepath, sizeof(filepath), U.app_template, "_");
+	}
+
 	printf("trying to save homefile at %s ", filepath);
 
 	ED_editors_flush_edits(C, false);
@@ -1485,16 +1499,12 @@ static int wm_homefile_read_exec(bContext *C, wmOperator *op)
 
 	char app_template_buf[sizeof(U.app_template)];
 	const char *app_template;
-	const bool is_app_template = !from_memory && RNA_boolean_get(op->ptr, "use_template");
+	/* const bool is_app_template = !from_memory && RNA_boolean_get(op->ptr, "app_template"); */
+	PropertyRNA *prop_app_template = RNA_struct_find_property(op->ptr, "app_template");
 	const bool use_splash = !from_memory && RNA_boolean_get(op->ptr, "use_splash");
 
-	if (is_app_template) {
-		if ((filepath != NULL) && wm_file_template_from_path(app_template_buf, filepath)) {
-			/* pass */
-		}
-		else {
-			app_template_buf[0] = '\0';
-		}
+	if (prop_app_template && RNA_property_is_set(op->ptr, prop_app_template)) {
+		RNA_property_string_get(op->ptr, prop_app_template, app_template_buf);
 		app_template = app_template_buf;
 	}
 	else {
@@ -1536,7 +1546,7 @@ void WM_OT_read_homefile(wmOperatorType *ot)
 	prop = RNA_def_boolean(ot->srna, "use_splash", false, "Splash", "");
 	RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 
-	prop = RNA_def_boolean(ot->srna, "use_template", false, "Template", "");
+	prop = RNA_def_string(ot->srna, "app_template", "Template", sizeof(U.app_template), "", "");
 	RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
 
 	/* omit poll to run in background mode */
@@ -1544,12 +1554,18 @@ void WM_OT_read_homefile(wmOperatorType *ot)
 
 void WM_OT_read_factory_settings(wmOperatorType *ot)
 {
+	PropertyRNA *prop;
+
 	ot->name = "Load Factory Settings";
 	ot->idname = "WM_OT_read_factory_settings";
 	ot->description = "Load default file and user preferences";
 
 	ot->invoke = WM_operator_confirm;
 	ot->exec = wm_homefile_read_exec;
+
+	prop = RNA_def_string(ot->srna, "app_template", "Template", sizeof(U.app_template), "", "");
+	RNA_def_property_flag(prop, PROP_HIDDEN | PROP_SKIP_SAVE);
+
 	/* omit poll to run in background mode */
 }




More information about the Bf-blender-cvs mailing list