Blender V5.0
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
6
8
10#include "NOD_rna_define.hh"
11#include "NOD_socket.hh"
12
14#include "UI_resources.hh"
15
16#include "RNA_enum_types.hh"
17
18#include "COM_node_operation.hh"
19#include "COM_result.hh"
20
22
24{
25 b.use_custom_socket_order();
26 b.allow_any_socket_order();
27
28 b.add_default_layout();
29 b.add_input<decl::Bool>("Enable").default_value(false).structure_type(StructureType::Single);
30
31 const bNode *node = b.node_or_null();
32 if (!node) {
33 return;
34 }
35 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
36
37 auto &input_value = b.add_input(data_type, "Value").hide_value();
38 auto &output_value = b.add_output(data_type, "Value").align_with_previous();
39
41 input_value.supports_field();
42 }
43
45 output_value.propagate_all();
46 }
47
49 output_value.reference_pass_all();
50 }
51
52 input_value.structure_type(StructureType::Dynamic);
53 output_value.structure_type(StructureType::Dynamic);
54}
55
56static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
57{
58 layout->use_property_split_set(true);
59 layout->use_property_decorate_set(false);
60 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
61}
62
64 const bNode &node_;
65
66 public:
67 LazyFunctionForEnableOutputNode(const bNode &node, MutableSpan<int> r_lf_index_by_bsocket)
68 : node_(node)
69 {
70 r_lf_index_by_bsocket[node.input_socket(0).index_in_tree()] = inputs_.append_and_get_index_as(
72 r_lf_index_by_bsocket[node.input_socket(1).index_in_tree()] = inputs_.append_and_get_index_as(
74 r_lf_index_by_bsocket[node.output_socket(0).index_in_tree()] =
75 outputs_.append_and_get_index_as("Value", CPPType::get<SocketValueVariant>());
76 }
77
78 void execute_impl(lf::Params &params, const lf::Context & /*context*/) const override
79 {
80 const bke::SocketValueVariant enable_variant = params.get_input<bke::SocketValueVariant>(0);
81 if (!enable_variant.is_single()) {
83 return;
84 }
85 const bool keep = enable_variant.get<bool>();
86 if (!keep) {
88 return;
89 }
90 const bke::SocketValueVariant *value_variant =
91 params.try_get_input_data_ptr_or_request<bke::SocketValueVariant>(1);
92 if (!value_variant) {
93 /* Wait until the value is available. */
94 return;
95 }
96 params.set_output(0, std::move(*value_variant));
97 }
98};
99
100static void node_init(bNodeTree * /*tree*/, bNode *node)
101{
102 node->custom1 = SOCK_FLOAT;
103}
104
105using namespace blender::compositor;
106
108 public:
110
111 void execute() override
112 {
113 const bool keep = this->get_input("Enable").get_single_value_default<bool>(true);
114 Result &output = this->get_result("Value");
115 if (keep) {
116 const Result &input = this->get_input("Value");
117 output.share_data(input);
118 }
119 else {
120 output.allocate_invalid();
121 }
122 }
123};
124
126{
127 return new EnableOutputOperation(context, node);
128}
129
131{
132 params.tree.ensure_topology_cache();
133 const bNodeSocket &output_socket = params.node.output_socket(0);
134 if (!output_socket.is_directly_linked()) {
135 return;
136 }
137 for (const bNodeSocket *target_socket : output_socket.logically_linked_sockets()) {
138 const bNode &target_node = target_socket->owner_node();
139 if (!target_node.is_group_output() && !target_node.is_reroute()) {
141 row.text = RPT_("Invalid Output Link");
142 row.tooltip = TIP_("This node should be linked to the group output node");
143 row.icon = ICON_ERROR;
144 params.rows.append(std::move(row));
145 return;
146 }
147 }
148}
149
152 PropertyRNA * /*prop*/,
153 bool *r_free)
154{
155 *r_free = true;
156 const bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
157 blender::bke::bNodeTreeType *ntree_type = ntree.typeinfo;
158 return enum_items_filter(
161 return ntree_type->valid_socket_type(ntree_type, socket_type);
162 });
163}
164
165static void node_rna(StructRNA *srna)
166{
168 "data_type",
169 "Data Type",
170 "",
175}
176
178 const bNode &node,
179 const bNodeSocket &output_socket)
180{
181 /* Internal links should always map corresponding input and output sockets. */
182 return node.input_by_identifier(output_socket.identifier);
183}
184
185static void node_register()
186{
187 static blender::bke::bNodeType ntype;
188
189 geo_cmp_node_type_base(&ntype, "NodeEnableOutput");
190 ntype.ui_name = "Enable Output";
191 ntype.ui_description = "Either pass through the input value or output the fallback value";
194 ntype.initfunc = node_init;
196 ntype.declare = node_declare;
201
202 node_rna(ntype.rna_ext.srna);
203}
205
206} // namespace blender::nodes::node_geo_enable_output_cc
207
208namespace blender::nodes {
209
210std::unique_ptr<LazyFunction> get_enable_output_node_lazy_function(
211 const bNode &node, GeometryNodesLazyFunctionGraphInfo &own_lf_graph_info)
212{
213 using namespace node_geo_enable_output_cc;
214 return std::make_unique<LazyFunctionForEnableOutputNode>(
215 node, own_lf_graph_info.mapping.lf_index_by_bsocket);
216}
217
218} // namespace blender::nodes
#define NODE_CLASS_INTERFACE
Definition BKE_node.hh:459
#define RPT_(msgid)
#define TIP_(msgid)
eNodeSocketDatatype
@ SOCK_FLOAT
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
static const CPPType & get()
NodeOperation(Context &context, DNode node)
Result & get_result(StringRef identifier)
Definition operation.cc:39
Result & get_input(StringRef identifier) const
Definition operation.cc:138
T get_single_value_default(const T &default_value) const
LazyFunctionForEnableOutputNode(const bNode &node, MutableSpan< int > r_lf_index_by_bsocket)
void execute_impl(lf::Params &params, const lf::Context &) const override
#define input
#define output
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
bool can_contain_reference(eNodeSocketDatatype socket_type)
bool can_contain_referenced_data(eNodeSocketDatatype socket_type)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bNodeSocketType * node_socket_type_find_static(int type, int subtype=0)
Definition node.cc:2471
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 NodeOperation * node_get_compositor_operation(Context &context, DNode node)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_extra_info(NodeExtraInfoParams &params)
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)
bool socket_type_supports_fields(const eNodeSocketDatatype socket_type)
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)
void geo_cmp_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
StructRNA * srna
char identifier[64]
bNodeTreeTypeHandle * typeinfo
int16_t custom1
Defines a socket type.
Definition BKE_node.hh:158
bool(* valid_socket_type)(bNodeTreeType *ntreetype, bNodeSocketType *socket_type)
Definition BKE_node.hh:529
Defines a node type.
Definition BKE_node.hh:238
NodeInternallyLinkedInputFunction internally_linked_input
Definition BKE_node.hh:384
std::string ui_description
Definition BKE_node.hh:244
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:348
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeExtraInfoFunction get_extra_info
Definition BKE_node.hh:381
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
bool ignore_inferred_input_socket_visibility
Definition BKE_node.hh:422
void use_property_decorate_set(bool is_sep)
void use_property_split_set(bool value)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238