[Bf-blender-cvs] [8ea0551d367] blender-projects-basics: Cleanup: Move operators & operator logic to editors/project

Julian Eisel noreply at git.blender.org
Fri Oct 14 15:18:37 CEST 2022


Commit: 8ea0551d36719b3c277541b655e3de2f75def480
Author: Julian Eisel
Date:   Fri Oct 14 15:17:23 2022 +0200
Branches: blender-projects-basics
https://developer.blender.org/rB8ea0551d36719b3c277541b655e3de2f75def480

Cleanup: Move operators & operator logic to editors/project

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

M	release/scripts/startup/bl_ui/space_project_settings.py
M	release/scripts/startup/bl_ui/space_topbar.py
M	source/blender/editors/include/ED_project.h
M	source/blender/editors/project/project.cc
M	source/blender/editors/project/project_ops.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_project_settings.py b/release/scripts/startup/bl_ui/space_project_settings.py
index 06114b846cc..4327ddedc68 100644
--- a/release/scripts/startup/bl_ui/space_project_settings.py
+++ b/release/scripts/startup/bl_ui/space_project_settings.py
@@ -21,7 +21,7 @@ class PROJECTSETTINGS_HT_header(Header):
 
         # Show '*' to let users know the settings have been modified.
         layout.operator(
-            "wm.save_project_settings",
+            "project.save_settings",
             text=iface_("Save Settings") + (" *" if is_dirty else ""),
             translate=False,
         )
@@ -135,7 +135,7 @@ class PROJECTSETTINGS_PT_no_project(Panel):
 
         row = layout.row()
         split = row.split(factor=0.3)
-        split.operator("wm.new_project", text="Set up Project...")
+        split.operator("project.new", text="Set up Project...")
 
 
 class PROJECTSETTINGS_PT_setup(CenterAlignMixIn, Panel):
diff --git a/release/scripts/startup/bl_ui/space_topbar.py b/release/scripts/startup/bl_ui/space_topbar.py
index 43a37e22f14..180050c7aa5 100644
--- a/release/scripts/startup/bl_ui/space_topbar.py
+++ b/release/scripts/startup/bl_ui/space_topbar.py
@@ -278,7 +278,7 @@ class TOPBAR_MT_file(Menu):
         layout.operator("wm.revert_mainfile")
         layout.menu("TOPBAR_MT_file_recover")
 
-        props = layout.operator("wm.new_project", text="Set up Project...")
+        props = layout.operator("project.new", text="Set up Project...")
         props.open_settings_after = True
 
         layout.separator()
diff --git a/source/blender/editors/include/ED_project.h b/source/blender/editors/include/ED_project.h
index 673701855c3..6a78f964ca2 100644
--- a/source/blender/editors/include/ED_project.h
+++ b/source/blender/editors/include/ED_project.h
@@ -13,12 +13,27 @@ extern "C" {
 #endif
 
 struct BlenderProject;
+struct Main;
+struct ReportList;
 
 void ED_operatortypes_project(void);
 
 /** Sets the project name to the directory name it is located in and registers a "Project Library"
  * asset library pointing to `//assets/`. */
 void ED_project_set_defaults(struct BlenderProject *project);
+/**
+ * High level project creation (create project + load if needed + write default settings if
+ * needed).
+ *
+ * Initializes a new project in \a project_root_dir by creating the `.blender_project/` directory
+ * there. The new project will only be loaded if \a bmain represents a file within the project
+ * directory.
+ *
+ * \return True on success.
+ */
+bool ED_project_new(const struct Main *bmain,
+                    const char *project_root_dir,
+                    struct ReportList *reports);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/editors/project/project.cc b/source/blender/editors/project/project.cc
index 3cdf2109ad5..8e31e344b45 100644
--- a/source/blender/editors/project/project.cc
+++ b/source/blender/editors/project/project.cc
@@ -12,6 +12,12 @@
 
 #include "BKE_asset_library_custom.h"
 #include "BKE_blender_project.h"
+#include "BKE_context.h"
+#include "BKE_main.h"
+#include "BKE_report.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
 
 #include "ED_project.h"
 
@@ -36,3 +42,48 @@ void ED_project_set_defaults(BlenderProject *project)
   BKE_asset_library_custom_add(
       libraries, DATA_(DEFAULT_ASSET_LIBRARY_NAME), DEFAULT_ASSET_LIBRARY_PATH);
 }
+
+bool ED_project_new(const Main *bmain, const char *project_root_dir, ReportList *reports)
+{
+  if (!BKE_project_create_settings_directory(project_root_dir)) {
+    BKE_reportf(reports,
+                RPT_ERROR,
+                "Failed to create project with unknown error. Is the directory read-only?");
+    return false;
+  }
+
+  BKE_reportf(reports, RPT_INFO, "Project created and loaded successfully");
+
+  const char *blendfile_path = BKE_main_blendfile_path(bmain);
+
+  bool activated_new_project = false;
+  if (blendfile_path[0] && BLI_path_contains(project_root_dir, blendfile_path)) {
+    if (BKE_project_active_load_from_path(blendfile_path)) {
+      activated_new_project = true;
+    }
+  }
+  else {
+    BKE_reportf(reports,
+                RPT_WARNING,
+                "The current file is not located inside of the new project. This means the new "
+                "project is not active");
+  }
+
+  /* Some default settings for the project. It has to be loaded for that. */
+  BlenderProject *new_project = activated_new_project ?
+                                    CTX_wm_project() :
+                                    BKE_project_load_from_path(project_root_dir);
+  if (new_project) {
+    ED_project_set_defaults(new_project);
+
+    /* Write defaults to the hard drive. */
+    BKE_project_settings_save(new_project);
+
+    /* We just temporary loaded the project, free it again. */
+    if (!activated_new_project) {
+      BKE_project_free(&new_project);
+    }
+  }
+
+  return true;
+}
diff --git a/source/blender/editors/project/project_ops.cc b/source/blender/editors/project/project_ops.cc
index 9f77e306176..c0ae738f2f0 100644
--- a/source/blender/editors/project/project_ops.cc
+++ b/source/blender/editors/project/project_ops.cc
@@ -9,9 +9,11 @@
 #include "BKE_asset_library_custom.h"
 #include "BKE_blender_project.h"
 #include "BKE_context.h"
