Commit: c8741a3c0336e9d92d1140a6b7a0815ca572c7aa Author: Hans Goudey Date: Thu Dec 29 16:40:29 2022 -0500 Branches: master https://developer.blender.org/rBc8741a3c0336e9d92d1140a6b7a0815ca572c7aa
Cleanup: Simplify node clipboard, use Vector instead of ListBase - Move from blenkernel to the node editor, the only place it was used - Use two vectors instead of ListBase - Remove define for validating the clipboard, which shouldn't be skipped - Comment formatting, other small cleanups to whitespace Differential Revision: https://developer.blender.org/D16880 =================================================================== M source/blender/blenkernel/BKE_node.h M source/blender/blenkernel/intern/node.cc M source/blender/editors/include/ED_node.h M source/blender/editors/space_node/CMakeLists.txt A source/blender/editors/space_node/clipboard.cc M source/blender/editors/space_node/node_edit.cc M source/blender/windowmanager/intern/wm_init_exit.c =================================================================== diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 266ee0d2988..acf52a5b965 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -713,6 +713,8 @@ bNode *node_copy_with_mapping(bNodeTree *dst_tree, bNode *node_copy(bNodeTree *dst_tree, const bNode &src_node, int flag, bool use_unique); +void node_free_node(bNodeTree *tree, bNode *node); + } // namespace blender::bke #endif @@ -865,20 +867,6 @@ bool nodeDeclarationEnsureOnOutdatedNode(struct bNodeTree *ntree, struct bNode * */ void nodeSocketDeclarationsUpdate(struct bNode *node); -/** - * Node Clipboard. - */ -void BKE_node_clipboard_clear(void); -void BKE_node_clipboard_free(void); -/** - * Return false when one or more ID's are lost. - */ -bool BKE_node_clipboard_validate(void); -void BKE_node_clipboard_add_node(struct bNode *node); -void BKE_node_clipboard_add_link(struct bNodeLink *link); -const struct ListBase *BKE_node_clipboard_get_nodes(void); -const struct ListBase *BKE_node_clipboard_get_links(void); - /** * Node Instance Hash. */ diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index eb744f1fd66..3cd0cf2f7ca 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -118,7 +118,6 @@ static CLG_LogRef LOG = {"bke.node"}; static void ntree_set_typeinfo(bNodeTree *ntree, bNodeTreeType *typeinfo); static void node_socket_copy(bNodeSocket *sock_dst, const bNodeSocket *sock_src, const int flag); static void free_localized_node_groups(bNodeTree *ntree); -static void node_free_node(bNodeTree *ntree, bNode *node); static void node_socket_interface_free(bNodeTree * /*ntree*/, bNodeSocket *sock, const bool do_id_user); @@ -243,7 +242,7 @@ static void ntree_free_data(ID *id) BLI_freelistN(&ntree->links); LISTBASE_FOREACH_MUTABLE (bNode *, node, &ntree->nodes) { - node_free_node(ntree, node); + blender::bke::node_free_node(ntree, node); } /* free interface sockets */ @@ -2951,12 +2950,14 @@ void nodeRebuildIDVector(bNodeTree *node_tree) } } +namespace blender::bke { + /** * Free the node itself. * * \note: ID user refcounting and changing the `nodes_by_id` vector are up to the caller. */ -static void node_free_node(bNodeTree *ntree, bNode *node) +void node_free_node(bNodeTree *ntree, bNode *node) { /* since it is called while free database, node->id is undefined */ @@ -3011,6 +3012,8 @@ static void node_free_node(bNodeTree *ntree, bNode *node) } } +} // namespace blender::bke + void ntreeFreeLocalNode(bNodeTree *ntree, bNode *node) { /* For removing nodes while editing localized node trees. */ @@ -3021,7 +3024,7 @@ void ntreeFreeLocalNode(bNodeTree *ntree, bNode *node) nodeUnlinkNode(ntree, node); node_unlink_attached(ntree, node); - node_free_node(ntree, node); + blender::bke::node_free_node(ntree, node); nodeRebuildIDVector(ntree); } @@ -3081,7 +3084,7 @@ void nodeRemoveNode(Main *bmain, bNodeTree *ntree, bNode *node, bool do_id_user) node_unlink_attached(ntree, node); /* Free node itself. */ - node_free_node(ntree, node); + blender::bke::node_free_node(ntree, node); nodeRebuildIDVector(ntree); } @@ -3649,149 +3652,6 @@ void nodeInternalLinks(bNode *node, bNodeLink ***r_links, int *r_len) *r_len = node->runtime->internal_links.size(); } -/* ************** Node Clipboard *********** */ - -#define USE_NODE_CB_VALIDATE - -#ifdef USE_NODE_CB_VALIDATE -/** - * This data structure is to validate the node on creation, - * otherwise we may reference missing data. - * - * Currently its only used for ID's, but nodes may one day - * reference other pointers which need validation. - */ -struct bNodeClipboardExtraInfo { - struct bNodeClipboardExtraInfo *next, *prev; - ID *id; - char id_name[MAX_ID_NAME]; - char library_name[FILE_MAX]; -}; -#endif /* USE_NODE_CB_VALIDATE */ - -struct bNodeClipboard { - ListBase nodes; - -#ifdef USE_NODE_CB_VALIDATE - ListBase nodes_extra_info; -#endif - - ListBase links; -}; - -static bNodeClipboard node_clipboard = {{nullptr}}; - -void BKE_node_clipboard_clear() -{ - LISTBASE_FOREACH_MUTABLE (bNodeLink *, link, &node_clipboard.links) { - nodeRemLink(nullptr, link); - } - BLI_listbase_clear(&node_clipboard.links); - - LISTBASE_FOREACH_MUTABLE (bNode *, node, &node_clipboard.nodes) { - node_free_node(nullptr, node); - } - BLI_listbase_clear(&node_clipboard.nodes); - -#ifdef USE_NODE_CB_VALIDATE - BLI_freelistN(&node_clipboard.nodes_extra_info); -#endif -} - -bool BKE_node_clipboard_validate() -{ - bool ok = true; - -#ifdef USE_NODE_CB_VALIDATE - bNodeClipboardExtraInfo *node_info; - bNode *node; - - /* lists must be aligned */ - BLI_assert(BLI_listbase_count(&node_clipboard.nodes) == - BLI_listbase_count(&node_clipboard.nodes_extra_info)); - - for (node = (bNode *)node_clipboard.nodes.first, - node_info = (bNodeClipboardExtraInfo *)node_clipboard.nodes_extra_info.first; - node; - node = (bNode *)node->next, node_info = (bNodeClipboardExtraInfo *)node_info->next) { - /* validate the node against the stored node info */ - - /* re-assign each loop since we may clear, - * open a new file where the ID is valid, and paste again */ - node->id = node_info->id; - - /* currently only validate the ID */ - if (node->id) { - /* We want to search into current blend file, so using G_MAIN is valid here too. */ - ListBase *lb = which_libbase(G_MAIN, GS(node_info->id_name)); - BLI_assert(lb != nullptr); - - if (BLI_findindex(lb, node_info->id) == -1) { - /* May assign null. */ - node->id = (ID *)BLI_findstring(lb, node_info->id_name + 2, offsetof(ID, name) + 2); - - if (node->id == nullptr) { - ok = false; - } - } - } - } -#endif /* USE_NODE_CB_VALIDATE */ - - return ok; -} - -void BKE_node_clipboard_add_node(bNode *node) -{ -#ifdef USE_NODE_CB_VALIDATE - /* add extra info */ - bNodeClipboardExtraInfo *node_info = (bNodeClipboardExtraInfo *)MEM_mallocN( - sizeof(bNodeClipboardExtraInfo), __func__); - - node_info->id = node->id; - if (node->id) { - BLI_strncpy(node_info->id_name, node->id->name, sizeof(node_info->id_name)); - if (ID_IS_LINKED(node->id)) { - BLI_strncpy( - node_info->library_name, node->id->lib->filepath_abs, sizeof(node_info->library_name)); - } - else { - node_info->library_name[0] = '\0'; - } - } - else { - node_info->id_name[0] = '\0'; - node_info->library_name[0] = '\0'; - } - BLI_addtail(&node_clipboard.nodes_extra_info, node_info); - /* end extra info */ -#endif /* USE_NODE_CB_VALIDATE */ - - /* add node */ - BLI_addtail(&node_clipboard.nodes, node); -} - -void BKE_node_clipboard_add_link(bNodeLink *link) -{ - BLI_addtail(&node_clipboard.links, link); -} - -const ListBase *BKE_node_clipboard_get_nodes() -{ - return &node_clipboard.nodes; -} - -const ListBase *BKE_node_clipboard_get_links() -{ - return &node_clipboard.links; -} - -void BKE_node_clipboard_free() -{ - BKE_node_clipboard_validate(); - BKE_node_clipboard_clear(); -} - /* Node Instance Hash */ const bNodeInstanceKey NODE_INSTANCE_KEY_BASE = {5381}; diff --git a/source/blender/editors/include/ED_node.h b/source/blender/editors/include/ED_node.h index 3ce972943b5..dc35ac74524 100644 --- a/source/blender/editors/include/ED_node.h +++ b/source/blender/editors/include/ED_node.h @@ -42,6 +42,10 @@ ENUM_OPERATORS(NodeBorder, NODE_RIGHT) #define NODE_EDGE_PAN_DELAY 0.5f #define NODE_EDGE_PAN_ZOOM_INFLUENCE 0.5f +/* clipboard.cc */ + +void ED_node_clipboard_free(void); + /* space_node.cc */ void ED_node_cursor_location_get(const struct SpaceNode *snode, float value[2]); diff --git a/source/blender/editors/space_node/CMakeLists.txt b/source/blender/editors/space_node/CMakeLists.txt index 3236082c0d2..4e2c4f8b8af 100644 --- a/source/blender/editors/space_node/CMakeLists.txt +++ b/source/blender/editors/space_node/CMakeLists.txt @@ -31,6 +31,7 @@ set(INC set(SRC add_menu_assets.cc add_node_search.cc + clipboard.cc drawnode.cc link_drag_search.cc node_add.cc diff --git a/source/blender/editors/space_node/clipboard.cc b/source/blender/editors/space_node/clipboard.cc new file mode 100644 index 00000000000..fa82f80d1a9 --- /dev/null +++ b/source/blender/editors/space_node/clipboard.cc @@ -0,0 +1,301 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "DNA_space_types.h" + +#include "BKE_context.h" +#include "BKE_global.h" +#include "BKE_lib_id.h" +#include "BKE_main.h" +#include "BKE_node.h" +#include "BKE_node_runtime.hh" +#include "BKE_node_tree_update.h" +#include "BKE_report.h" + +#include "ED_node.h" +#include "ED_node.hh" +#include "ED_render.h" +#include "ED_screen.h" + +#include "DEG_depsgraph_build.h" + +#include "node_intern.hh" + +namespace blender::ed::space_node { + +struct NodeClipboardItem { + bNode *node; + + /* Extra info to validate the node on creation. Otherwise we may reference missing data. */ + ID *id; + std::string id_name; + std::string library_name; +}; + +struct NodeClipboard { + Vector<NodeClipboardItem> nodes; + Vector<bNodeLink> links; + + void clear() + { + for (NodeClipboardItem &item : this->nodes) { + bke::node_free_node(nullptr, item.node); + } + this->nodes.clear_and_shrink(); + this->links.clear_and_shrink(); + } + + /** + * Replace node IDs that are no longer available in the current file. Return false when one or + * more IDs are lost. + */ + bool validate() + { + bool ok = true; + + for (NodeClipboardItem &item : this->nodes) { + bNode &node = *item.node; + /* Reassign each loop since we may clear, open a new file where the ID is valid, and paste + * again. */ + node.id = item.id; + + if (node.id) { + const ListBase *lb = which_libbase(G_MAIN, GS(item.id_name.c_str())); + if (BLI_findindex(lb, item.id) == -1) { + /* May assign null. */ + node.id = static_cast<ID *>( + B @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs