[Bf-blender-cvs] [adaf4f56e1e] master: Support loading catalogs in the Current File asset library

Julian Eisel noreply at git.blender.org
Wed Sep 29 13:20:19 CEST 2021


Commit: adaf4f56e1ed2d8ff55be4681838c9705da022ad
Author: Julian Eisel
Date:   Wed Sep 29 13:18:44 2021 +0200
Branches: master
https://developer.blender.org/rBadaf4f56e1ed2d8ff55be4681838c9705da022ad

Support loading catalogs in the Current File asset library

When the Asset Browser shows the "Current File" asset library, now it
also attempts to load an asset catalog definition file from location of
the current .blend file. This happens as follows:
* First, see if the file is inside of an asset library that is "mounted" in the
  Preferences. Load the catalogs from there if so.
* Otherwise, if the file is saved, load the catalogs from the directory the
  file is saved in.
* If the file is not saved, no catalogs will be loaded.

Unit tests are being worked on in D12689.

Creating catalogs from the "Current File" asset library still doesn't work, as
the asset catalog service doesn't construct an in-memory catalog definition
file in that case yet.

Differential Revision: https://developer.blender.org/D12675

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

M	source/blender/blenkernel/BKE_asset_library.h
M	source/blender/blenkernel/intern/asset_catalog.cc
M	source/blender/blenkernel/intern/asset_library.cc
M	source/blender/editors/space_file/filelist.c

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

diff --git a/source/blender/blenkernel/BKE_asset_library.h b/source/blender/blenkernel/BKE_asset_library.h
index 709b915f9ff..04486a7c132 100644
--- a/source/blender/blenkernel/BKE_asset_library.h
+++ b/source/blender/blenkernel/BKE_asset_library.h
@@ -20,6 +20,8 @@
 
 #pragma once
 
+struct Main;
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -31,6 +33,41 @@ typedef struct AssetLibrary AssetLibrary;
 struct AssetLibrary *BKE_asset_library_load(const char *library_path);
 void BKE_asset_library_free(struct AssetLibrary *asset_library);
 
+/**
+ * Try to find an appropriate location for an asset library root from a file or directory path.
+ * Does not check if \a input_path exists.
+ *
+ * The design is made to find an appropriate asset library path from a .blend file path, but
+ * technically works with any file or directory as \a input_path.
+ * Design is:
+ * * If \a input_path lies within a known asset library path (i.e. an asset library registered in
+ *   the Preferences), return the asset library path.
+ * * Otherwise, if \a input_path has a parent path, return the parent path (e.g. to use the
+ *   directory a .blend file is in as asset library root).
+ * * If \a input_path is empty or doesn't have a parent path (e.g. because a .blend wasn't saved
+ *   yet), there is no suitable path. The caller has to decide how to handle this case.
+ *
+ * \param r_library_path: The returned asset library path with a trailing slash, or an empty string
+ *                        if no suitable path is found. Assumed to be a buffer of at least
+ *                        #FILE_MAXDIR bytes.
+ *
+ * \return True if the function could find a valid, that is, a non-empty path to return in \a
+ *         r_library_path.
+ */
+bool BKE_asset_library_find_suitable_root_path_from_path(
+    const char *input_path, char r_library_path[768 /* FILE_MAXDIR */]);
+/**
+ * Uses the current location on disk of the file represented by \a bmain as input to
+ * #BKE_asset_library_find_suitable_root_path_from_path(). Refer to it for a design
+ * description.
+ *
+ * \return True if the function could find a valid, that is, a non-empty path to return in \a
+ *         r_library_path. If \a bmain wasn't saved into a file yet, the return value will be
+ *         false.
+ */
+bool BKE_asset_library_find_suitable_root_path_from_main(
+    const struct Main *bmain, char r_library_path[768 /* FILE_MAXDIR */]);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenkernel/intern/asset_catalog.cc b/source/blender/blenkernel/intern/asset_catalog.cc
index d54e3b93dda..b00f4305aa6 100644
--- a/source/blender/blenkernel/intern/asset_catalog.cc
+++ b/source/blender/blenkernel/intern/asset_catalog.cc
@@ -19,6 +19,7 @@
  */
 
 #include "BKE_asset_catalog.hh"
+#include "BKE_asset_library.h"
 #include "BKE_preferences.h"
 
 #include "BLI_fileops.h"