+#include "BKE_main.h"
 #include "BKE_report.h"
 
 #include "BLI_path_util.h"
+#include "BLT_translation.h"
 
 #include "DNA_space_types.h"
 
@@ -22,9 +24,155 @@
 #include "WM_types.h"
 
 #include "ED_project.h"
+#include "ED_screen.h"
 
 using namespace blender;
 
+static bool has_active_project_poll(bContext *C)
+{
+  const BlenderProject *active_project = CTX_wm_project();
+  CTX_wm_operator_poll_msg_set(C, TIP_("No active project loaded"));
+  return active_project != NULL;
+}
+
+/* -------------------------------------------------------------------- */
+/** \name New project operator
+ * \{ */
+
+static int new_project_exec(bContext *C, wmOperator *op)
+{
+  const Main *bmain = CTX_data_main(C);
+
+  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 (!ED_project_new(bmain, project_root_dir, op->reports)) {
+    return OPERATOR_CANCELLED;
+  }
+
+  PropertyRNA *prop_open_settings = RNA_struct_find_property(op->ptr, "open_settings_after");
+  if (RNA_property_is_set(op->ptr, prop_open_settings) &&
+      RNA_property_boolean_get(op->ptr, prop_open_settings)) {
+    ED_project_settings_window_show(C, op->reports);
+  }
+
+  WM_main_add_notifier(NC_PROJECT, NULL);
+  /* Update the window title. */
+  WM_main_add_notifier(NC_WM | ND_DATACHANGED, NULL);
+
+  return OPERATOR_FINISHED;
+}
+
+static int 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, "directory", blendfile_path);
+  }
+
+  WM_event_add_fileselect(C, op);
+  return OPERATOR_RUNNING_MODAL;
+}
+
+static void PROJECT_OT_new(wmOperatorType *ot)
+{
+  ot->name = "New Project";
+  ot->idname = "PROJECT_OT_new";
+  ot->description = "Choose a directory to use as the root of a project";
+
+  ot->invoke = new_project_invoke;
+  ot->exec = new_project_exec;
+  /* omit window poll so this can work in background mode */
+
+  WM_operator_properties_filesel(ot,
+                                 FILE_TYPE_FOLDER,
+                                 FILE_BLENDER,
+                                 FILE_OPENFILE,
+                                 WM_FILESEL_DIRECTORY,
+                                 FILE_DEFAULTDISPLAY,
+                                 FILE_SORT_DEFAULT);
+
+  PropertyRNA *prop;
+  prop = RNA_def_boolean(ot->srna,
+                         "open_settings_after",
+                         false,
+                         "",
+                         "Open the project settings window after successfully creating a project");
+  RNA_def_property_flag(prop, PROP_HIDDEN);
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Write Project Settings Operator
+ * \{ */
+
+static int save_settings_exec(bContext *UNUSED(C), wmOperator *UNUSED(op))
+{
+  BlenderProject *active_project = CTX_wm_project();
+
+  if (!BKE_project_settings_save(active_project)) {
+    return OPERATOR_CANCELLED;
+  }
+
+  return OPERATOR_FINISHED;
+}
+
+static void PROJECT_OT_save_settings(wmOperatorType *ot)
+{
+  ot->name = "Save Project Settings";
+  ot->idname = "PROJECT_OT_save_settings";
+  ot->description = "Make the current changes to the project settings permanent";
+
+  ot->invoke = WM_operator_confirm;
+  ot->poll = has_active_project_poll;
+  ot->exec = save_settings_exec;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Delete project setup operator
+ * \{ */
+
+static int delete_project_setup_exec(bContext *C, wmOperator *op)
+{
+  if (!BKE_project_delete_settings_directory(CTX_wm_project())) {
+    BKE_report(op->reports,
+               RPT_ERROR,
+               "Failed to delete project settings. Is the project directory read-only?");
+    return OPERATOR_CANCELLED;
+  }
+  BKE_project_active_unset();
+
+  WM_main_add_notifier(NC_PROJECT, NULL);
+  /* Update the window title. */
+  WM_event_add_notifier_ex(CTX_wm_manager(C), CTX_wm_window(C), NC_WM | ND_DATACHANGED, NULL);
+
+  return OPERATOR_FINISHED;
+}
+
+static void PROJECT_OT_delete_setup(wmOperatorType *ot)
+{
+  ot->name = "Delete Project Setup";
+  ot->idname = "PROJECT_OT_delete_setup";
+  ot->description =
+      "Remove the configuration of the current project with all settings, but keep project files "
+      "(such as .blend files) untouched";
+
+  ot->invoke = WM_operator_confirm;
+  ot->exec = delete_project_setup_exec;
+  /* omit window poll so this can work in background mode */
+  ot->poll = has_active_project_poll;
+}
+
+/** \} */
 /* -------------------------------------

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list