Blender V5.3
node_geo_repeat.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string.hh"
6#include "BLI_string_utf8.hh"
7
8#include "NOD_geo_repeat.hh"
9#include "NOD_socket.hh"
14
15#include "BLO_read_write.hh"
16
17#include "RNA_access.hh"
18#include "RNA_prototypes.hh"
19
20#include "BKE_screen.hh"
21
22#include "WM_api.hh"
23
25
26#include "node_geometry_util.hh"
28
29namespace blender {
30
32
34static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *current_node_ptr)
35{
36 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(current_node_ptr->owner_id);
37 bNode *current_node = static_cast<bNode *>(current_node_ptr->data);
38
39 const bke::bNodeTreeZones *zones = ntree.zones();
40 if (!zones) {
41 return;
42 }
43 const bke::bNodeTreeZone *zone = zones->get_zone_by_node(current_node->identifier);
44 if (!zone) {
45 return;
46 }
47 if (!zone->output_node_id) {
48 return;
49 }
50 bNode &output_node = const_cast<bNode &>(*zone->output_node());
52 current_node_ptr->owner_id, RNA_Node, &output_node);
53
54 if (ui::Layout *panel = layout.panel(C, "repeat_items", false, IFACE_("Repeat Items"))) {
55 socket_items::ui::draw_items_list_with_operators<RepeatItemsAccessor>(
56 C, panel, ntree, output_node);
57 socket_items::ui::draw_active_item_props<RepeatItemsAccessor>(
58 ntree, output_node, [&](PointerRNA *item_ptr) {
59 panel->use_property_split_set(true);
60 panel->use_property_decorate_set(false);
61 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
62 });
63 }
64
65 layout.prop(&output_node_ptr, "inspection_index", UI_ITEM_NONE, std::nullopt, ICON_NONE);
66}
67
69
71
73{
74 b.use_custom_socket_order();
75 b.allow_any_socket_order();
76 b.add_output<decl::Int>("Iteration"_ustr)
77 .description("Index of the current iteration. Starts counting at zero");
78 b.add_input<decl::Int>("Iterations"_ustr).min(0).default_value(1);
79
80 const bNode *node = b.node_or_null();
81 const bNodeTree *tree = b.tree_or_null();
82 if (node && tree) {
83 const NodeGeometryRepeatInput &storage = node_storage(*node);
84 if (const bNode *output_node = tree->node_by_id(storage.output_node_id)) {
85 const auto &output_storage = *static_cast<const NodeGeometryRepeatOutput *>(
86 output_node->storage);
87 for (const int i : IndexRange(output_storage.items_num)) {
88 const NodeRepeatItem &item = output_storage.items[i];
89 const eNodeSocketDatatype socket_type = item.socket_type;
90 const UString name = item.name ? UString(item.name) : ""_ustr;
92 auto &input_decl = b.add_input(socket_type, name, identifier)
93 .socket_name_ptr(
94 &tree->id, *RepeatItemsAccessor::item_srna, &item, "name")
95 .structure_type(StructureType::Dynamic);
96 b.add_output(socket_type, name, identifier)
97 .align_with_previous()
98 .propagate_all({input_decl.index()})
99 .inferred_structure_type({input_decl.index()})
100 .structure_type(StructureType::Dynamic);
101 }
102 }
103 }
104 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
105 .structure_type(StructureType::Dynamic)
106 .custom_draw(socket_items::ui::draw_extend_socket_fn<RepeatItemsAccessor>());
107 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
108 .structure_type(StructureType::Dynamic)
109 .align_with_previous();
110}
111
112static void node_init(bNodeTree * /*tree*/, bNode *node)
113{
114 NodeGeometryRepeatInput *data = MEM_new<NodeGeometryRepeatInput>(__func__);
115 /* Needs to be initialized for the node to work. */
116 data->output_node_id = 0;
117 node->storage = data;
118}
119
120static void node_label(const bNodeTree * /*ntree*/,
121 const bNode * /*node*/,
122 char *label,
123 const int label_maxncpy)
124{
125 BLI_strncpy_utf8(label, CTX_IFACE_(BLT_I18NCONTEXT_ID_NODETREE, "Repeat"), label_maxncpy);
126}
127
129{
130 bNode *output_node = params.ntree.node_by_id(node_storage(params.node).output_node_id);
131 if (!output_node) {
132 return true;
133 }
135 params.ntree, params.node, *output_node, params.link);
136}
137
139 bNode *node,
140 bNodeExecData * /*execdata*/,
143{
144 int zone_id = node_storage(*node).output_node_id;
145 return GPU_stack_link_zone(mat, node, "REPEAT_BEGIN", in, out, zone_id, false, 1, 1);
146}
147
148static void node_register()
149{
150 static bke::bNodeType ntype;
151 sh_geo_node_type_base(&ntype, "GeometryNodeRepeatInput"_ustr, GEO_NODE_REPEAT_INPUT);
152 ntype.ui_name = "Repeat Input";
153 ntype.enum_name_legacy = "REPEAT_INPUT";
155 ntype.initfunc = node_init;
156 ntype.declare = node_declare;
157 ntype.labelfunc = node_label;
158 ntype.gather_link_search_ops = nullptr;
160 ntype.no_muting = true;
162 ntype.gpu_fn = node_shader_fn;
164 ntype, "NodeGeometryRepeatInput", node_free_standard_storage, node_copy_standard_storage);
166}
168
169} // namespace repeat_input_node
170
172
174
176{
177 b.use_custom_socket_order();
178 b.allow_any_socket_order();
179 const bNodeTree *tree = b.tree_or_null();
180 const bNode *node = b.node_or_null();
181 if (node) {
182 const NodeGeometryRepeatOutput &storage = node_storage(*node);
183 for (const int i : IndexRange(storage.items_num)) {
184 const NodeRepeatItem &item = storage.items[i];
185 const eNodeSocketDatatype socket_type = item.socket_type;
186 const UString name = item.name ? UString(item.name) : ""_ustr;
188 auto &input_decl = b.add_input(socket_type, name, identifier)
189 .socket_name_ptr(
190 &tree->id, *RepeatItemsAccessor::item_srna, &item, "name")
191 .structure_type(StructureType::Dynamic);
192 b.add_output(socket_type, name, identifier)
193 .align_with_previous()
194 .propagate_all({input_decl.index()})
195 .inferred_structure_type({input_decl.index()})
196 .structure_type(StructureType::Dynamic);
197 }
198 }
199 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
200 .structure_type(StructureType::Dynamic)
201 .custom_draw(socket_items::ui::draw_extend_socket_fn<RepeatItemsAccessor>());
202 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
203 .structure_type(StructureType::Dynamic)
204 .align_with_previous();
205}
206
207static void node_init(bNodeTree *tree, bNode *node)
208{
209 NodeGeometryRepeatOutput *data = MEM_new<NodeGeometryRepeatOutput>(__func__);
210
211 data->next_identifier = 0;
212
213 if (tree->type == NTREE_GEOMETRY) {
214 data->items = MEM_new_array<NodeRepeatItem>(1, __func__);
215 data->items[0].name = BLI_strdup(DATA_("Geometry"));
216 data->items[0].socket_type = SOCK_GEOMETRY;
217 data->items[0].identifier = data->next_identifier++;
218 data->items_num = 1;
219 }
220
221 node->storage = data;
222}
223
225{
227 MEM_delete(reinterpret_cast<NodeGeometryRepeatOutput *>(node->storage));
228}
229
230static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
231{
232 const NodeGeometryRepeatOutput &src_storage = node_storage(*src_node);
233 auto *dst_storage = MEM_new<NodeGeometryRepeatOutput>(__func__, dna::shallow_copy(src_storage));
234 dst_node->storage = dst_storage;
235
237}
238
244
249
251{
252 const bNodeSocket &other_socket = params.other_socket();
253 if (!RepeatItemsAccessor::supports_socket_type(other_socket.type, params.node_tree().type)) {
254 return;
255 }
256 if (other_socket.in_out == SOCK_OUT) {
257 params.add_item_full_name(
258 std::string(IFACE_("Repeat")) + " " + UI_MENU_ARROW_SEP + IFACE_("Iterations"),
259 [](LinkSearchOpParams &params) {
260 bNode &input_node = params.add_node("GeometryNodeRepeatInput"_ustr);
261 bNode &output_node = params.add_node("GeometryNodeRepeatOutput"_ustr);
262 output_node.location[0] = 300;
263
264 auto &input_storage = *static_cast<NodeGeometryRepeatInput *>(input_node.storage);
265 input_storage.output_node_id = output_node.identifier;
267
268 params.update_and_connect_available_socket(input_node, "Iterations"_ustr);
269 });
270 }
271 params.add_item_full_name(IFACE_("Repeat"), [](LinkSearchOpParams &params) {
272 bNode &input_node = params.add_node("GeometryNodeRepeatInput"_ustr);
273 bNode &output_node = params.add_node("GeometryNodeRepeatOutput"_ustr);
274 output_node.location[0] = 300;
275
276 auto &input_storage = *static_cast<NodeGeometryRepeatInput *>(input_node.storage);
277 input_storage.output_node_id = output_node.identifier;
278
280 const UString name(params.socket.name);
282 params.node_tree, output_node, params.socket.type, name.c_str());
285 bNode &node = params.socket.in_out == SOCK_IN ? output_node : input_node;
286 params.connect_available_socket_by_identifier(
288 params.node_tree.ensure_topology_cache();
290 input_node,
291 input_node.output_socket(1),
292 output_node,
293 output_node.input_socket(0));
294 });
295}
296
297static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
298{
300}
301
306
308 bNode *node,
309 bNodeExecData * /*execdata*/,
312{
313 int zone_id = node->identifier;
314 return GPU_stack_link_zone(mat, node, "REPEAT_END", in, out, zone_id, true, 0, 0);
315}
316
317static void node_register()
318{
319 static bke::bNodeType ntype;
320 sh_geo_node_type_base(&ntype, "GeometryNodeRepeatOutput"_ustr, GEO_NODE_REPEAT_OUTPUT);
321 ntype.ui_name = "Repeat Output";
322 ntype.enum_name_legacy = "REPEAT_OUTPUT";
324 ntype.initfunc = node_init;
325 ntype.declare = node_declare;
329 ntype.no_muting = true;
334 ntype.gpu_fn = node_shader_fn;
335 bke::node_type_storage(ntype, "NodeGeometryRepeatOutput", node_free_storage, node_copy_storage);
337}
339
340} // namespace repeat_output_node
341
342} // namespace nodes::node_geo_repeat_cc
343
344namespace nodes {
345
346StructRNA **RepeatItemsAccessor::item_srna = &RNA_RepeatItem;
347
349{
350 writer->write_string(item.name);
351}
352
354{
355 BLO_read_string(reader, &item.name);
356}
357
358} // namespace nodes
359
360Span<NodeRepeatItem> NodeGeometryRepeatOutput::items_span() const
361{
363}
364
365MutableSpan<NodeRepeatItem> NodeGeometryRepeatOutput::items_span()
366{
368}
369
370} // namespace blender
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define GEO_NODE_REPEAT_OUTPUT
#define GEO_NODE_REPEAT_INPUT
#define BLT_I18NCONTEXT_ID_NODETREE
#define CTX_IFACE_(context, msgid)
#define IFACE_(msgid)
#define DATA_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
constexpr IndexRange()=default
constexpr MutableSpan()=default
constexpr Span()=default
const char * c_str() const
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
void add_item_full_name(std::string name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeLink & node_add_link(bNodeTree &ntree, bNode &fromnode, bNodeSocket &fromsock, bNode &tonode, bNodeSocket &tosock)
Definition node.cc:4638
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 int node_shader_fn(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_label(const bNodeTree *, const bNode *, char *label, const int label_maxncpy)
static void node_declare(NodeDeclarationBuilder &b)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static int node_shader_fn(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_declare(NodeDeclarationBuilder &b)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void node_init(bNodeTree *tree, bNode *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)
Accessor::ItemT * add_item_with_socket_type_and_name(bNodeTree &ntree, bNode &node, const eNodeSocketDatatype socket_type, const char *name, std::optional< int > dimensions=std::nullopt)
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 update_node_declaration_and_sockets(bNodeTree &ntree, bNode &node, Main *bmain)
BMesh Iterators.
char * BLI_strncpy_utf8(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
bool GPU_stack_link_zone(GPUMaterial *material, const bNode *bnode, const char *name, GPUNodeStack *in, GPUNodeStack *out, int zone_index, bool is_zone_end, int in_argument_count, int out_argument_count)
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)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
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
#define min(a, b)
Definition sort.cc:38
#define UI_MENU_ARROW_SEP
void write_string(const char *data)
eNodeSocketDatatype socket_type
eNodeSocketDatatype type
eNodeSocketInOut in_out
eNodeTree_Type type
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
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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
const char * enum_name_legacy
Definition BKE_node.hh:274
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_write_item(BlendWriter *writer, const ItemT &item)
static bool supports_socket_type(const eNodeSocketDatatype socket_type, const int ntree_type)
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const NodeRepeatItem &item)
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)