[Bf-blender-cvs] [c32b7bfe549] blender-projects-basics: Add "New Project" operator to select a directory to use as project root

Julian Eisel noreply at git.blender.org
Thu Sep 29 15:48:53 CEST 2022


Commit: c32b7bfe54976d426aa5f959d09bd83993ec49fa
Author: Julian Eisel
Date:   Thu Sep 29 15:47:41 2022 +0200
Branches: blender-projects-basics
https://developer.blender.org/rBc32b7bfe54976d426aa5f959d09bd83993ec49fa

Add "New Project" operator to select a directory to use as project root

Name and exact design of this is pending still, they are not optimal.

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

M	release/scripts/startup/bl_ui/space_topbar.py
M	source/blender/blenkernel/BKE_blender_project.h
M	source/blender/blenkernel/intern/blender_project.cc
M	source/blender/windowmanager/intern/wm_files.c
M	source/blender/windowmanager/intern/wm_operators.c
M	source/blender/windowmanager/wm_files.h

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

diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index da089ea23b0..aedd9dfb41f 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -270,11 +270,13 @@ class TOPBAR_MT_file(Menu):
 
         layout.operator_context = 'INVOKE_AREA'
         layout.menu("TOPBAR_MT_file_new", text="New", icon='FILE_NEW')
-        layout.operator("wm.open_mainfile", text="Open...", icon='FILE_FOLDER')
+        layout.operator("wm.open_mainfile", text="Open File...", icon='FILE_FOLDER')
         layout.menu("TOPBAR_MT_file_open_recent")
         layout.operator("wm.revert_mainfile")
         layout.menu("TOPBAR_MT_file_recover")
 
+        layout.operator("wm.new_project", text="New Project...")
+
         layout.separator()
 
         layout.operator_context = 'EXEC_AREA' if context.blend_data.is_saved else 'INVOKE_AREA'
