[Bf-blender-cvs] [45bb7d7013f] asset-browser: Support showing local assets in Asset Browsers ("Local" repository)

Julian Eisel noreply at git.blender.org
Wed Sep 30 17:21:40 CEST 2020


Commit: 45bb7d7013fcbbf2c57a757e0442f31b2060cfc8
Author: Julian Eisel
Date:   Wed Sep 30 17:04:20 2020 +0200
Branches: asset-browser
https://developer.blender.org/rB45bb7d7013fcbbf2c57a757e0442f31b2060cfc8

Support showing local assets in Asset Browsers ("Local" repository)

Adds a new repository "Local" (will probably rename this in a bit, together
with other UI improvements).

The asset browser shows the assets in the current Blender session then. Note
that there's a difference between the data saved in the file, and the current
session's data - the file may have unsaved changes.
So this new repository acts on the current runtime "main" data, not on a file
stored on disk.

Note that the Asset Browser still has to refreshed or reopened when
creating/deleting assets, so that the changes are reflected.

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

M	source/blender/blenkernel/BKE_asset.h
M	source/blender/blenkernel/BKE_icons.h
M	source/blender/blenkernel/intern/asset.c
M	source/blender/blenkernel/intern/icons.c
M	source/blender/blenlib/intern/path_util.c
M	source/blender/editors/asset/asset_ops.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_file/space_file.c
M	source/blender/imbuf/intern/thumbs_blend.c
M	source/blender/makesdna/DNA_space_types.h
M	source/blender/makesrna/intern/rna_space.c

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

diff --git a/source/blender/blenkernel/BKE_asset.h b/source/blender/blenkernel/BKE_asset.h
index bb81a05921c..a4a794691a3 100644
--- a/source/blender/blenkernel/BKE_asset.h
+++ b/source/blender/blenkernel/BKE_asset.h
@@ -29,6 +29,8 @@ extern "C" {
 
 struct BlendWriter;
 struct BlendDataReader;
+struct ID;
+struct PreviewImage;
 
 struct AssetData *BKE_asset_data_create(void);
 void BKE_asset_data_free(struct AssetData *asset_data);
@@ -43,6 +45,9 @@ struct CustomTagEnsureResult BKE_assetdata_tag_ensure(struct AssetData *asset_da
                                                       const char *name);
 void BKE_assetdata_tag_remove(struct AssetData *asset_data, struct CustomTag *tag);
 
+struct PreviewImage *BKE_assetdata_preview_get_from_id(const struct AssetData *asset_data,
+                                                       const struct ID *owner_id);
+
 void BKE_assetdata_write(struct BlendWriter *writer, struct AssetData *asset_data);
 void BKE_assetdata_read(struct BlendDataReader *reader, struct AssetData *asset_data);
 
diff --git a/source/blender/blenkernel/BKE_icons.h b/source/blender/blenkernel/BKE_icons.h
index c52fac4fb01..dd622a75c83 100644
--- a/source/blender/blenkernel/BKE_icons.h
+++ b/source/blender/blenkernel/BKE_icons.h
@@ -147,6 +147,8 @@ struct PreviewImage *BKE_previewimg_id_ensure(struct ID *id);
 
 void BKE_previewimg_ensure(struct PreviewImage *prv, const int size);
 
+struct ImBuf *BKE_previewimg_to_imbuf(struct PreviewImage *prv, const int size);
+
 struct PreviewImage *BKE_previewimg_cached_get(const char *name);
 
 struct PreviewImage *BKE_previewimg_cached_ensure(const char *name);
diff --git a/source/blender/blenkernel/intern/asset.c b/source/blender/blenkernel/intern/asset.c
index 0f62ee6df1d..7950adee2f4 100644
--- a/source/blender/blenkernel/intern/asset.c
+++ b/source/blender/blenkernel/intern/asset.c
@@ -80,6 +80,13 @@ void BKE_assetdata_tag_remove(AssetData *asset_data, CustomTag *tag)
   BLI_freelinkN(&asset_data->tags, tag);
 }
 
+/* Queries -------------------------------------------- */
+
+PreviewImage *BKE_assetdata_preview_get_from_id(const AssetData *UNUSED(asset_data), const ID *id)
+{
+  return BKE_previewimg_id_get(id);
+}
+
 /* .blend file API -------------------------------------------- */
 
 void BKE_assetdata_write(BlendWriter *writer, AssetData *asset_data)
