BKE node: Add a new in_lib function to create nodetrees within a library.

While not common, some places of the code (like readfile versionning)
may have to generate new IDs for linked data.
This commit is contained in:
Bastien Montagne
2024-04-16 17:35:22 +02:00
parent cec57e9138
commit fda3900722
2 changed files with 29 additions and 5 deletions

View File

@@ -456,6 +456,15 @@ void ntreeSetTypes(const bContext *C, bNodeTree *ntree);
bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname);
/**
* Add a new (non-embedded) node tree, like #ntreeAddTree, but allows to create it inside a given
* library. Used mainly by readfile code when versionning linked data.
*/
bNodeTree *BKE_node_tree_add_in_lib(Main *bmain,
Library *owner_library,
const char *name,
const char *idname);
/**
* Free tree which is embedded into another data-block.
*/

View File

@@ -3142,8 +3142,12 @@ void nodePositionPropagate(bNode *node)
} // namespace blender::bke
static bNodeTree *ntreeAddTree_do(
Main *bmain, ID *owner_id, const bool is_embedded, const char *name, const char *idname)
static bNodeTree *ntreeAddTree_do(Main *bmain,
std::optional<Library *> owner_library,
ID *owner_id,
const bool is_embedded,
const char *name,
const char *idname)
{
/* trees are created as local trees for compositor, material or texture nodes,
* node groups and other tree types are created as library data.
@@ -3152,7 +3156,10 @@ static bNodeTree *ntreeAddTree_do(
if (is_embedded || bmain == nullptr) {
flag |= LIB_ID_CREATE_NO_MAIN;
}
bNodeTree *ntree = reinterpret_cast<bNodeTree *>(BKE_libblock_alloc(bmain, ID_NT, name, flag));
BLI_assert_msg(!owner_library || !owner_id,
"Embedded NTrees should never have a defined owner library here");
bNodeTree *ntree = reinterpret_cast<bNodeTree *>(
BKE_libblock_alloc_in_lib(bmain, owner_library, ID_NT, name, flag));
BKE_libblock_init_empty(&ntree->id);
if (is_embedded) {
BLI_assert(owner_id != nullptr);
@@ -3174,7 +3181,15 @@ static bNodeTree *ntreeAddTree_do(
bNodeTree *ntreeAddTree(Main *bmain, const char *name, const char *idname)
{
return ntreeAddTree_do(bmain, nullptr, false, name, idname);
return ntreeAddTree_do(bmain, std::nullopt, nullptr, false, name, idname);
}
bNodeTree *BKE_node_tree_add_in_lib(Main *bmain,
Library *owner_library,
const char *name,
const char *idname)
{
return ntreeAddTree_do(bmain, owner_library, nullptr, false, name, idname);
}
namespace blender::bke {
@@ -3184,7 +3199,7 @@ bNodeTree *ntreeAddTreeEmbedded(Main * /*bmain*/,
const char *name,
const char *idname)
{
return ntreeAddTree_do(nullptr, owner_id, true, name, idname);
return ntreeAddTree_do(nullptr, std::nullopt, owner_id, true, name, idname);
}
bNodeTree *ntreeCopyTree_ex(const bNodeTree *ntree, Main *bmain, const bool do_id_user)