Do ID remapping for asset libraries
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
struct IDRemapper;
|
||||
struct Main;
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -31,6 +32,8 @@ void BKE_asset_library_refresh_catalog_simplename(struct AssetLibrary *asset_lib
|
||||
/** Return whether any loaded AssetLibrary has unsaved changes to its catalogs. */
|
||||
bool BKE_asset_library_has_any_unsaved_catalogs(void);
|
||||
|
||||
void BKE_asset_library_remap_ids(struct IDRemapper *mappings);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@@ -79,6 +79,8 @@ struct AssetLibrary {
|
||||
|
||||
void on_blend_save_post(Main *bmain, PointerRNA **pointers, int num_pointers);
|
||||
|
||||
void remap_ids(struct IDRemapper &mappings);
|
||||
|
||||
private:
|
||||
bCallbackFuncStore on_save_callback_store_{};
|
||||
|
||||
|
@@ -21,6 +21,8 @@ namespace blender::bke {
|
||||
* interact with them. Think of it like a view on an asset.
|
||||
*/
|
||||
class AssetRepresentation {
|
||||
friend class AssetLibrary;
|
||||
|
||||
/** Null if the asset represents a local ID, in which case the ID owns the metadata. */
|
||||
std::unique_ptr<AssetMetaData> metadata_ = nullptr;
|
||||
/** If this asset represents an ID in the current file, this references the ID directly. This
|
||||
|
@@ -8,11 +8,13 @@
|
||||
|
||||
#include "BKE_asset_library.hh"
|
||||
#include "BKE_asset_representation.hh"
|
||||
#include "BKE_lib_remap.h"
|
||||
#include "BKE_main.h"
|
||||
#include "BKE_preferences.h"
|
||||
|
||||
#include "BLI_fileops.h"
|
||||
#include "BLI_path_util.h"
|
||||
#include "BLI_set.hh"
|
||||
|
||||
#include "DNA_asset_types.h"
|
||||
#include "DNA_userdef_types.h"
|
||||
@@ -99,6 +101,13 @@ void BKE_asset_library_refresh_catalog_simplename(struct AssetLibrary *asset_lib
|
||||
lib->refresh_catalog_simplename(asset_data);
|
||||
}
|
||||
|
||||
void BKE_asset_library_remap_ids(IDRemapper *mappings)
|
||||
{
|
||||
blender::bke::AssetLibraryService *service = blender::bke::AssetLibraryService::get();
|
||||
service->foreach_loaded_asset_library(
|
||||
[mappings](blender::bke::AssetLibrary &library) { library.remap_ids(*mappings); });
|
||||
}
|
||||
|
||||
namespace blender::bke {
|
||||
|
||||
AssetLibrary::AssetLibrary() : catalog_service(std::make_unique<AssetCatalogService>())
|
||||
@@ -204,6 +213,28 @@ void AssetLibrary::on_blend_save_post(struct Main *main,
|
||||
}
|
||||
}
|
||||
|
||||
void AssetLibrary::remap_ids(IDRemapper &mappings)
|
||||
{
|
||||
Set<AssetRepresentation *> removed_id_assets;
|
||||
|
||||
for (auto &asset_uptr : asset_storage_) {
|
||||
if (!asset_uptr->is_local_id()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
IDRemapperApplyResult result = BKE_id_remapper_apply(
|
||||
&mappings, &asset_uptr->local_asset_id_, ID_REMAP_APPLY_DEFAULT);
|
||||
if (result == ID_REMAP_RESULT_SOURCE_UNASSIGNED) {
|
||||
removed_id_assets.add(asset_uptr.get());
|
||||
}
|
||||
}
|
||||
|
||||
/* Remove the assets from storage. */
|
||||
for (AssetRepresentation *asset : removed_id_assets) {
|
||||
remove_asset(*asset);
|
||||
}
|
||||
}
|
||||
|
||||
void AssetLibrary::refresh_catalog_simplename(struct AssetMetaData *asset_data)
|
||||
{
|
||||
if (BLI_uuid_is_nil(asset_data->catalog_id)) {
|
||||
|
@@ -6,6 +6,7 @@
|
||||
|
||||
#include "asset_library_service.hh"
|
||||
|
||||
#include "BKE_asset_library.hh"
|
||||
#include "BKE_blender.h"
|
||||
#include "BKE_preferences.h"
|
||||
|
||||
@@ -175,4 +176,15 @@ bool AssetLibraryService::has_any_unsaved_catalogs() const
|
||||
return false;
|
||||
}
|
||||
|
||||
void AssetLibraryService::foreach_loaded_asset_library(FunctionRef<void(AssetLibrary &)> fn) const
|
||||
{
|
||||
if (current_file_library_) {
|
||||
fn(*current_file_library_);
|
||||
}
|
||||
|
||||
for (const auto &asset_lib_uptr : on_disk_libraries_.values()) {
|
||||
fn(*asset_lib_uptr);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace blender::bke
|
||||
|
@@ -12,6 +12,7 @@
|
||||
|
||||
#include "BKE_asset_library.hh"
|
||||
|
||||
#include "BLI_function_ref.hh"
|
||||
#include "BLI_map.hh"
|
||||
|
||||
#include <memory>
|
||||
@@ -62,6 +63,8 @@ class AssetLibraryService {
|
||||
/** Returns whether there are any known asset libraries with unsaved catalog edits. */
|
||||
bool has_any_unsaved_catalogs() const;
|
||||
|
||||
void foreach_loaded_asset_library(FunctionRef<void(AssetLibrary &)> fn) const;
|
||||
|
||||
protected:
|
||||
static std::unique_ptr<AssetLibraryService> instance_;
|
||||
|
||||
|
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "BKE_anim_data.h"
|
||||
#include "BKE_asset.h"
|
||||
#include "BKE_asset_library.h"
|
||||
#include "BKE_idprop.h"
|
||||
#include "BKE_idtype.h"
|
||||
#include "BKE_key.h"
|
||||
@@ -137,16 +138,16 @@ void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_i
|
||||
BKE_main_lock(bmain);
|
||||
}
|
||||
|
||||
struct IDRemapper *remapper = BKE_id_remapper_create();
|
||||
BKE_id_remapper_add(remapper, id, NULL);
|
||||
|
||||
if ((flag & LIB_ID_FREE_NO_UI_USER) == 0) {
|
||||
if (free_notifier_reference_cb) {
|
||||
free_notifier_reference_cb(id);
|
||||
}
|
||||
|
||||
if (remap_editor_id_reference_cb) {
|
||||
struct IDRemapper *remapper = BKE_id_remapper_create();
|
||||
BKE_id_remapper_add(remapper, id, NULL);
|
||||
remap_editor_id_reference_cb(remapper);
|
||||
BKE_id_remapper_free(remapper);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,6 +159,9 @@ void BKE_id_free_ex(Main *bmain, void *idv, int flag, const bool use_flag_from_i
|
||||
}
|
||||
}
|
||||
|
||||
BKE_asset_library_remap_ids(remapper);
|
||||
BKE_id_remapper_free(remapper);
|
||||
|
||||
BKE_libblock_free_data(id, (flag & LIB_ID_FREE_NO_USER_REFCOUNT) == 0);
|
||||
|
||||
if ((flag & LIB_ID_FREE_NO_MAIN) == 0) {
|
||||
|
Reference in New Issue
Block a user