UI: Asset Shelf (Experimental Feature) #104831

Closed
Julian Eisel wants to merge 399 commits from asset-shelf into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
5 changed files with 53 additions and 14 deletions
Showing only changes of commit e27067d7ac - Show all commits

View File

@ -283,6 +283,18 @@ std::string AssetLibraryService::resolve_asset_weak_reference_to_library_path(
return normalized_library_path;
}
std::string AssetLibraryService::normalize_asset_weak_reference_relative_asset_identifier(
const AssetWeakReference &asset_reference)
{
/* This assumes that the 'group' is the first element of the path. There shoudl never ever be any
* path separator in group names. */
StringRefNull relative_asset_identifier = asset_reference.relative_asset_identifier;
const int64_t alt_group_len = int64_t(relative_asset_identifier.find(ALTSEP));
int64_t group_len = std::min(int64_t(relative_asset_identifier.find(SEP)), alt_group_len);
return utils::normalize_path(relative_asset_identifier, size_t(group_len));
}
/* TODO currently only works for asset libraries on disk (custom or essentials asset libraries).
* Once there is a proper registry of asset libraries, this could contain an asset library locator
* and/or identifier, so a full path (not necessarily file path) can be built for all asset
@ -299,8 +311,9 @@ std::string AssetLibraryService::resolve_asset_weak_reference_to_full_path(
return "";
}
std::string normalized_full_path = utils::normalize_path(
library_path + SEP_STR + asset_reference.relative_asset_identifier);
std::string normalized_full_path = utils::normalize_path(library_path + SEP_STR) +
normalize_asset_weak_reference_relative_asset_identifier(
asset_reference);
return normalized_full_path;
}
@ -313,7 +326,8 @@ std::optional<AssetLibraryService::ExplodedPath> AssetLibraryService::
switch (eAssetLibraryType(asset_reference.asset_library_type)) {
case ASSET_LIBRARY_LOCAL: {
std::string path_in_file = utils::normalize_path(asset_reference.relative_asset_identifier);
std::string path_in_file = normalize_asset_weak_reference_relative_asset_identifier(
asset_reference);
const int64_t group_len = int64_t(path_in_file.find(SEP));
ExplodedPath exploded;

View File

@ -79,6 +79,11 @@ class AssetLibraryService {
/** Get the "All" asset library, which loads all others and merges them into one. */
AssetLibrary *get_asset_library_all(const Main *bmain);
/** return a normalized version of #AssetWeakReference.relative_asset_identifier.
* Special care is required here because slahes or backslashes should not be converted in the ID
* name itself. */
std::string normalize_asset_weak_reference_relative_asset_identifier(
const AssetWeakReference &asset_reference);
/** Get a valid library path from the weak reference. Empty if e.g. the reference is to a local
* asset. */
std::string resolve_asset_weak_reference_to_library_path(

View File

@ -27,14 +27,28 @@ std::string normalize_directory_path(StringRef directory)
return std::string(dir_normalized);
}
std::string normalize_path(StringRefNull path)
/**
* Normalize the given `path` (remove 'parent directory' and double-slashes element etc., and
* convert to native path separators).
*
* If `max_len` is not default `std::string::npos` value, only the first part of the given string
* up to `max_len` is processed, the rest remains unchanged. Needed to avoid modifying ID name part
* of linked data paths. */
std::string normalize_path(std::string path, size_t max_len)
{
char *buf = BLI_strdupn(path.c_str(), size_t(path.size()));
max_len = (max_len == std::string::npos) ? size_t(path.size()) :
std::min(max_len, size_t(path.size()));
char *buf = BLI_strdupn(path.c_str(), max_len);
BLI_path_normalize(nullptr, buf);
BLI_path_slash_native(buf);
std::string normalized_path = buf;
MEM_freeN(buf);
if (max_len != size_t(path.size())) {
normalized_path = normalized_path + path.substr(int64_t(max_len));
}
return normalized_path;
}

View File

@ -20,6 +20,6 @@ std::string normalize_directory_path(StringRef directory);
* Returns a normalized file path, with no maximum length.
* Slashes are converted to native format.
*/
std::string normalize_path(StringRefNull path);
std::string normalize_path(std::string path, size_t max_len = std::string::npos);
} // namespace blender::asset_system::utils

View File

@ -8,6 +8,8 @@
#include "DNA_asset_types.h"
#include "../intern/utils.hh"
#include "testing/testing.h"
namespace blender::asset_system::tests {
@ -92,10 +94,11 @@ TEST_F(AssetRepresentationTest, weak_reference__resolve_to_full_path__custom_lib
std::unique_ptr<AssetWeakReference> weak_ref = asset.make_weak_reference();
std::string expected_path = asset_library_root_ + "/" + "path/to/an/asset";
std::string expected_path = utils::normalize_path(asset_library_root_ + "/" + "path/") +
"to/an/asset";
std::string resolved_path = service->resolve_asset_weak_reference_to_full_path(*weak_ref);
EXPECT_EQ(BLI_path_cmp_normalized(resolved_path.c_str(), expected_path.c_str()), 0);
EXPECT_EQ(BLI_path_cmp(resolved_path.c_str(), expected_path.c_str()), 0);
}
/* #AssetLibraryService::resolve_asset_weak_reference_to_exploded_path(). */
@ -107,9 +110,11 @@ TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__curren
std::unique_ptr<AssetWeakReference> weak_ref = asset.make_weak_reference();
std::string expected_full_path = utils::normalize_path("path/to/an/asset", 5);
std::optional<AssetLibraryService::ExplodedPath> resolved_path =
service->resolve_asset_weak_reference_to_exploded_path(*weak_ref);
EXPECT_EQ(*resolved_path->full_path, "path/to/an/asset");
EXPECT_EQ(*resolved_path->full_path, expected_full_path);
EXPECT_EQ(resolved_path->dir_component, "");
EXPECT_EQ(resolved_path->group_component, "path");
/* ID names may contain slashes. */
@ -126,12 +131,13 @@ TEST_F(AssetRepresentationTest, weak_reference__resolve_to_exploded_path__custom
std::unique_ptr<AssetWeakReference> weak_ref = asset.make_weak_reference();
std::string expected_full_path = asset_library_root_ + "/some.blend/Material/asset/name";
std::string expected_full_path = utils::normalize_path(asset_library_root_ +
"/some.blend/Material/") +
"asset/name";
std::optional<AssetLibraryService::ExplodedPath> resolved_path =
service->resolve_asset_weak_reference_to_exploded_path(*weak_ref);
EXPECT_EQ(BLI_path_cmp_normalized(resolved_path->full_path->c_str(), expected_full_path.c_str()),
0);
EXPECT_EQ(BLI_path_cmp(resolved_path->full_path->c_str(), expected_full_path.c_str()), 0);
EXPECT_EQ(BLI_path_cmp_normalized(std::string(resolved_path->dir_component).c_str(),
std::string(asset_library_root_ + "/some.blend").c_str()),
0);