From b3fb73f3254573a5a86184b51a5f8e3435fbf472 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Tue, 14 Feb 2023 17:35:29 +0100 Subject: [PATCH] Assets: bundle Essentials with Blender This patch adds an "Essentials" asset library that is bundled with Blender. Also see #103620. At build time, the `lib/assets/publish` folder is copied to `datafiles/assets` in the build directory. In the UI, the "Essentials" library can be accessed like other custom asset libraries with the exception that assets from that library cannot be linked. The immediate impact of this is that Blender now comes with some geometry node groups for procedural hair grooming. Pull Request #104474 --- .../blender/asset_system/AS_asset_library.hh | 3 +++ .../asset_system/AS_asset_representation.h | 2 ++ .../asset_system/AS_essentials_library.hh | 15 +++++++++++ source/blender/asset_system/CMakeLists.txt | 2 ++ .../intern/asset_essentials_library.cc | 24 ++++++++++++++++++ .../asset_system/intern/asset_library.cc | 6 +++++ .../intern/asset_library_service.cc | 7 ++++++ .../intern/asset_representation.cc | 8 ++++++ .../intern/asset_library_reference_enum.cc | 7 +++++- .../editors/asset/intern/asset_list.cc | 1 + source/blender/editors/space_file/file_draw.c | 25 +++++++++++++------ source/blender/editors/space_file/filesel.cc | 10 +++++++- source/blender/makesdna/DNA_asset_types.h | 4 +-- source/creator/CMakeLists.txt | 14 +++++++++++ 14 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 source/blender/asset_system/AS_essentials_library.hh create mode 100644 source/blender/asset_system/intern/asset_essentials_library.cc diff --git a/source/blender/asset_system/AS_asset_library.hh b/source/blender/asset_system/AS_asset_library.hh index b3b7d421724..d487f9711fd 100644 --- a/source/blender/asset_system/AS_asset_library.hh +++ b/source/blender/asset_system/AS_asset_library.hh @@ -67,6 +67,9 @@ class AssetLibrary { std::unique_ptr catalog_service; + /** Assets owned by this library should never be linked. */ + bool never_link = false; + friend class AssetLibraryService; public: diff --git a/source/blender/asset_system/AS_asset_representation.h b/source/blender/asset_system/AS_asset_representation.h index 6a5425979aa..359567cbf0b 100644 --- a/source/blender/asset_system/AS_asset_representation.h +++ b/source/blender/asset_system/AS_asset_representation.h @@ -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) + ATTR_WARN_UNUSED_RESULT; #ifdef __cplusplus } diff --git a/source/blender/asset_system/AS_essentials_library.hh b/source/blender/asset_system/AS_essentials_library.hh new file mode 100644 index 00000000000..fafaab6e6a0 --- /dev/null +++ b/source/blender/asset_system/AS_essentials_library.hh @@ -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(); + +} diff --git a/source/blender/asset_system/CMakeLists.txt b/source/blender/asset_system/CMakeLists.txt index f8e1df40d80..25b00e54e71 100644 --- a/source/blender/asset_system/CMakeLists.txt +++ b/source/blender/asset_system/CMakeLists.txt @@ -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 diff --git a/source/blender/asset_system/intern/asset_essentials_library.cc b/source/blender/asset_system/intern/asset_essentials_library.cc new file mode 100644 index 00000000000..8a4e51af27a --- /dev/null +++ b/source/blender/asset_system/intern/asset_essentials_library.cc @@ -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 diff --git a/source/blender/asset_system/intern/asset_library.cc b/source/blender/asset_system/intern/asset_library.cc index fdb54c16af6..15abc7c776e 100644 --- a/source/blender/asset_system/intern/asset_library.cc +++ b/source/blender/asset_system/intern/asset_library.cc @@ -260,6 +260,12 @@ StringRefNull AssetLibrary::root_path() const Vector all_valid_asset_library_refs() { Vector 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)) { diff --git a/source/blender/asset_system/intern/asset_library_service.cc b/source/blender/asset_system/intern/asset_library_service.cc index af48a173bc0..b9ae8e7bd69 100644 --- a/source/blender/asset_system/intern/asset_library_service.cc +++ b/source/blender/asset_system/intern/asset_library_service.cc @@ -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) : diff --git a/source/blender/asset_system/intern/asset_representation.cc b/source/blender/asset_system/intern/asset_representation.cc index 13c6236a6dd..5d3660e4bb3 100644 --- a/source/blender/asset_system/intern/asset_representation.cc +++ b/source/blender/asset_system/intern/asset_representation.cc @@ -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(asset_handle); + return asset->owner_asset_library().never_link; +} + /** \} */ 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 d20f3205a77..95cecfcc249 100644 --- a/source/blender/editors/asset/intern/asset_library_reference_enum.cc +++ b/source/blender/editors/asset/intern/asset_library_reference_enum.cc @@ -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}, }; diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc index 64934316413..78859a9206d 100644 --- a/source/blender/editors/asset/intern/asset_list.cc +++ b/source/blender/editors/asset/intern/asset_list.cc @@ -372,6 +372,7 @@ std::optional 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: diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c index 0cc188b47c0..a940be844e0 100644 --- a/source/blender/editors/space_file/file_draw.c +++ b/source/blender/editors/space_file/file_draw.c @@ -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); + 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); diff --git a/source/blender/editors/space_file/filesel.cc b/source/blender/editors/space_file/filesel.cc index e6dfe6bdfb3..87d16320362 100644 --- a/source/blender/editors/space_file/filesel.cc +++ b/source/blender/editors/space_file/filesel.cc @@ -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; diff --git a/source/blender/makesdna/DNA_asset_types.h b/source/blender/makesdna/DNA_asset_types.h index 195cc059f18..d61e75d98c0 100644 --- a/source/blender/makesdna/DNA_asset_types.h +++ b/source/blender/makesdna/DNA_asset_types.h @@ -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. */ + 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 diff --git a/source/creator/CMakeLists.txt b/source/creator/CMakeLists.txt index 22b3d197672..265c12cac3d 100644 --- a/source/creator/CMakeLists.txt +++ b/source/creator/CMakeLists.txt @@ -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