Blender V5.3
node_geo_enable_output.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
7
9
10#include "BLI_stack.hh"
11
13#include "NOD_rna_define.hh"
14#include "NOD_socket.hh"
15
17#include "UI_resources.hh"
18
19#include "RNA_enum_types.hh"
20
21#include "COM_node_operation.hh"
22#include "COM_result.hh"
23
24namespace blender {
25
27
29{
30 b.use_custom_socket_order();
31 b.allow_any_socket_order();
32
33 b.add_default_layout();
34 b.add_input<decl::Bool>("Enable"_ustr)
35 .default_value(false)
36 .structure_type(StructureType::Single);
37
38 const bNode *node = b.node_or_null();
39 if (!node) {
40 return;
41 }
42 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
43
44 b.add_input(data_type, "Value"_ustr).hide_value().structure_type(StructureType::Dynamic);
45 b.add_output(data_type, "Value"_ustr)
46 .align_with_previous()
47 .inferred_structure_type()
48 .propagate_all();
49}
50
51static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
52{
53 layout.use_property_split_set(true);
54 layout.use_property_decorate_set(false);
55 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
56}
57
58class LazyFunctionForEnableOutputNode : public LazyFunction {
59 const bNode &node_;
60
61 public:
63 : node_(node)
64 {
65 debug_name_ = node.name;
66 r_lf_index_by_bsocket[node.input_socket(0).index_in_tree()] = inputs_.append_and_get_index_as(
68 r_lf_index_by_bsocket[node.input_socket(1).index_in_tree()] = inputs_.append_and_get_index_as(
69 "Value", CPPType::get<SocketValueVariant>(), lf::ValueUsage::Maybe);
70 r_lf_index_by_bsocket[node.output_socket(0).index_in_tree()] =
71 outputs_.append_and_get_index_as("Value", CPPType::get<SocketValueVariant>());
72 }
73
74 void execute_impl(lf::Params &params, const lf::Context & /*context*/) const override
75 {
76 const bke::SocketValueVariant enable_variant = params.get_input<bke::SocketValueVariant>(0);
77 if (!enable_variant.is_single()) {
79 return;
80 }
81 const bool keep = enable_variant.copy_as<bool>();
82 if (!keep) {
84 return;
85 }
86 const bke::SocketValueVariant *value_variant =
87 params.try_get_input_data_ptr_or_request<bke::SocketValueVariant>(1);
88 if (!value_variant) {
89 /* Wait until the value is available. */
90 return;
91 }
92 params.set_output(0, std::move(*value_variant));
93 }
94};
95
96static void node_init(bNodeTree * /*tree*/, bNode *node)
97{
98 node->custom1 = SOCK_FLOAT;
99}
100
101using namespace blender::compositor;
102
104 public:
106
107 void execute() override
108 {
109 const bool keep = this->get_input("Enable").get_single_value_default<bool>();
110 Result &output = this->get_result("Value");
111 if (keep) {
112 const Result &input = this->get_input("Value");
113 output.share_data(input);
114 }
115 else {
116 output.allocate_invalid();
117 }
118 }
119};
120
122{
123 return new EnableOutputOperation(context, node);
124}
125
126static const bNodeSocket *get_passthrough_socket(const bNodeSocket &query_input)
127{
128 const bNode &node = query_input.owner_node();
129 switch (node.type_legacy) {
130 case GEO_NODE_SWITCH:
133 if (query_input.index() == 0) {
134 return nullptr;
135 }
136 return &node.output_socket(0);
137 }
138 }
139 return nullptr;
140}
141
142static bool is_linked_to_group_output(const bNodeSocket &value_output_socket)
143{
144 Stack<const bNodeSocket *> outputs_to_check;
145 outputs_to_check.push(&value_output_socket);
146 Set<const bNodeSocket *> visited_sockets;
147
148 while (!outputs_to_check.is_empty()) {
149 const bNodeSocket &output_socket = *outputs_to_check.pop();
150 if (!visited_sockets.add(&output_socket)) {
151 continue;
152 }
153 for (const bNodeSocket *target_socket : output_socket.logically_linked_sockets()) {
154 const bNode &target_node = target_socket->owner_node();
155 if (target_node.is_group_output()) {
156 return true;
157 }
158 const bNodeSocket *passthrough_socket = get_passthrough_socket(*target_socket);
159 if (!passthrough_socket) {
160 continue;
161 }
162 BLI_assert(passthrough_socket->is_output());
163 outputs_to_check.push(passthrough_socket);
164 }
165 }
166 return false;
167}
168
170{
171 params.tree.ensure_topology_cache();
172 const bNodeSocket &output_socket = params.node.output_socket(0);
173 if (!output_socket.is_directly_linked()) {
174 return;
175 }
176 if (!is_linked_to_group_output(output_socket)) {
178 row.text = RPT_("Invalid Output Link");
179 row.tooltip = TIP_(
180 "This node should be linked to the group output node, optionally through switch nodes.");
181 row.icon = ICON_STATUS_ERROR;
182 params.rows.append(std::move(row));
183 }
184}
185
187 PointerRNA *ptr,
188 PropertyRNA * /*prop*/,
189 bool *r_free)
190{
191 *r_free = true;
192 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
193 bke::bNodeTreeType *ntree_type = ntree.typeinfo;
194 return enum_items_filter(
197 return ntree_type->valid_socket_type(ntree_type, socket_type);
198 });
199}
200
201static void node_rna(StructRNA *srna)
202{
204 "data_type",
205 "Data Type",
206 "",
211}
212
214 const bNode &node,
215 const bNodeSocket &output_socket)
216{
217 /* Internal links should always map corresponding input and output sockets. */
218 return node.input_by_identifier(output_socket.identifier_ustr());
219}
220
221static void node_register()
222{
223 static bke::bNodeType ntype;
224
225 common_node_type_base(&ntype, "NodeEnableOutput"_ustr);
226 ntype.ui_name = "Enable Output";
227 ntype.ui_description = "Either pass through the input value or output the fallback value";
230 ntype.initfunc = node_init;
232 ntype.declare = node_declare;
237
238 node_rna(ntype.rna_ext.srna);
239}
241
242} // namespace nodes::node_geo_enable_output_cc
243
244namespace nodes {
245
246std::unique_ptr<LazyFunction> get_enable_output_node_lazy_function(
247 const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
248{
249 using namespace node_geo_enable_output_cc;
250 return std::make_unique<LazyFunctionForEnableOutputNode>(
251 node, own_lf_graph_info.mapping.lf_index_by_bsocket);
252}
253
254} // namespace nodes
255} // namespace blender
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:490
#define GEO_NODE_MENU_SWITCH
#define GEO_NODE_SWITCH
#define GEO_NODE_INDEX_SWITCH
#define RPT_(msgid)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define output
static const CPPType & get()
bool add(const Key &key)
Definition BLI_set.hh:248
bool is_empty() const
Definition BLI_stack.hh:310
void push(const T &value)
Definition BLI_stack.hh:215
NodeOperation(Context &context, const bNode &node)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Result & get_input(StringRef identifier) const
Definition operation.cc:35
LazyFunctionForEnableOutputNode(const bNode &node, MutableSpan< int > r_lf_index_by_bsocket)
void execute_impl(lf::Params &params, const lf::Context &) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2975
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static const EnumPropertyItem * data_type_items_callback(bContext *, PointerRNA *ptr, PropertyRNA *, bool *r_free)
static bool is_linked_to_group_output(const bNodeSocket &value_output_socket)
static const bNodeSocket * get_passthrough_socket(const bNodeSocket &query_input)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_extra_info(NodeExtraInfoParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &output_socket)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void set_default_remaining_node_outputs(lf::Params &params, const bNode &node)
std::unique_ptr< LazyFunction > get_enable_output_node_lazy_function(const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bke::bNodeTreeType * typeinfo
Defines a socket type.
Definition BKE_node.hh:163
bool(*) valid_socket_type(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:569
Defines a node type.
Definition BKE_node.hh:265
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:415
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:412
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool ignore_inferred_input_socket_visibility
Definition BKE_node.hh:453
Vector< NodeExtraInfoRow > & rows
void use_property_decorate_set(bool is_decorate)
void use_property_split_set(bool is_sep)
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)