Assets: bundle essentials with Blender #104474

Merged
Jacques Lucke merged 29 commits from JacquesLucke/blender:bundled-assets into main 2023-02-14 17:35:41 +01:00
14 changed files with 116 additions and 12 deletions

View File

@ -67,6 +67,9 @@ class AssetLibrary {
std::unique_ptr<AssetCatalogService> catalog_service;
/** Assets owned by this library should never be linked. */
bool never_link = false;
friend class AssetLibraryService;
public:

View File

@ -22,6 +22,8 @@ const char *AS_asset_representation_name_get(const AssetRepresentation *asset)
AssetMetaData *AS_asset_representation_metadata_get(const AssetRepresentation *asset)
ATTR_WARN_UNUSED_RESULT;
bool AS_asset_representation_is_local_id(const AssetRepresentation *asset) ATTR_WARN_UNUSED_RESULT;
bool AS_asset_representation_is_never_link(const AssetRepresentation *asset)
Review

Personally I prefer is_never_link to make clear this is getting, not setting.

Personally I prefer `is_never_link` to make clear this is getting, not setting.
ATTR_WARN_UNUSED_RESULT;
#ifdef __cplusplus
}

View File

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#pragma once
#include "BLI_string_ref.hh"
namespace blender::asset_system {
StringRefNull essentials_directory_path();
}

View File