diff --git a/source/blender/blenkernel/BKE_blender_project.h b/source/blender/blenkernel/BKE_blender_project.h
index 8fdda3e18ea..fb4ca90a184 100644
--- a/source/blender/blenkernel/BKE_blender_project.h
+++ b/source/blender/blenkernel/BKE_blender_project.h
@@ -17,6 +17,9 @@ extern "C" {
 /* C-handle for #bke::BlenderProject. */
 typedef struct BlenderProject BlenderProject;
 
+/** See #bke::ProjectSettings::create_settings_directory(). */
+bool BKE_project_create_settings_directory(const char *project_root_path) ATTR_NONNULL();
+
 BlenderProject *BKE_project_active_get(void) ATTR_WARN_UNUSED_RESULT;
 /**
  * \note: When unsetting an active project, the previously active one will be destroyed, so
diff --git a/source/blender/blenkernel/intern/blender_project.cc b/source/blender/blenkernel/intern/blender_project.cc
index 8016b78fd81..51f44bcd032 100644
--- a/source/blender/blenkernel/intern/blender_project.cc
+++ b/source/blender/blenkernel/intern/blender_project.cc
@@ -136,6 +136,11 @@ StringRefNull ProjectSettings::project_root_path() const
 
 using namespace blender;
 
+bool BKE_project_create_settings_directory(const char *project_root_path)
+{
+  return bke::ProjectSettings::create_settings_directory(project_root_path);
+}
+
 BlenderProject *BKE_project_active_get(void)
 {
   return reinterpret_cast<BlenderProject *>(bke::BlenderProject::get_active());
diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index 92844dddf4c..7dd61195982 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -62,6 +62,7 @@
 #include "BKE_asset_library.h"
 #include "BKE_autoexec.h"
 #include "BKE_blender.h"
+#include "BKE_blender_project.h"
 #include "BKE_blendfile.h"
 #include "BKE_callbacks.h"
 #include "BKE_context.h"
@@ -3283,6 +3284,81 @@ void WM_OT_save_mainfile(wmOperatorType *ot)
 
 /** \} */
 
+/* -------------------------------------------------------------------- */
+/** \name New project operator
+ * \{ */
+
+static int wm_new_project_exec(bContext *C, wmOperator *op)
+{
+  const Main *bmain = CTX_data_main(C);
+  const char *blendfile_path = BKE_main_blendfile_path(bmain);
+
+  if (!RNA_struct_property_is_set(op->ptr, "directory")) {
+    BKE_report(op->reports, RPT_ERROR, "No path defined for creating a new project in");
+    return OPERATOR_CANCELLED;
+  }
+  char project_root_dir[FILE_MAXDIR];
+  RNA_string_get(op->ptr, "directory", project_root_dir);
+
+  if (!BKE_project_create_settings_directory(project_root_dir)) {
+    BKE_reportf(op->reports,
+                RPT_ERROR,
+                "Failed to create project with unknown error. Is the directory read-only?");
+    return OPERATOR_CANCELLED;
+  }
+
+  BKE_reportf(op->reports, RPT_INFO, "Project created and loaded successfully");
+
+  if (blendfile_path[0] && BLI_path_contains(project_root_dir, blendfile_path)) {
+    BKE_project_active_load_from_path(blendfile_path);
+
+    /* Update the window title. */
+    WM_event_add_notifier_ex(CTX_wm_manager(C), CTX_wm_window(C), NC_WM | ND_DATACHANGED, NULL);
+  }
+  else {
+    BKE_reportf(op->reports,
+                RPT_INFO,
+                "The current file is not located inside of the new project. This means the new "
+                "project is not active");
+  }
+
+  return OPERATOR_FINISHED;
+}
+
+static int wm_new_project_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
+{
+  const Main *bmain = CTX_data_main(C);
+  const char *blendfile_path = BKE_main_blendfile_path(bmain);
+  if (blendfile_path[0]) {
+    /* Open at the .blend file location if any. */
+    RNA_string_set(op->ptr, "filepath", blendfile_path);
+  }
+
+  WM_event_add_fileselect(C, op);
+  return OPERATOR_RUNNING_MODAL;
+}
+
+void WM_OT_new_project(wmOperatorType *ot)
+{
+  ot->name = "New Project";
+  ot->idname = "WM_OT_new_project";
+  ot->description = "Choose a directory to use as the root of a project";
+
+  ot->invoke = wm_new_project_invoke;
+  ot->exec = wm_new_project_exec;
+  /* omit window poll so this can work in background mode */
+
+  WM_operator_properties_filesel(ot,
+                                 FILE_TYPE_FOLDER,
+                                 FILE_BLENDER,
+                                 FILE_SAVE,
+                                 WM_FILESEL_DIRECTORY,
+                                 FILE_DEFAULTDISPLAY,
+                                 FILE_SORT_DEFAULT);
+}
+
+/** \} */
+
 /* -------------------------------------------------------------------- */
 /** \name Auto Script Execution Warning Dialog
  * \{ */
diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c
index 58791dbd00a..73cc40fe0d6 100644
--- a/source/blender/windowmanager/intern/wm_operators.c
+++ b/source/blender/windowmanager/intern/wm_operators.c
@@ -3756,6 +3756,7 @@ void wm_operatortypes_register(void)
   WM_operatortype_append(WM_OT_recover_auto_save);
   WM_operatortype_append(WM_OT_save_as_mainfile);
   WM_operatortype_append(WM_OT_save_mainfile);
+  WM_operatortype_append(WM_OT_new_project);
   WM_operatortype_append(WM_OT_redraw_timer);
   WM_operatortype_append(WM_OT_memory_statistics);
   WM_operatortype_append(WM_OT_debug_menu);
diff --git a/source/blender/windowmanager/wm_files.h b/source/blender/windowmanager/wm_files.h
index 1c09f861c6c..28cff9961a9 100644
--- a/source/blender/windowmanager/wm_files.h
+++ b/source/blender/windowmanager/wm_files.h
@@ -104,6 +104,8 @@ void WM_OT_recover_auto_save(struct wmOperatorType *ot);
 void WM_OT_save_as_mainfile(struct wmOperatorType *ot);
 void WM_OT_save_mainfile(struct wmOperatorType *ot);
 
+void WM_OT_new_project(struct wmOperatorType *ot);
+
 /* wm_files_link.c */
 
 void WM_OT_link(struct wmOperatorType *ot);



More information about the Bf-blender-cvs mailing list