Blender V5.3
node_geo_switch.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
7
9
11#include "UI_resources.hh"
12
13#include "NOD_rna_define.hh"
14#include "NOD_socket.hh"
16
17#include "RNA_enum_types.hh"
18
19#include "COM_node_operation.hh"
20#include "COM_utilities.hh"
21
23
24namespace blender {
25
27
29
31{
32 auto &switch_decl = b.add_input<decl::Bool>("Switch"_ustr);
33 const bNodeTree *node_tree = b.tree_or_null();
34 const bNode *node = b.node_or_null();
35 if (!node_tree || !node) {
36 return;
37 }
38 const NodeSwitch &storage = node_storage(*node);
39 const eNodeSocketDatatype socket_type = storage.input_type;
40
41 auto &false_decl = b.add_input(socket_type, "False"_ustr);
42 auto &true_decl = b.add_input(socket_type, "True"_ustr);
43 auto &output_decl = b.add_output(socket_type, "Output"_ustr).propagate_all();
44
45 StructureType value_structure_type = StructureType::Dynamic;
46 StructureType condition_structure_type = StructureType::Dynamic;
47
48 if (node_tree->type == NTREE_COMPOSIT) {
49 const bool is_single_compositor_type = compositor::Result::is_single_value_only_type(
51 if (is_single_compositor_type) {
52 value_structure_type = StructureType::Single;
53 }
54 condition_structure_type = StructureType::Single;
55
56 false_decl.compositor_realization_mode(CompositorInputRealizationMode::None);
57 true_decl.compositor_realization_mode(CompositorInputRealizationMode::None);
58 }
59 if (node_tree->type == NTREE_GEOMETRY) {
60 if (socket_type_supports_fields(socket_type)) {
61 output_decl.inferred_structure_type();
62 }
63 else {
64 output_decl.inferred_structure_type({false_decl.index(), true_decl.index()});
65 }
66 }
67
68 switch_decl.structure_type(condition_structure_type);
69 false_decl.structure_type(value_structure_type);
70 true_decl.structure_type(value_structure_type);
71 output_decl.structure_type(value_structure_type);
72}
73
74static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
75{
76 layout.prop(ptr, "input_type", UI_ITEM_NONE, "", ICON_NONE);
77}
78
79static void node_init(bNodeTree *tree, bNode *node)
80{
81 NodeSwitch *data = MEM_new<NodeSwitch>(__func__);
82 data->input_type = tree->type == NTREE_GEOMETRY ? SOCK_FLOAT : SOCK_RGBA;
83 node->storage = data;
84}
85
87{
88 if (params.in_out() == SOCK_OUT) {
89 params.add_item(IFACE_("Output"), [](LinkSearchOpParams &params) {
90 bNode &node = params.add_node("GeometryNodeSwitch"_ustr);
91 node_storage(node).input_type = params.socket.type;
92 params.update_and_connect_available_socket(node, "Output"_ustr);
93 });
94 }
95 else {
96 /* Make sure the switch input comes first in the search for boolean sockets. */
97 int true_false_weights = 0;
98 if (params.other_socket().type == SOCK_BOOLEAN) {
99 params.add_item(IFACE_("Switch"), [](LinkSearchOpParams &params) {
100 bNode &node = params.add_node("GeometryNodeSwitch"_ustr);
101 params.update_and_connect_available_socket(node, "Switch"_ustr);
102 });
103 true_false_weights--;
104 }
105
106 params.add_item(
107 IFACE_("False"),
108 [](LinkSearchOpParams &params) {
109 bNode &node = params.add_node("GeometryNodeSwitch"_ustr);
110 node_storage(node).input_type = params.socket.type;
111 params.update_and_connect_available_socket(node, "False"_ustr);
112 },
113 true_false_weights);
114 params.add_item(
115 IFACE_("True"),
116 [](LinkSearchOpParams &params) {
117 bNode &node = params.add_node("GeometryNodeSwitch"_ustr);
118 node_storage(node).input_type = params.socket.type;
119 params.update_and_connect_available_socket(node, "True"_ustr);
120 },
121 true_false_weights);
122 }
123}
124
125class LazyFunctionForSwitchNode : public LazyFunction {
126 private:
127 int32_t node_id_;
128 bool can_be_field_ = false;
129 const CPPType *base_type_;
130
131 public:
132 LazyFunctionForSwitchNode(const bNode &node) : node_id_(node.identifier)
133 {
134 const NodeSwitch &storage = node_storage(node);
135 const eNodeSocketDatatype data_type = storage.input_type;
136 can_be_field_ = socket_type_supports_fields(data_type);
137
138 const bke::bNodeSocketType *socket_type = nullptr;
139 for (const bNodeSocket *socket : node.output_sockets()) {
140 if (socket->type == data_type) {
141 socket_type = socket->typeinfo;
142 break;
143 }
144 }
145 BLI_assert(socket_type != nullptr);
146 const CPPType &cpp_type = CPPType::get<SocketValueVariant>();
147 base_type_ = socket_type->base_cpp_type;
148
149 debug_name_ = node.name;
150 inputs_.append_as("Condition", CPPType::get<SocketValueVariant>());
151 inputs_.append_as("False", cpp_type, lf::ValueUsage::Maybe);
152 inputs_.append_as("True", cpp_type, lf::ValueUsage::Maybe);
153 outputs_.append_as("Value", cpp_type);
154 }
155
156 void execute_impl(lf::Params &params, const lf::Context &context) const override
157 {
158 SocketValueVariant condition_variant = params.get_input<SocketValueVariant>(0);
159 if (!condition_variant.is_context_dependent_field()) {
160 this->execute_single(condition_variant.ensure_type<bool>(), params);
161 return;
162 }
163
164 if (can_be_field_) {
165 this->execute_field(condition_variant.ensure_type<Field<bool>>(), params);
166 return;
167 }
168
169 auto &user_data = *static_cast<GeoNodesUserData *>(context.user_data);
170 auto &local_user_data = *static_cast<GeoNodesLocalUserData *>(context.local_user_data);
171 if (eval_log::NodeTreeLogger *tree_logger = local_user_data.try_get_tree_logger(user_data)) {
172 tree_logger->node_warnings.append(
173 *tree_logger->allocator,
174 {node_id_, {NodeWarningType::Error, N_("Type cannot be switched by a field")}});
175 }
176
177 this->execute_single(condition_variant.ensure_type<bool>(), params);
178 }
179
180 static constexpr int false_input_index = 1;
181 static constexpr int true_input_index = 2;
182
183 void execute_single(const bool condition, lf::Params &params) const
184 {
185 const int input_to_forward = condition ? true_input_index : false_input_index;
186 const int input_to_ignore = condition ? false_input_index : true_input_index;
187
188 params.set_input_unused(input_to_ignore);
189 void *value_to_forward = params.try_get_input_data_ptr_or_request(input_to_forward);
190 if (value_to_forward == nullptr) {
191 /* Try again when the value is available. */
192 return;
193 }
194
195 const CPPType &type = *outputs_[0].type;
196 void *output_ptr = params.get_output_data_ptr(0);
197 type.move_construct(value_to_forward, output_ptr);
198 params.output_set(0);
199 }
200
201 void execute_field(Field<bool> condition, lf::Params &params) const
202 {
203 /* When the condition is a non-constant field, we need both inputs. */
204 auto *false_value_variant = params.try_get_input_data_ptr_or_request<SocketValueVariant>(
206 auto *true_value_variant = params.try_get_input_data_ptr_or_request<SocketValueVariant>(
208 if (ELEM(nullptr, false_value_variant, true_value_variant)) {
209 /* Try again when inputs are available. */
210 return;
211 }
212
213 const MultiFunction &switch_multi_function = this->get_switch_multi_function();
214
215 GField false_field = std::move(false_value_variant->ensure_type<GField>());
216 GField true_field = std::move(true_value_variant->ensure_type<GField>());
217
218 GField output_field{FieldOperation::from(
219 switch_multi_function,
220 {std::move(condition), std::move(false_field), std::move(true_field)})};
221
222 void *output_ptr = params.get_output_data_ptr(0);
223 new (output_ptr) SocketValueVariant(SocketValueVariant::from(std::move(output_field)));
224 params.output_set(0);
225 }
226
227 const MultiFunction &get_switch_multi_function() const
228 {
229 const MultiFunction *switch_multi_function = nullptr;
230 base_type_->to_static_type<float,
231 int,
232 bool,
233 float3,
235 std::string,
237 float4x4,
238 MenuValue>([&]<typename T>() {
239 static auto switch_fn = mf::build::SI3_SO<bool, T, T, T>(
240 "Switch", [](const bool condition, const T &false_value, const T &true_value) {
241 return condition ? true_value : false_value;
242 });
243 switch_multi_function = &switch_fn;
244 });
245 BLI_assert(switch_multi_function != nullptr);
246 return *switch_multi_function;
247 }
248};
249
250using namespace blender::compositor;
251
253 public:
255
256 void execute() override
257 {
258 const Result &input = this->get_input(this->get_condition() ? "True" : "False");
259 Result &output = this->get_result("Output");
260 output.share_data(input);
261 }
262
264 {
265 return this->get_input("Switch").get_single_value_default<bool>();
266 }
267};
268
270{
271 return new SwitchOperation(context, node);
272}
273
275 const bNode &node,
276 const bNodeSocket & /*output_socket*/)
277{
278 /* Default to the False input. */
279 return &node.input_socket(1);
280}
281
282static void node_rna(StructRNA *srna)
283{
285 "input_type",
286 "Input Type",
287 "",
289 NOD_storage_enum_accessors(input_type),
291 [](bContext * /*C*/, PointerRNA *ptr, PropertyRNA * /*prop*/, bool *r_free) {
292 *r_free = true;
293 const bNodeTree &ntree = *id_cast<const bNodeTree *>(ptr->owner_id);
294 return enum_items_filter(
296 [&](const EnumPropertyItem &item) -> bool {
298 ntree.type, eNodeSocketDatatype(item.value));
299 });
300 });
301}
302
303static void register_node()
304{
305 static bke::bNodeType ntype;
306
307 common_node_type_base(&ntype, "GeometryNodeSwitch"_ustr, GEO_NODE_SWITCH);
308 ntype.ui_name = "Switch";
309 ntype.ui_description = "Switch between two inputs";
310 ntype.enum_name_legacy = "SWITCH";
312 ntype.declare = node_declare;
313 ntype.initfunc = node_init;
321
323
324 node_rna(ntype.rna_ext.srna);
325}
326NOD_REGISTER_NODE(register_node)
327
328} // namespace nodes::node_geo_switch_cc
329
330namespace nodes {
331
332std::unique_ptr<LazyFunction> get_switch_node_lazy_function(const bNode &node)
333{
334 using namespace node_geo_switch_cc;
335 BLI_assert(node.type_legacy == GEO_NODE_SWITCH);
336 return std::make_unique<LazyFunctionForSwitchNode>(node);
337}
338
339} // namespace nodes
340} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define GEO_NODE_SWITCH
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
#define UI_ITEM_NONE
#define ELEM(...)
#define output
static const CPPType & get()
void move_construct(void *src, void *dst) const
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
static bool is_single_value_only_type(ResultType type)
Definition result.cc:51
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
static SocketValueVariant from(T &&value)
void execute_impl(lf::Params &params, const lf::Context &context) const override
void execute_field(Field< bool > condition, lf::Params &params) const
void execute_single(const bool condition, lf::Params &params) const
NodeOperation(Context &context, const bNode &node)
#define storage(slot, qualifiers)
#define condition(cond)
#define T
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
bool node_tree_type_supports_socket_type_static(const int ntree_type, const eNodeSocketDatatype socket_type)
Definition node.cc:6295
ResultType socket_data_type_to_result_type(const eNodeSocketDatatype data_type, const std::optional< int > dimensions=std::nullopt)
Definition utilities.cc:39
QuaternionBase< float > Quaternion
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static NodeOperation * get_compositor_operation(Context &context, const bNode &node)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_init(bNodeTree *tree, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_rna(StructRNA *srna)
static const bNodeSocket * node_internally_linked_input(const bNodeTree &, const bNode &node, const bNodeSocket &)
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)
std::unique_ptr< LazyFunction > get_switch_node_lazy_function(const bNode &node)
bool socket_type_supports_fields(const eNodeSocketDatatype socket_type)
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)
BMesh const char void * data
signed int int32_t
Definition BKE_text.h:78
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eNodeSocketDatatype type
eNodeTree_Type type
Defines a socket type.
Definition BKE_node.hh:163
const CPPType * base_cpp_type
Definition BKE_node.hh:210
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
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 ignore_inferred_input_socket_visibility
Definition BKE_node.hh:453
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)