[Bf-blender-cvs] [6177df9dce5] blender-projects-basics: Support displaying project asset libraries in asset browser

Julian Eisel noreply at git.blender.org
Thu Oct 13 21:33:32 CEST 2022


Commit: 6177df9dce5d94b8b8832a476d9a3b84188d1334
Author: Julian Eisel
Date:   Thu Oct 13 21:31:20 2022 +0200
Branches: blender-projects-basics
https://developer.blender.org/rB6177df9dce5d94b8b8832a476d9a3b84188d1334

Support displaying project asset libraries in asset browser

The hint in the asset browser for when the selected library path doesn't
exists is updated to mention the project settings too, and there is a
button to open the project settings in the asset library section.

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

M	source/blender/editors/asset/ED_asset_library.h
M	source/blender/editors/asset/intern/asset_library_reference.cc
M	source/blender/editors/asset/intern/asset_library_reference_enum.cc
M	source/blender/editors/asset/intern/asset_list.cc
M	source/blender/editors/screen/screen_ops.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/filelist.cc
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_node/add_node_search.cc
M	source/blender/editors/space_node/link_drag_search.cc
M	source/blender/makesdna/DNA_asset_types.h

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

diff --git a/source/blender/editors/asset/ED_asset_library.h b/source/blender/editors/asset/ED_asset_library.h
index cc0d97054b6..c5f526e18d5 100644
--- a/source/blender/editors/asset/ED_asset_library.h
+++ b/source/blender/editors/asset/ED_asset_library.h
@@ -34,6 +34,8 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value);
 const struct EnumPropertyItem *ED_asset_library_reference_to_rna_enum_itemf(
     bool include_local_library);
 
+struct CustomAssetLibraryDefinition *ED_asset_library_find_custom_library_from_reference(
+    const AssetLibraryReference *library_ref);
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/editors/asset/intern/asset_library_reference.cc b/source/blender/editors/asset/intern/asset_library_reference.cc
index 5096b9d653d..2c454b70119 100644
--- a/source/blender/editors/asset/intern/asset_library_reference.cc
+++ b/source/blender/editors/asset/intern/asset_library_reference.cc
@@ -4,8 +4,15 @@
  * \ingroup edasset
  */
 
+#include "BKE_asset_library_custom.h"
+#include "BKE_blender_project.h"
+#include "BKE_context.h"
+
+#include "DNA_userdef_types.h"
+
 #include "BLI_hash.hh"
 
