Fix: Adding node group asset doesn't respect import method #109706

Merged
Hans Goudey merged 3 commits from HooglyBoogly/blender:fix-asset-import-method into main 2023-07-11 14:18:18 +02:00
6 changed files with 68 additions and 28 deletions

View File

@ -10,11 +10,19 @@
#include "DNA_ID_enums.h" #include "DNA_ID_enums.h"
struct ID;
struct Main; struct Main;
namespace blender::asset_system { namespace blender::asset_system {
class AssetRepresentation; class AssetRepresentation;
} }
struct ID *ED_asset_get_local_id_from_asset_or_append_and_reuse( namespace blender::ed::asset {
Main *bmain, const blender::asset_system::AssetRepresentation &asset, ID_Type idtype);
/**
* If the asset already has a corresponding local #ID, return it. Otherwise, link or append the
* asset's data-block, using "Append & Reuse" if the method is unspecified.
*/
ID *asset_local_id_ensure_imported(Main &bmain, const asset_system::AssetRepresentation &asset);
HooglyBoogly marked this conversation as resolved Outdated

I think the name should communicate that this may do the actual import, this is an important and potentially costly operation.

How about ensure_asset_id_imported(), get_asset_local_id_or_import() or something like that?

I think the name should communicate that this may do the actual import, this is an important and potentially costly operation. How about `ensure_asset_id_imported()`, `get_asset_local_id_or_import()` or something like that?
} // namespace blender::ed::asset

View File

@ -8,6 +8,8 @@
#include "AS_asset_representation.hh" #include "AS_asset_representation.hh"
#include "DNA_space_types.h"
#include "BLO_readfile.h" #include "BLO_readfile.h"
#include "WM_api.h" #include "WM_api.h"
@ -16,8 +18,9 @@
using namespace blender; using namespace blender;
ID *ED_asset_get_local_id_from_asset_or_append_and_reuse( namespace blender::ed::asset {
Main *bmain, const asset_system::AssetRepresentation &asset, ID_Type idtype)
ID *asset_local_id_ensure_imported(Main &bmain, const asset_system::AssetRepresentation &asset)
{ {
if (ID *local_id = asset.local_id()) { if (ID *local_id = asset.local_id()) {
return local_id; return local_id;
@ -28,14 +31,42 @@ ID *ED_asset_get_local_id_from_asset_or_append_and_reuse(
return nullptr; return nullptr;
} }
return WM_file_append_datablock(bmain, switch (asset.get_import_method().value_or(ASSET_IMPORT_APPEND_REUSE)) {
nullptr, case ASSET_IMPORT_LINK:
nullptr, return WM_file_link_datablock(&bmain,
nullptr, nullptr,
blend_path.c_str(), nullptr,
idtype, nullptr,
asset.get_name().c_str(), blend_path.c_str(),
BLO_LIBLINK_APPEND_RECURSIVE | asset.get_id_type(),
BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR | asset.get_name().c_str(),
BLO_LIBLINK_APPEND_LOCAL_ID_REUSE); (asset.get_use_relative_path() ? FILE_RELPATH : 0));
case ASSET_IMPORT_APPEND:
return WM_file_append_datablock(&bmain,
nullptr,
nullptr,
nullptr,
blend_path.c_str(),
asset.get_id_type(),
asset.get_name().c_str(),
BLO_LIBLINK_APPEND_RECURSIVE |
BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR |
(asset.get_use_relative_path() ? FILE_RELPATH : 0));
case ASSET_IMPORT_APPEND_REUSE:
return WM_file_append_datablock(&bmain,
nullptr,
nullptr,
nullptr,
blend_path.c_str(),
asset.get_id_type(),
asset.get_name().c_str(),
BLO_LIBLINK_APPEND_RECURSIVE |
BLO_LIBLINK_APPEND_ASSET_DATA_CLEAR |
BLO_LIBLINK_APPEND_LOCAL_ID_REUSE |
(asset.get_use_relative_path() ? FILE_RELPATH : 0));
}
BLI_assert_unreachable();
return nullptr;
} }
} // namespace blender::ed::asset

View File

@ -75,8 +75,9 @@ static const bNodeTree *get_node_group(const bContext &C)
if (!asset) { if (!asset) {
return nullptr; return nullptr;
} }
Main &bmain = *CTX_data_main(&C);
bNodeTree *node_group = reinterpret_cast<bNodeTree *>( bNodeTree *node_group = reinterpret_cast<bNodeTree *>(
ED_asset_get_local_id_from_asset_or_append_and_reuse(CTX_data_main(&C), *asset, ID_NT)); asset::asset_local_id_ensure_imported(bmain, *asset));
if (!node_group) { if (!node_group) {
return nullptr; return nullptr;
} }

View File

@ -72,17 +72,17 @@ static void search_items_for_asset_metadata(const bNodeTree &node_tree,
return; return;
} }
params.add_single_node_item( params.add_single_node_item(IFACE_(asset.get_name().c_str()),
IFACE_(asset.get_name().c_str()), asset_data.description == nullptr ? "" :
asset_data.description == nullptr ? "" : IFACE_(asset_data.description), IFACE_(asset_data.description),
[&asset](const bContext &C, bNodeTree &node_tree, bNode &node) { [&asset](const bContext &C, bNodeTree &node_tree, bNode &node) {
Main &bmain = *CTX_data_main(&C); Main &bmain = *CTX_data_main(&C);
node.flag &= ~NODE_OPTIONS; node.flag &= ~NODE_OPTIONS;
node.id = ED_asset_get_local_id_from_asset_or_append_and_reuse(&bmain, asset, ID_NT); node.id = asset::asset_local_id_ensure_imported(bmain, asset);
id_us_plus(node.id); id_us_plus(node.id);
BKE_ntree_update_tag_node_property(&node_tree, &node); BKE_ntree_update_tag_node_property(&node_tree, &node);
DEG_relations_tag_update(&bmain); DEG_relations_tag_update(&bmain);
}); });
} }
static void gather_search_items_for_all_assets(const bContext &C, static void gather_search_items_for_all_assets(const bContext &C,

View File

@ -200,7 +200,7 @@ static void search_link_ops_for_asset_metadata(const bNodeTree &node_tree,
bNode &node = params.add_node(params.node_tree.typeinfo->group_idname); bNode &node = params.add_node(params.node_tree.typeinfo->group_idname);
node.flag &= ~NODE_OPTIONS; node.flag &= ~NODE_OPTIONS;
node.id = ED_asset_get_local_id_from_asset_or_append_and_reuse(&bmain, asset, ID_NT); node.id = asset::asset_local_id_ensure_imported(bmain, asset);
id_us_plus(node.id); id_us_plus(node.id);
BKE_ntree_update_tag_node_property(&params.node_tree, &node); BKE_ntree_update_tag_node_property(&params.node_tree, &node);
DEG_relations_tag_update(&bmain); DEG_relations_tag_update(&bmain);

View File

@ -387,7 +387,7 @@ static bool add_node_group_asset(const bContext &C,
bNodeTree &edit_tree = *snode.edittree; bNodeTree &edit_tree = *snode.edittree;
bNodeTree *node_group = reinterpret_cast<bNodeTree *>( bNodeTree *node_group = reinterpret_cast<bNodeTree *>(
ED_asset_get_local_id_from_asset_or_append_and_reuse(&bmain, asset, ID_NT)); asset::asset_local_id_ensure_imported(bmain, asset));
if (!node_group) { if (!node_group) {
return false; return false;
} }