diff --git a/source/blender/blenkernel/intern/icons.c b/source/blender/blenkernel/intern/icons.c
index 8c9b5ab6ab2..730e6b6d285 100644
--- a/source/blender/blenkernel/intern/icons.c
+++ b/source/blender/blenkernel/intern/icons.c
@@ -523,6 +523,27 @@ void BKE_previewimg_ensure(PreviewImage *prv, const int size)
   }
 }
 
+/**
+ * Create an #ImBuf holding a copy of the preview image buffer in \a prv.
+ * \note The returned image buffer has to be free'd (#IMB_freeImBuf()).
+ */
+ImBuf *BKE_previewimg_to_imbuf(PreviewImage *prv, const int size)
+{
+  const unsigned int w = prv->w[size];
+  const unsigned int h = prv->h[size];
+  const unsigned int *rect = prv->rect[size];
+
+  ImBuf *ima = NULL;
+
+  if (w > 0 && h > 0 && rect) {
+    /* first allocate imbuf for copying preview into it */
+    ima = IMB_allocImBuf(w, h, 32, IB_rect);
+    memcpy(ima->rect, rect, w * h * sizeof(*ima->rect));
+  }
+
+  return ima;
+}
+
 void BKE_previewimg_blend_write(BlendWriter *writer, const PreviewImage *prv)
 {
   /* Note we write previews also for undo steps. It takes up some memory,
diff --git a/source/blender/blenlib/intern/path_util.c b/source/blender/blenlib/intern/path_util.c
index 6328c887063..09879d6645d 100644
--- a/source/blender/blenlib/intern/path_util.c
+++ b/source/blender/blenlib/intern/path_util.c
@@ -265,6 +265,10 @@ void BLI_path_normalize(const char *relabase, char *path)
  */
 void BLI_path_normalize_dir(const char *relabase, char *dir)
 {
+  /* Would just create an unexpected "/" path, just early exit entirely. */
+  if (dir[0] == '\0') {
+    return;
+  }
   BLI_path_normalize(relabase, dir);
   BLI_path_slash_ensure(dir);
 }
diff --git a/source/blender/editors/asset/asset_ops.c b/source/blender/editors/asset/asset_ops.c
index 16fbf57fdf5..4188d4ba1d7 100644
--- a/source/blender/editors/asset/asset_ops.c
+++ b/source/blender/editors/asset/asset_ops.c
@@ -58,7 +58,8 @@ static int asset_make_exec(bContext *C, wmOperator *op)
 
   UI_icon_render_id(C, NULL, id, true, false);
   /* Store reference to the ID's preview. */
-  id->asset_data->preview = BKE_previewimg_id_get(id);
+  /* XXX get rid of this? File read will be a hassle and no real need for it right now. */
+  id->asset_data->preview = BKE_assetdata_preview_get_from_id(id->asset_data, id);
 
   BKE_reportf(op->reports, RPT_INFO, "Data-block '%s' is now an asset", id->name + 2);
 
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index fc10ec020f0..f03351ce928 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -38,6 +38,7 @@
 
 #include "BKE_context.h"
 #include "BKE_main.h"
+#include "BKE_main_idmap.h"
 
 #include "BLO_readfile.h"
 
@@ -134,6 +135,7 @@ static void draw_tile(int sx, int sy, int width, int height, int colorid, int sh
 }
 
 static void file_draw_icon(uiBlock *block,
+                           const struct FileList *files,
                            const FileDirEntry *file,
                            const char *rootpath,
                            const char *path,
@@ -159,7 +161,13 @@ static void file_draw_icon(uiBlock *block,
   UI_but_func_tooltip_set(but, file_draw_tooltip_func, BLI_strdup(path));
 
   if (drag) {
-    if (file->typeflag & FILE_TYPE_ASSET) {
+    /* TODO duplicated from file_draw_preview(). */
+    ID *id;
+
+    if ((id = filelist_file_get_id(files, file))) {
+      UI_but_drag_set_id(but, id);
+    }
+    else if (file->typeflag & FILE_TYPE_ASSET) {
       char *rootpath_cpy = BLI_strdup(rootpath);
       BLI_path_slash_rstrip(rootpath_cpy);
       UI_but_drag_set_asset(
@@ -220,6 +228,7 @@ void file_calc_previews(const bContext *C, ARegion *region)
 }
 
 static void file_draw_preview(uiBlock *block,
+                              const struct FileList *files,
                               const FileDirEntry *file,
                               const char *rootpath,
                               const char *path,
@@ -396,8 +405,13 @@ static void file_draw_preview(uiBlock *block,
 
   /* dragregion */
   if (drag) {
+    ID *id;
+
+    if ((id = filelist_file_get_id(files, file))) {
+      UI_but_drag_set_id(but, id);
+    }
     /* path is no more static, cannot give it directly to but... */
-    if (file->typeflag & FILE_TYPE_ASSET) {
+    else if (file->typeflag & FILE_TYPE_ASSET) {
       char *rootpath_cpy = BLI_strdup(rootpath);
       BLI_path_slash_rstrip(rootpath_cpy);
       UI_but_drag_set_asset(but, file->name, rootpath_cpy, file->blentype, icon, imb, scale);
@@ -838,6 +852,7 @@ void file_draw_list(const bContext *C, ARegion *region)
       }
 
       file_draw_preview(block,
+                        files,
                         file,
                         root,
                         path,
@@ -854,6 +869,7 @@ void file_draw_list(const bContext *C, ARegion *region)
     }
     else {
       file_draw_icon(block,
+                     files,
                      file,
                      root,
                      path,
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index b3f9e8b6ef6..e23d5ffc704 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -928,6 +928,7 @@ void FILE_OT_select_all(wmOperatorType *ot)
 
 /* Note we could get rid of this one, but it's used by some addon so...
  * Does not hurt keeping it around for now. */
+/* TODO disallow bookmark editing in assets mode? */
 static int bookmark_select_exec(bContext *C, wmOperator *op)
 {
   Main *bmain = CTX_data_main(C);
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index 7e5cf1fd386..b83b7bc24d9 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -53,13 +53,16 @@
 #  include "BLI_winstuff.h"
 #endif
 
+#include "BKE_asset.h"
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_icons.h"
 #include "BKE_idtype.h"
 #include "BKE_main.h"
+#include "BKE_main_idmap.h"
 #include "BLO_readfile.h"
 
+#include "DNA_asset_types.h"
 #include "DNA_space_types.h"
 
 #include "ED_datafiles.h"
@@ -117,6 +120,10 @@ void folderlist_popdir(struct ListBase *folderlist, char *dir)
 
 void folderlist_pushdir(ListBase *folderlist, const char *dir)
 {
+  if (!dir[0]) {
+    return;
+  }
+
   struct FolderList *folder, *previous_folder;
   previous_folder = folderlist->last;
 
@@ -215,6 +222,12 @@ typedef struct FileListInternEntry {
   /** not strictly needed, but used during sorting, avoids to have to recompute it there... */
   char *name;
 
+  /** When showing local IDs (FILE_MAIN, FILE_MAIN_ASSET), UUID of the ID this file represents. */
+  uint id_uuid;
+  /* For the few file types that have the preview already in memory. For others, there's delayed
+   * preview reading from disk. Non-owning pointer. */
+  PreviewImage *preview_image;
+
   /** Defined in BLI_fileops.h */
   eFileAttributes attributes;
   BLI_stat_t st;
@@ -295,6 +308,8 @@ enum {
 typedef struct FileList {
   FileDirEntryArr filelist;
 
+  eFileSelectType type;
+
   short prv_w;
   short prv_h;
 
@@ -316,6 +331,12 @@ typedef struct FileList {
    */
   GHash *selection_state;
 
+  /* If the file browser shows local IDs (FILE_MAIN, FILE_MAIN_ASSET), this is needed to keep safe
+   * references (safe over deletion or undo/redo) to the IDs. */
+  /* TODO could we use something more lighweight? Something that only stores IDs that are actually
+   * used. */
+  struct IDNameLib_Map *id_map;
+
   short max_recursion;
   short recursion_level;
 
@@ -327,7 +348,8 @@ typedef struct FileList {
   bool (*checkdirf)(struct FileList *, char *, const bool);
 
   /* Fill filelist (to be called by read job). */
-  void (*read_jobf)(struct FileList *, cons

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list