@ -17,6 +17,7 @@ set(SRC
intern/asset_catalog.cc
intern/asset_catalog_path.cc
intern/asset_catalog_tree.cc
intern/asset_essentials_library.cc
intern/asset_identifier.cc
intern/asset_library.cc
intern/asset_library_service.cc
@ -30,6 +31,7 @@ set(SRC
AS_asset_identifier.hh
AS_asset_library.hh
AS_asset_representation.hh
AS_essentials_library.hh
intern/asset_library_service.hh
intern/asset_storage.hh
intern/utils.hh

View File

@ -0,0 +1,24 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup asset_system
*/
#include "BLI_path_util.h"
#include "BKE_appdir.h"
#include "AS_essentials_library.hh"
namespace blender::asset_system {
StringRefNull essentials_directory_path()
{
static std::string path = []() {
const char *datafiles_path = BKE_appdir_folder_id(BLENDER_DATAFILES, "assets");
return datafiles_path;
}();
return path;
}
} // namespace blender::asset_system

View File

@ -260,6 +260,12 @@ StringRefNull AssetLibrary::root_path() const
Vector<AssetLibraryReference> all_valid_asset_library_refs()
{
Vector<AssetLibraryReference> result;
{
AssetLibraryReference library_ref{};
library_ref.custom_library_index = -1;
library_ref.type = ASSET_LIBRARY_ESSENTIALS;
result.append(library_ref);
}
int i;
LISTBASE_FOREACH_INDEX (const bUserAssetLibrary *, asset_library, &U.asset_libraries, i) {
if (!BLI_is_dir(asset_library->path)) {

View File

@ -16,6 +16,7 @@
#include "AS_asset_catalog_tree.hh"
#include "AS_asset_library.hh"
#include "AS_essentials_library.hh"
#include "asset_library_service.hh"
#include "utils.hh"
@ -60,6 +61,12 @@ AssetLibrary *AssetLibraryService::get_asset_library(
const eAssetLibraryType type = eAssetLibraryType(library_reference.type);
switch (type) {
case ASSET_LIBRARY_ESSENTIALS: {
const StringRefNull root_path = essentials_directory_path();
AssetLibrary *asset_library = get_asset_library_on_disk(root_path);
asset_library->never_link = true;
return asset_library;
}
case ASSET_LIBRARY_LOCAL: {
/* For the "Current File" library we get the asset library root path based on main. */
std::string root_path = bmain ? AS_asset_library_find_suitable_root_path_from_main(bmain) :

View File

@ -10,6 +10,7 @@
#include "DNA_asset_types.h"
#include "AS_asset_identifier.hh"
#include "AS_asset_library.hh"
#include "AS_asset_representation.h"
#include "AS_asset_representation.hh"
@ -126,4 +127,11 @@ bool AS_asset_representation_is_local_id(const AssetRepresentation *asset_handle
return asset->is_local_id();
}
bool AS_asset_representation_is_never_link(const AssetRepresentation *asset_handle)
{
const asset_system::AssetRepresentation *asset =
reinterpret_cast<const asset_system::AssetRepresentation *>(asset_handle);
return asset->owner_asset_library().never_link;
}
/** \} */

View File

@ -47,7 +47,7 @@ AssetLibraryReference ED_asset_library_reference_from_enum_value(int value)
if (value < ASSET_LIBRARY_CUSTOM) {
library.type = value;
library.custom_library_index = -1;
BLI_assert(ELEM(value, ASSET_LIBRARY_ALL, ASSET_LIBRARY_LOCAL));
BLI_assert(ELEM(value, ASSET_LIBRARY_ALL, ASSET_LIBRARY_LOCAL, ASSET_LIBRARY_ESSENTIALS));
return library;
}
@ -87,6 +87,11 @@ const EnumPropertyItem *ED_asset_library_reference_to_rna_enum_itemf(const bool
ICON_CURRENT_FILE,
"Current File",
"Show the assets currently available in this Blender session"},
{ASSET_LIBRARY_ESSENTIALS,
"ESSENTIALS",
ICON_NONE,
"Essentials",
"Show the basic building blocks and utilities coming with Blender"},
{0, nullptr, 0, nullptr, nullptr},
};

View File

@ -372,6 +372,7 @@ std::optional<eFileSelectType> AssetListStorage::asset_library_reference_to_file
switch (eAssetLibraryType(library_reference.type)) {
case ASSET_LIBRARY_ALL:
return FILE_ASSET_LIBRARY_ALL;
case ASSET_LIBRARY_ESSENTIALS:
case ASSET_LIBRARY_CUSTOM:
return FILE_ASSET_LIBRARY;
case ASSET_LIBRARY_LOCAL:

View File

@ -55,6 +55,8 @@
#include "GPU_immediate_util.h"
#include "GPU_state.h"
#include "AS_asset_representation.h"
#include "filelist.h"
#include "file_intern.h" /* own include */
@ -126,6 +128,19 @@ static void draw_tile_background(const rcti *draw_rect, int colorid, int shade)
UI_draw_roundbox_aa(&draw_rect_fl, true, 5.0f, color);
}
static eFileAssetImportType get_asset_import_type(const SpaceFile *sfile, const FileDirEntry *file)
{
const FileAssetSelectParams *asset_params = ED_fileselect_get_asset_params(sfile);
JacquesLucke marked this conversation as resolved
Review

Doing this kind of path comparison is a bit meh, and it assumes assets are files on disk. Not a big deal but I think we can do it properly right away.

I'd suggest storing a bool never_link_ inside asset_system::AssetLibrary that is set in the get_asset_library() switch. And then here you can do something like:

const FileList *filelist = filelist_files_ensure(sfile->files);
if (AS_asset_library_is_never_link(filelist->asset_library)) {
  return FILE_ASSET_IMPORT_APPEND_REUSE;
}
Doing this kind of path comparison is a bit meh, and it assumes assets are files on disk. Not a big deal but I think we can do it properly right away. I'd suggest storing a `bool never_link_` inside `asset_system::AssetLibrary` that is set in the `get_asset_library()` switch. And then here you can do something like: ```c++ const FileList *filelist = filelist_files_ensure(sfile->files); if (AS_asset_library_is_never_link(filelist->asset_library)) { return FILE_ASSET_IMPORT_APPEND_REUSE; } ```
Review

I liked the idea, but it didn't really work unfortunately, because e.g. "All" is also an asset library but it contains assets that can be linked and those that must not be linked. So I did not address this feedback yet.

I liked the idea, but it didn't really work unfortunately, because e.g. "All" is also an asset library but it contains assets that can be linked and those that must not be linked. So I did not address this feedback yet.
Review

I thought it would be fine since during loading you have access to the nested library, but I missed that here, where you need to do the query, you can only access the "All" one.
I'd suggest to let AssetRepresentation hold a reference to the owning library, so that the info can be queried through the library. I think that makes most sense longer term.

I thought it would be fine since during loading you have access to the nested library, but I missed that here, where you need to do the query, you can only access the "All" one. I'd suggest to let `AssetRepresentation` hold a reference to the owning library, so that the info can be queried through the library. I think that makes most sense longer term.
BLI_assert(asset_params != NULL);
if (asset_params->import_type != FILE_ASSET_IMPORT_LINK) {
return asset_params->import_type;
}
if (AS_asset_representation_is_never_link(file->asset)) {
return FILE_ASSET_IMPORT_APPEND_REUSE;
}
return FILE_ASSET_IMPORT_LINK;
}
static void file_draw_icon(const SpaceFile *sfile,
uiBlock *block,
const FileDirEntry *file,
@ -165,13 +180,10 @@ static void file_draw_icon(const SpaceFile *sfile,
ImBuf *preview_image = filelist_file_getimage(file);
char blend_path[FILE_MAX_LIBEXTRA];
if (BLO_library_path_explode(path, blend_path, NULL, NULL)) {
const FileAssetSelectParams *asset_params = ED_fileselect_get_asset_params(sfile);
BLI_assert(asset_params != NULL);
UI_but_drag_set_asset(but,
&(AssetHandle){.file_data = file},
BLI_strdup(blend_path),
asset_params->import_type,
get_asset_import_type(sfile, file),
icon,
preview_image,
UI_DPI_FAC);
@ -558,13 +570,10 @@ static void file_draw_preview(const SpaceFile *sfile,
char blend_path[FILE_MAX_LIBEXTRA];
if (BLO_library_path_explode(path, blend_path, NULL, NULL)) {
const FileAssetSelectParams *asset_params = ED_fileselect_get_asset_params(sfile);
BLI_assert(asset_params != NULL);
UI_but_drag_set_asset(but,
&(AssetHandle){.file_data = file},
BLI_strdup(blend_path),
asset_params->import_type,
get_asset_import_type(sfile, file),
icon,
imb,
scale);

View File

@ -58,6 +58,8 @@
#include "UI_interface_icons.h"
#include "UI_view2d.h"
#include "AS_essentials_library.hh"
#include "file_intern.h"
#include "filelist.h"
@ -424,7 +426,13 @@ static void fileselect_refresh_asset_params(FileAssetSelectParams *asset_params)
}
}
switch (library->type) {
switch (eAssetLibraryType(library->type)) {
case ASSET_LIBRARY_ESSENTIALS:
BLI_strncpy(base_params->dir,
blender::asset_system::essentials_directory_path().c_str(),
sizeof(base_params->dir));
base_params->type = FILE_ASSET_LIBRARY;
break;
case ASSET_LIBRARY_ALL:
base_params->dir[0] = '\0';
base_params->type = FILE_ASSET_LIBRARY_ALL;

View File

@ -85,11 +85,11 @@ typedef struct AssetMetaData {
} AssetMetaData;
typedef enum eAssetLibraryType {
/* For the future. Display assets bundled with Blender by default. */
// ASSET_LIBRARY_BUNDLED = 0,
/** Display assets from the current session (current "Main"). */
ASSET_LIBRARY_LOCAL = 1,
ASSET_LIBRARY_ALL = 2,
/** Display assets bundled with Blender by default. */
JacquesLucke marked this conversation as resolved
Review

Doxygen comment.

Doxygen comment.
ASSET_LIBRARY_ESSENTIALS = 3,
/** Display assets from custom asset libraries, as defined in the preferences
* (#bUserAssetLibrary). The name will be taken from #FileSelectParams.asset_library_ref.idname

View File

@ -1481,6 +1481,20 @@ install(
)
# -----------------------------------------------------------------------------
# Bundle assets
set(ASSET_BUNDLE_DIR ${CMAKE_SOURCE_DIR}/../lib/assets/publish/)
if(EXISTS "${ASSET_BUNDLE_DIR}")
install(
DIRECTORY ${ASSET_BUNDLE_DIR}
DESTINATION ${TARGETDIR_VER}/datafiles/assets
PATTERN ".svn" EXCLUDE
)
endif()
# -----------------------------------------------------------------------------
# Setup link libraries