@@ -299,6 +300,10 @@ bool AssetCatalogService::write_to_disk_on_blendfile_save(const CatalogFilePath
 CatalogFilePath AssetCatalogService::find_suitable_cdf_path_for_writing(
     const CatalogFilePath &blend_file_path)
 {
+  BLI_assert_msg(!blend_file_path.empty(),
+                 "A non-empty .blend file path is required to be able to determine where the "
+                 "catalog definition file should be put");
+
   /* Determine the default CDF path in the same directory of the blend file. */
   char blend_dir_path[PATH_MAX];
   BLI_split_dir_part(blend_file_path.c_str(), blend_dir_path, sizeof(blend_dir_path));
@@ -311,26 +316,19 @@ CatalogFilePath AssetCatalogService::find_suitable_cdf_path_for_writing(
     return cdf_path_next_to_blend;
   }
 
-  const bUserAssetLibrary *asset_lib_pref = BKE_preferences_asset_library_containing_path(
-      &U, blend_file_path.c_str());
-  if (asset_lib_pref) {
-    /* - The directory containing the blend file is part of an asset library, as per
-     *   the user's preferences?
-     *    -> Merge with & write to ${ASSET_LIBRARY_ROOT}/blender_assets.cats.txt  */
-
-    char asset_lib_cdf_path[PATH_MAX];
-    BLI_path_join(asset_lib_cdf_path,
-                  sizeof(asset_lib_cdf_path),
-                  asset_lib_pref->path,
-                  DEFAULT_CATALOG_FILENAME.c_str(),
-                  NULL);
-
-    return asset_lib_cdf_path;
-  }
+  /* - There's no definition file next to the .blend file.
+   *    -> Ask the asset library API for an appropriate location.  */
+  char suitable_root_path[PATH_MAX];
+  BKE_asset_library_find_suitable_root_path_from_path(blend_file_path.c_str(),
+                                                          suitable_root_path);
+  char asset_lib_cdf_path[PATH_MAX];
+  BLI_path_join(asset_lib_cdf_path,
+                sizeof(asset_lib_cdf_path),
+                suitable_root_path,
+                DEFAULT_CATALOG_FILENAME.c_str(),
+                NULL);
 
-  /* - Otherwise
-   *    -> Create a new file blender_assets.cats.txt next to the blend file. */
-  return cdf_path_next_to_blend;
+  return asset_lib_cdf_path;
 }
 
 std::unique_ptr<AssetCatalogDefinitionFile> AssetCatalogService::construct_cdf_in_memory(
diff --git a/source/blender/blenkernel/intern/asset_library.cc b/source/blender/blenkernel/intern/asset_library.cc
index 8d38f2106c1..1086efe45fd 100644
--- a/source/blender/blenkernel/intern/asset_library.cc
+++ b/source/blender/blenkernel/intern/asset_library.cc
@@ -21,6 +21,11 @@
 #include "BKE_asset_library.hh"
 #include "BKE_callbacks.h"
 #include "BKE_main.h"
+#include "BKE_preferences.h"
+
+#include "BLI_path_util.h"
+
+#include "DNA_userdef_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -45,6 +50,24 @@ void BKE_asset_library_free(struct AssetLibrary *asset_library)
   delete lib;
 }
 
+bool BKE_asset_library_find_suitable_root_path_from_path(const char *input_path,
+                                                         char *r_library_path)
+{
+  if (bUserAssetLibrary *preferences_lib = BKE_preferences_asset_library_containing_path(
+          &U, input_path)) {
+    BLI_strncpy(r_library_path, preferences_lib->path, FILE_MAXDIR);
+    return true;
+  }
+
+  BLI_split_dir_part(input_path, r_library_path, FILE_MAXDIR);
+  return r_library_path[0] != '\0';
+}
+
+bool BKE_asset_library_find_suitable_root_path_from_main(const Main *bmain, char *r_library_path)
+{
+  return BKE_asset_library_find_suitable_root_path_from_path(bmain->name, r_library_path);
+}
+
 namespace blender::bke {
 
 void AssetLibrary::load(StringRefNull library_root_directory)
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 194e577e19e..60fe5364aba 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -3398,15 +3398,34 @@ static void filelist_readjob_lib(FileListReadJob *job_params,
   filelist_readjob_do(true, job_params, stop, do_update, progress);
 }
 
+static void filelist_asset_library_path(const FileListReadJob *job_params,
+                                        char r_library_root_path[FILE_MAX])
+{
+  if (job_params->filelist->type == FILE_MAIN_ASSET) {
+    /* For the "Current File" library (#FILE_MAIN_ASSET) we get the asset library root path based
+     * on main. */
+    BKE_asset_library_find_suitable_root_path_from_main(job_params->current_main,
+                                                        r_library_root_path);
+  }
+  else {
+    BLI_strncpy(r_library_root_path, job_params->tmp_filelist->filelist.root, FILE_MAX);
+  }
+}
+
+/**
+ * Load asset library data, which currently means loading the asset catalogs for the library.
+ */
 static void filelist_readjob_load_asset_library_data(FileListReadJob *job_params, short *do_update)
 {
   FileList *tmp_filelist = job_params->tmp_filelist; /* Use the thread-safe filelist queue. */
 
-  /* Check whether assets catalogs need to be loaded. */
   if (job_params->filelist->asset_library_ref != NULL) {
+    char library_root_path[FILE_MAX];
+    filelist_asset_library_path(job_params, library_root_path);
+
     /* Load asset catalogs, into the temp filelist for thread-safety.
      * #filelist_readjob_endjob() will move it into the real filelist. */
-    tmp_filelist->asset_library = BKE_asset_library_load(tmp_filelist->filelist.root);
+    tmp_filelist->asset_library = BKE_asset_library_load(library_root_path);
     *do_update = true;
   }
 }



More information about the Bf-blender-cvs mailing list