[Bf-blender-cvs] [97f667058cb] blender-projects-basics: Set default project name & add default asset library

Julian Eisel noreply at git.blender.org
Thu Oct 13 19:37:20 CEST 2022


Commit: 97f667058cb667c139cad805456bd9c2ee64ce37
Author: Julian Eisel
Date:   Thu Oct 13 18:58:05 2022 +0200
Branches: blender-projects-basics
https://developer.blender.org/rB97f667058cb667c139cad805456bd9c2ee64ce37

Set default project name & add default asset library

Default project name will just be the directory name. The default asset
library will be called "Project Library" and point to an `assets/`
directory inside the project root directory.

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

M	source/blender/blenkernel/BKE_asset_library_custom.h
M	source/blender/blenkernel/BKE_blender_project.h
M	source/blender/blenkernel/BKE_blender_project.hh
M	source/blender/blenkernel/intern/asset_library_custom.cc
M	source/blender/blenkernel/intern/blender_project.cc
M	source/blender/editors/include/ED_project.h
M	source/blender/editors/project/CMakeLists.txt
A	source/blender/editors/project/project.cc
M	source/blender/makesrna/intern/rna_asset.c
M	source/blender/windowmanager/intern/wm_files.c

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

diff --git a/source/blender/blenkernel/BKE_asset_library_custom.h b/source/blender/blenkernel/BKE_asset_library_custom.h
index b065be6ca7c..738729ce957 100644
--- a/source/blender/blenkernel/BKE_asset_library_custom.h
+++ b/source/blender/blenkernel/BKE_asset_library_custom.h
@@ -36,8 +36,9 @@ void BKE_asset_library_custom_name_set(struct ListBase *custom_libraries,
 /**
  * Set the library path, ensuring it is pointing to a directory.
  * Single blend files can only act as "Current File" library; libraries on disk
- * should always be directories. If the path does not exist, that's fine; it can
- * created as directory if necessary later.
+ * should always be directories. Blindly sets the path without additional checks. The asset system
+ * can ignore libraries that it can't resolve to a valid location. If the path does not exist,
+ * that's fine; it can created as directory if necessary later.
  */
 void BKE_asset_library_custom_path_set(struct CustomAssetLibraryDefinition *library,
                                        const char *path) ATTR_NONNULL();
diff --git a/source/blender/blenkernel/BKE_blender_project.h b/source/blender/blenkernel/BKE_blender_project.h
index 143d48a81f1..3a0e0d54d74 100644
--- a/source/blender/blenkernel/BKE_blender_project.h
+++ b/source/blender/blenkernel/BKE_blender_project.h
@@ -38,6 +38,19 @@ bool BKE_project_is_path_project_root(const char *path) ATTR_WARN_UNUSED_RESULT
  * referenced file/directory is a project root directory).
  */
 bool BKE_project_contains_path(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
+/**
+ * Attempt to load project based on the given path and return it. This should never become the
+ * active project, which should be loaded with #BKE_project_active_load_from_path() instead
+ * (because the active project uses unique_ptr without the guarded allocator, unlike this C-API
+ * function). The returned project pointer is owning and needs freeing with #BKE_project_free().
+ */
+BlenderProject *BKE_project_load_from_path(const char *path) ATTR_WARN_UNUSED_RESULT
+    ATTR_NONNULL();
+/**
+ * Free a project allocated by #BKE_project_load_from_path() and null the pointer to it.
+ */
+void BKE_project_free(BlenderProject **project) ATTR_NONNULL();
+
 /**
  * Attempt to load and activate a project based on the given path. If the path doesn't lead
  * into a project, the active project is unset. Note that the project will be unset on any
diff --git a/source/blender/blenkernel/BKE_blender_project.hh b/source/blender/blenkernel/BKE_blender_project.hh
index c4412c8b269..99d76e3a597 100644
--- a/source/blender/blenkernel/BKE_blender_project.hh
+++ b/source/blender/blenkernel/BKE_blender_project.hh
@@ -12,6 +12,8 @@
 #include "BLI_string_ref.hh"
 #include "BLI_utility_mixins.hh"
 
+struct BlenderProject;
+
 namespace blender::io::serialize {
 class DictionaryValue;
 }
@@ -39,10 +41,11 @@ class BlenderProject {
    */
   static auto project_root_path_find_from_path [[nodiscard]] (StringRef path) -> StringRef;
 
+  explicit BlenderProject(std::unique_ptr<ProjectSettings> settings);
+
   auto get_settings [[nodiscard]] () const -> ProjectSettings &;
 
  private:
-  explicit BlenderProject(std::unique_ptr<ProjectSettings> settings);
   static std::unique_ptr<BlenderProject> &active_project_ptr();
 };
 
