Blender V5.3
node_geo_closure.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_string_utf8.hh"
8
9#include "BKE_idprop.hh"
10
11#include "NOD_geo_closure.hh"
16#include "NOD_sync_sockets.hh"
17
18#include "BLO_read_write.hh"
20
21namespace blender {
22
24
26static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
27{
28 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(current_node_ptr->owner_id);
29 bNode *current_node = static_cast<bNode *>(current_node_ptr->data);
30
31 const bke::bNodeTreeZones *zones = ntree.zones();
32 if (!zones) {
33 return;
34 }
35 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(current_node->identifier);
36 if (!zone) {
37 return;
38 }
39 if (!zone->output_node_id) {
40 return;
41 }
42 bNode &output_node = const_cast<bNode &>(*zone->output_node());
43
44 layout.use_property_split_set(true);
45 layout.use_property_decorate_set(false);
46
47 PointerRNA output_node_ptr = RNA_pointer_create_discrete(&ntree.id, RNA_Node, &output_node);
48
49 layout.op("node.sockets_sync", IFACE_("Sync"), ICON_FILE_REFRESH);
50 layout.prop(&output_node_ptr, "define_signature", UI_ITEM_NONE, std::nullopt, ICON_NONE);
51 if (current_node->type_legacy == NODE_CLOSURE_INPUT) {
52 if (ui::Layout *panel = layout.panel(C, "input_items", false, IFACE_("Input Items"))) {
53 socket_items::ui::draw_items_list_with_operators<ClosureInputItemsAccessor>(
54 C, panel, ntree, output_node);
55 socket_items::ui::draw_active_item_props<ClosureInputItemsAccessor>(
56 ntree, output_node, [&](PointerRNA *item_ptr) {
57 panel->use_property_split_set(true);
58 panel->use_property_decorate_set(false);
59 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
60 panel->prop(item_ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
61 });
62 }
63 }
64 else {
65 if (ui::Layout *panel = layout.panel(C, "output_items", false, IFACE_("Output Items"))) {
66 socket_items::ui::draw_items_list_with_operators<ClosureOutputItemsAccessor>(
67 C, panel, ntree, output_node);
68 socket_items::ui::draw_active_item_props<ClosureOutputItemsAccessor>(
69 ntree, output_node, [&](PointerRNA *item_ptr) {
70 panel->use_property_split_set(true);
71 panel->use_property_decorate_set(false);
72 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
73 panel->prop(item_ptr, "structure_type", UI_ITEM_NONE, IFACE_("Shape"), ICON_NONE);
74 });
75 }
76 }
77}
78
79namespace input_node {
80
82
84{
85 const bNode *node = b.node_or_null();
86 const bNodeTree *tree = b.tree_or_null();
87 if (node && tree) {
88 const NodeClosureInput &storage = node_storage(*node);
89 const bNode *output_node = tree->node_by_id(storage.output_node_id);
90 if (output_node) {
91 const auto &output_storage = *static_cast<const NodeClosureOutput *>(output_node->storage);
92 for (const int i : IndexRange(output_storage.input_items.items_num)) {
93 const NodeClosureInputItem &item = output_storage.input_items.items[i];
94 const eNodeSocketDatatype socket_type = item.socket_type;
96 auto &decl = b.add_output(socket_type, UString(item.name), identifier);
97 decl.socket_name_ptr(&tree->id, *ClosureInputItemsAccessor::item_srna, &item, "name");
99 decl.structure_type(StructureType(item.structure_type));
100 }
101 else {
102 decl.structure_type(StructureType::Dynamic);
103 }
104 }
105 }
106 }
107 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
108 .custom_draw(socket_items::ui::draw_extend_socket_fn<ClosureInputItemsAccessor>());
109}
110
111static void node_label(const bNodeTree * /*ntree*/,
112 const bNode * /*node*/,
113 char *label,
114 const int label_maxncpy)
115{
116 BLI_strncpy_utf8(label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, "Closure"), label_maxncpy);
117}
118
119static void node_init(bNodeTree * /*tree*/, bNode *node)
120{
121 NodeClosureInput *data = MEM_new<NodeClosureInput>(__func__);
122 node->storage = data;
123}
124
126{
127 bNode *output_node = params.ntree.node_by_id(node_storage(params.node).output_node_id);
128 if (!output_node) {
129 return true;
130 }
132 params.ntree, params.node, *output_node, params.link);
133}
134
135static void node_register()
136{
137 static bke::bNodeType ntype;
138 sh_geo_node_type_base(&ntype, "NodeClosureInput"_ustr, NODE_CLOSURE_INPUT);
139 ntype.ui_name = "Closure Input";
141 ntype.declare = node_declare;
142 ntype.gather_link_search_ops = nullptr;
143 ntype.initfunc = node_init;
144 ntype.labelfunc = node_label;
145 ntype.no_muting = true;
149 ntype, "NodeClosureInput", node_free_standard_storage, node_copy_standard_storage);
151}
153
154} // namespace input_node
155
156namespace output_node {
157
159
161{
162 const bNodeTree *tree = b.tree_or_null();
163 const bNode *node = b.node_or_null();
164 if (node && tree) {
165 const NodeClosureOutput &storage = node_storage(*node);
166 for (const int i : IndexRange(storage.output_items.items_num)) {
167 const NodeClosureOutputItem &item = storage.output_items.items[i];
168 const eNodeSocketDatatype socket_type = item.socket_type;
170 auto &decl = b.add_input(socket_type, UString(item.name), identifier);
171 decl.socket_name_ptr(&tree->id, *ClosureOutputItemsAccessor::item_srna, &item, "name");
173 decl.structure_type(StructureType(item.structure_type));
174 }
175 else {
176 decl.structure_type(StructureType::Dynamic);
177 }
178 }
179 }
180 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
181 .custom_draw(socket_items::ui::draw_extend_socket_fn<ClosureOutputItemsAccessor>());
182 b.add_output<decl::Closure>("Closure"_ustr);
183}
184
185static void node_init(bNodeTree * /*tree*/, bNode *node)
186{
187 NodeClosureOutput *data = MEM_new<NodeClosureOutput>(__func__);
188 node->storage = data;
189}
190
191static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
192{
193 const NodeClosureOutput &src_storage = node_storage(*src_node);
194 auto *dst_storage = MEM_new<NodeClosureOutput>(__func__, dna::shallow_copy(src_storage));
195 dst_node->storage = dst_storage;
196
199}
200
207
209{
210 if (params.C && params.link.fromnode == &params.node && params.link.tosock->type == SOCK_CLOSURE)
211 {
212 const NodeClosureOutput &storage = node_storage(params.node);
213 if (storage.input_items.items_num == 0 && storage.output_items.items_num == 0) {
214 SpaceNode *snode = CTX_wm_space_node(params.C);
215 if (snode && snode->edittree == &params.ntree) {
217 ->get_corresponding_input(params.ntree, params.node);
218 if (input_node) {
219 sync_sockets_closure(*snode, *input_node, params.node, nullptr, params.link.tosock);
220 }
221 }
222 }
223 return true;
224 }
226 params.ntree, params.node, params.node, params.link);
227}
228
234
236{
237 const bNodeSocket &other_socket = params.other_socket();
238 if (other_socket.type != SOCK_CLOSURE) {
239 return;
240 }
241 if (other_socket.in_out == SOCK_OUT) {
242 return;
243 }
244 params.add_item_full_name(IFACE_("Closure"), [](LinkSearchOpParams &params) {
245 bNode &input_node = params.add_node("NodeClosureInput"_ustr);
246 bNode &output_node = params.add_node("NodeClosureOutput"_ustr);
247 output_node.location[0] = 300;
248
249 auto &input_storage = *static_cast<NodeClosureInput *>(input_node.storage);
250 input_storage.output_node_id = output_node.identifier;
251
252 params.connect_available_socket(output_node, "Closure"_ustr);
253
254 SpaceNode &snode = *CTX_wm_space_node(&params.C);
256 });
257}
258
264
270
271static void node_register()
272{
273 static bke::bNodeType ntype;
274 sh_geo_node_type_base(&ntype, "NodeClosureOutput"_ustr, NODE_CLOSURE_OUTPUT);
275 ntype.ui_name = "Closure Output";
277 ntype.declare = node_declare;
278 ntype.initfunc = node_init;
280 ntype.no_muting = true;
287 bke::node_type_storage(ntype, "NodeClosureOutput", node_free_storage, node_copy_storage);
289}
291
292} // namespace output_node
293
294} // namespace nodes::node_geo_closure_cc
295
296namespace nodes {
297
298StructRNA **ClosureInputItemsAccessor::item_srna = &RNA_NodeClosureInputItem;
299
301{
302 writer->write_string(item.name);
303}
304
309
310StructRNA **ClosureOutputItemsAccessor::item_srna = &RNA_NodeClosureOutputItem;
311
313{
314 writer->write_string(item.name);
315}
316
321
322} // namespace nodes
323} // namespace blender
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define NODE_CLOSURE_OUTPUT
#define NODE_CLOSURE_INPUT
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr IndexRange()=default
UString()=default
std::optional< int > output_node_id
const bNode * output_node() const
const bNodeTreeZone * get_zone_by_node(const int32_t node_id) const
const bNode * get_corresponding_input(const bNodeTree &tree, const bNode &output_bnode) const
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
#define storage(slot, qualifiers)
const bNodeZoneType * zone_type_by_node_type(const int node_type)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
static void node_init(bNodeTree *, bNode *node)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_label(const bNodeTree *, const bNode *, char *label, const int label_maxncpy)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_init(bNodeTree *, bNode *node)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_declare(NodeDeclarationBuilder &b)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
void sync_sockets_closure(SpaceNode &snode, bNode &closure_input_node, bNode &closure_output_node, ReportList *reports, const bNodeSocket *src_closure_socket)
BMesh Iterators.
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void sh_geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
BMesh const char void * data
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
SpaceNode * CTX_wm_space_node(const bContext *C)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void write_string(const char *data)
eNodeSocketDatatype socket_type
NodeSocketInterfaceStructureType structure_type
NodeSocketInterfaceStructureType structure_type
struct bNodeTree * edittree
eNodeSocketDatatype type
eNodeSocketInOut in_out
Defines a node type.
Definition BKE_node.hh:265
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const ItemT &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
static std::string socket_identifier_for_item(const ItemT &item)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)