+#include "ED_asset_library.h"
 #include "asset_library_reference.hh"
 
 namespace blender::ed::asset {
@@ -18,14 +25,15 @@ AssetLibraryReferenceWrapper::AssetLibraryReferenceWrapper(const AssetLibraryRef
 bool operator==(const AssetLibraryReferenceWrapper &a, const AssetLibraryReferenceWrapper &b)
 {
   return (a.type == b.type) &&
-         ((a.type == ASSET_LIBRARY_CUSTOM) ? (a.custom_library_index == b.custom_library_index) :
-                                             true);
+         (ELEM(a.type, ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES, ASSET_LIBRARY_CUSTOM_FROM_PROJECT) ?
+              (a.custom_library_index == b.custom_library_index) :
+              true);
 }
 
 uint64_t AssetLibraryReferenceWrapper::hash() const
 {
   uint64_t hash1 = DefaultHash<decltype(type)>{}(type);
-  if (type != ASSET_LIBRARY_CUSTOM) {
+  if (!ELEM(type, ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES, ASSET_LIBRARY_CUSTOM_FROM_PROJECT)) {
     return hash1;
   }
 
@@ -34,3 +42,24 @@ uint64_t AssetLibraryReferenceWrapper::hash() const
 }
 
 }  // namespace blender::ed::asset
+
+CustomAssetLibraryDefinition *ED_asset_library_find_custom_library_from_reference(
+    const AssetLibraryReference *library_ref)
+{
+  switch (library_ref->type) {
+    case ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES:
+      return BKE_asset_library_custom_find_from_index(&U.asset_libraries,
+                                                      library_ref->custom_library_index);
+    case ASSET_LIBRARY_CUSTOM_FROM_PROJECT: {
+      BlenderProject *project = CTX_wm_project();
+      if (project) {
+        return BKE_asset_library_custom_find_from_index(
+            BKE_project_custom_asset_libraries_get(project), library_ref->custom_library_index);
+      }
+    }
+    case ASSET_LIBRARY_LOCAL:
+      return nullptr;
+  }
+
+  return nullptr;
+}
diff --git a/source/blender/editors/asset/intern/asset_library_reference_enum.cc b/source/blender/editors/asset/intern/asset_library_reference_enum.cc
index c680bf45e39..7d75dc83854 100644
--- a/source/blender/editors/asset/intern/asset_library_reference_enum.cc
+++ b/source/blender/editors/asset/intern/asset_library_reference_enum.cc
@@ -12,6 +12,8 @@
 #include "BLI_listbase.h"
 
 #include "BKE_asset_library_custom.h"
+#include "BKE_blender_project.h"
+#include "BKE_context.h"
 
 #include "DNA_userdef_types.h"
 
@@ -23,17 +25,15 @@
 
 int ED_asset_library_reference_to_enum_value(const AssetLibraryReference *library)
 {
-  /* Simple case: Predefined repository, just set the value. */
-  if (library->type < ASSET_LIBRARY_CUSTOM) {
+  /* Simple case: Predefined library, just set the value. */
+  if (library->type < ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES) {
     return library->type;
   }
 
   /* Note that the path isn't checked for validity here. If an invalid library path is used, the
    * Asset Browser can give a nice hint on what's wrong. */
-  const CustomAssetLibraryDefinition *user_library = BKE_asset_library_custom_find_from_index(
-      &U.asset_libraries, library->custom_library_index);
-  if (user_library) {
-    return ASSET_LIBRARY_CUSTOM + library->custom_library_index;
+  if (ED_asset_library_find_custom_library_from_reference(library)) {
+    return library->type + library->custom_library_index;
   }
 
   return ASSET_LIBRARY_LOCAL;
@@ -44,32 +44,63 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value)
   AssetLibraryReference library;
 
   /* Simple case: Predefined repository, just set the value. */
-  if (value < ASSET_LIBRARY_CUSTOM) {
+  if (value < ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES) {
     library.type = value;
     library.custom_library_index = -1;
     BLI_assert(ELEM(value, ASSET_LIBRARY_LOCAL));
     return library;
   }
 
-  const CustomAssetLibraryDefinition *user_library = BKE_asset_library_custom_find_from_index(
-      &U.asset_libraries, value - ASSET_LIBRARY_CUSTOM);
+  const eAssetLibraryType type = (value < ASSET_LIBRARY_CUSTOM_FROM_PROJECT) ?
+                                     ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES :
+                                     ASSET_LIBRARY_CUSTOM_FROM_PROJECT;
 
-  /* Note that there is no check if the path exists here. If an invalid library path is used, the
-   * Asset Browser can give a nice hint on what's wrong. */
-  if (!user_library) {
-    library.type = ASSET_LIBRARY_LOCAL;
-    library.custom_library_index = -1;
-  }
-  else {
-    const bool is_valid = (user_library->name[0] && user_library->path[0]);
-    if (is_valid) {
-      library.custom_library_index = value - ASSET_LIBRARY_CUSTOM;
-      library.type = ASSET_LIBRARY_CUSTOM;
+  const CustomAssetLibraryDefinition *custom_library = nullptr;
+
+  library.type = type;
+  library.custom_library_index = value - type;
+
+  {
+    custom_library = ED_asset_library_find_custom_library_from_reference(&library);
+
+    /* Note that there is no check if the path exists here. If an invalid library path is used, the
+     * Asset Browser can give a nice hint on what's wrong. */
+    const bool is_valid = custom_library && (custom_library->name[0] && custom_library->path[0]);
+    if (!is_valid) {
+      library.custom_library_index = -1;
     }
   }
+
   return library;
 }
 
+static void add_custom_asset_library_enum_items(
+    const ListBase * /*CustomAssetLibraryDefinition*/ libraries,
+    const eAssetLibraryType library_type,
+    EnumPropertyItem **items,
+    int *totitem)
+{
+  int i;
+  LISTBASE_FOREACH_INDEX (CustomAssetLibraryDefinition *, custom_library, libraries, i) {
+    /* Note that the path itself isn't checked for validity here. If an invalid library path is
+     * used, the Asset Browser can give a nice hint on what's wrong. */
+    const bool is_valid = (custom_library->name[0] && custom_library->path[0]);
+    if (!is_valid) {
+      continue;
+    }
+
+    AssetLibraryReference library_reference;
+    library_reference.type = library_type;
+    library_reference.custom_library_index = i;
+
+    const int enum_value = ED_asset_library_reference_to_enum_value(&library_reference);
+    /* Use library path as description, it's a nice hint for users. */
+    EnumPropertyItem tmp = {
+        enum_value, custom_library->name, ICON_NONE, custom_library->name, custom_library->path};
+    RNA_enum_item_add(items, totitem, &tmp);
+  }
+}
+
 const EnumPropertyItem *ED_asset_library_reference_to_rna_enum_itemf(
     const bool include_local_library)
 {
@@ -92,29 +123,21 @@ const EnumPropertyItem *ED_asset_library_reference_to_rna_enum_itemf(
     RNA_enum_items_add(&item, &totitem, predefined_items);
   }
 
-  /* Add separator if needed. */
-  if (!BLI_listbase_is_empty(&U.asset_libraries)) {
+  BlenderProject *project = CTX_wm_project();
+  if (project && !BLI_listbase_is_empty(BKE_project_custom_asset_libraries_get(project))) {
     RNA_enum_item_add_separator(&item, &totitem);
-  }
 
-  int i;
-  LISTBASE_FOREACH_INDEX (CustomAssetLibraryDefinition *, user_library, &U.asset_libraries, i) {
-    /* Note that the path itself isn't checked for validity here. If an invalid library path is
-     * used, the Asset Browser can give a nice hint on what's wrong. */
-    const bool is_valid = (user_library->name[0] && user_library->path[0]);
-    if (!is_valid) {
-      continue;
-    }
+    add_custom_asset_library_enum_items(BKE_project_custom_asset_libraries_get(project),
+                                        ASSET_LIBRARY_CUSTOM_FROM_PROJECT,
+                                        &item,
+                                        &totitem);
+  }
 
-    AssetLibraryReference library_reference;
-    library_reference.type = ASSET_LIBRARY_CUSTOM;
-    library_reference.custom_library_index = i;
+  if (!BLI_listbase_is_empty(&U.asset_libraries)) {
+    RNA_enum_item_add_separator(&item, &totitem);
 
-    const int enum_value = ED_asset_library_reference_to_enum_value(&library_reference);
-    /* Use library path as description, it's a nice hint for users. */
-    EnumPropertyItem tmp = {
-        enum_value, user_library->name, ICON_NONE, user_library->name, user_library->path};
-    RNA_enum_item_add(&item, &totitem, &tmp);
+    add_custom_asset_library_enum_items(
+        &U.asset_libraries, ASSET_LIBRARY_CUSTOM_FROM_PREFERENCES, &item, &totitem);
   }
 
   RNA_enum_item_end(&item, &totitem);
diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc
index 4fff2191713..719c8747173 100644
--- a/source/blender/editors/asset/intern/asset_list.cc
+++ b/source/blender/editors/asset/intern/asset_list.cc
@@ -13,6 +13,7 @@
 #include <string>
 
 #include "BKE_asset_library_custom.h"
+#include "BKE_blender_project.h"
 #include "BKE_context.h"
 
 #include "BLI_map.hh"
@@ -31,6 +32,7 @@
 
 #include "ED_asset_handle.h"
 #include "ED_asset_indexer.h"
+#include "ED_asset_library.h"
 #include "ED_asset_list.h"
 #include "ED_asset_list.hh"
 #include "asset_library_reference.hh"
@@ -130,15 +132,8 @@ void AssetList::setup()
 {
   FileList *files = filelist_;
 
-  CustomAssetLibraryDefinition *user_library = nullptr;
-
-  /*

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list