@@ -83,6 +86,13 @@ class ProjectSettings {
    */
   static auto load_from_disk [[nodiscard]] (StringRef project_path)
   -> std::unique_ptr<ProjectSettings>;
+  /**
+   * Read project settings from the given \a path, which may point to some directory or file inside
+   * of the project directory. Both Unix and Windows style slashes are allowed. Path is expected to
+   * be normalized.
+   * \return The read project settings or null in case of failure.
+   */
+  static auto load_from_path [[nodiscard]] (StringRef path) -> std::unique_ptr<ProjectSettings>;
   /**
    * Write project settings to the given \a project_path, which may be either a project root
    * directory or the .blender_project directory. The .blender_project directory must exist.
diff --git a/source/blender/blenkernel/intern/asset_library_custom.cc b/source/blender/blenkernel/intern/asset_library_custom.cc
index 9627bd8a10b..4e9cb2ae50d 100644
--- a/source/blender/blenkernel/intern/asset_library_custom.cc
+++ b/source/blender/blenkernel/intern/asset_library_custom.cc
@@ -30,6 +30,8 @@
 
 #include "BKE_asset_library_custom.h"
 
+using namespace blender;
+
 /* -------------------------------------------------------------------- */
 /** \name Asset Libraries
  * \{ */
@@ -75,9 +77,6 @@ void BKE_asset_library_custom_name_set(ListBase *custom_libraries,
 void BKE_asset_library_custom_path_set(CustomAssetLibraryDefinition *library, const char *path)
 {
   BLI_strncpy(library->path, path, sizeof(library->path));
-  if (BLI_is_file(library->path)) {
-    BLI_path_parent_dir(library->path);
-  }
 }
 
 CustomAssetLibraryDefinition *BKE_asset_library_custom_find_from_index(
diff --git a/source/blender/blenkernel/intern/blender_project.cc b/source/blender/blenkernel/intern/blender_project.cc
index 1e74d75c368..32631623a09 100644
--- a/source/blender/blenkernel/intern/blender_project.cc
+++ b/source/blender/blenkernel/intern/blender_project.cc
@@ -190,6 +190,8 @@ static std::unique_ptr<ExtractedSettings> extract_settings(
               "Unexpected asset_library entry in settings.json, expected path to be string");
         }
 
+        /* TODO this isn't really extracting, should be creating data from the settings be a
+         * separate step? */
         CustomAssetLibraryDefinition *library = BKE_asset_library_custom_add(
             &extracted_settings->asset_libraries);
         /* Name or path may not be set, this is fine. */
@@ -267,6 +269,16 @@ std::unique_ptr<ProjectSettings> ProjectSettings::load_from_disk(StringRef proje
   return loaded_settings;
 }
 
+std::unique_ptr<ProjectSettings> ProjectSettings::load_from_path(StringRef path)
+{
+  StringRef project_root = bke::BlenderProject::project_root_path_find_from_path(path);
+  if (project_root.is_empty()) {
+    return nullptr;
+  }
+
+  return bke::ProjectSettings::load_from_disk(project_root);
+}
+
 std::unique_ptr<serialize::DictionaryValue> ProjectSettings::to_dictionary() const
 {
   using namespace serialize;
@@ -448,22 +460,36 @@ bool BKE_project_contains_path(const char *path)
   return !found_root_path.is_empty();
 }
 
+BlenderProject *BKE_project_load_from_path(const char *path)
+{
+  std::unique_ptr<bke::ProjectSettings> project_settings = bke::ProjectSettings::load_from_path(
+      path);
+  if (!project_settings) {
+    return nullptr;
+  }
+
+  return reinterpret_cast<BlenderProject *>(
+      MEM_new<bke::BlenderProject>(__func__, std::move(project_settings)));
+}
+
+void BKE_project_free(BlenderProject **project_handle)
+{
+  bke::BlenderProject *project = reinterpret_cast<bke::BlenderProject *>(*project_handle);
+  BLI_assert_msg(project != bke::BlenderProject::get_active(),
+                 "Projects loaded with #BKE_project_load_from_path() must never be set active.");
+
+  MEM_delete(project);
+  *project_handle = nullptr;
+}
+
 BlenderProject *BKE_project_active_load_from_path(const char *path)
 {
   /* Project should be unset if the path doesn't contain a project root. Unset in the beginning so
    * early exiting behaves correctly. */
   BKE_project_active_unset();
 
-  StringRef project_root = bke::BlenderProject::project_root_path_find_from_path(path);
-  if (project_root.is_empty()) {
-    return nullptr;
-  }
-
-  std::unique_ptr project_settings = bke::ProjectSettings::load_from_disk(project_root);
-  if (!project_settings) {
-    return nullptr;
-  }
-
+  std::unique_ptr<bke::ProjectSettings> project_settings = bke::ProjectSettings::load_from_path(
+      path);
   bke::BlenderProject::set_active_from_settings(std::move(project_settings));
 
   return BKE_project_active_get();
diff --git a/source/blender/editors/include/ED_project.h b/source/blender/editors/include/ED_project.h
index ddf5c795a1b..673701855c3 100644
--- a/source/blender/editors/include/ED_project.h
+++ b/source/blender/editors/include/ED_project.h
@@ -12,8 +12,14 @@
 extern "C" {
 #endif
 
+struct BlenderProject;
+
 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);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/editors/project/CMakeLists.txt b/source/blender/editors/project/CMakeLists.txt
index 2343250dafb..fd85c307f06 100644
--- a/source/blender/editors/project/CMakeLists.txt
+++ b/source/blender/editors/project/CMakeLists.txt
@@ -6,6 +6,7 @@ set(INC
   ../include
   ../../blenkernel
   ../../blenlib
+  ../../blentranslation
   ../../makesdna
   ../../makesrna
   ../../windowmanager
@@ -16,6 +17,7 @@ set(INC_SYS
 )
 
 set(SRC
+    project.cc
     project_ops.cc
 )
 
diff --git a/source/blender/editors/project/project.cc b/source/blender/editors/project/project.cc
new file mode 100644
index 00000000000..3cdf2109ad5
--- /dev/null
+++ b/source/blender/editors/project/project.cc
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup edproject
+ */
+
+#include "BLI_path_util.h"
+#include "BLI_string.h"
+#include "BLI_string_ref.hh"
+
+#include "BLT_translation.h"
+
+#include "BKE_asset_library_custom.h"
+#include "BKE_blender_project.h"
+
+#include "ED_project.h"
+
+using namespace blender;
+
+/** Name of the asset library added by default. Needs translation with `DATA_()`. */
+inline static const char *DEFAULT_ASSET_LIBRARY_NAME = N_("Project Library");
+inline static const char *DEFAULT_ASSET_LIBRARY_PATH = "//assets/";
+
+void ED_project_set_defaults(BlenderProject *project)
+{
+  char project_root_dir[FILE_MAXDIR];
+  BLI_strncpy(project_root_dir, BKE_project_root_path_get(project), sizeof(project_root_dir));
+
+  /* Set directory name as default project name. */
+  char dirname[FILE_MAXFILE];
+  BLI_path_slash_rstrip(project_root_dir);
+  BLI_split_file_part(project_root_dir, dirname, sizeof(dirname));
+  BKE_project_name_set(project, 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list