Blender V5.0
node_fn_string_to_value.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
5#include "BLI_string_utf8.h"
6
7#include "fast_float.h"
8
10
11#include "NOD_rna_define.hh"
13
15#include "UI_resources.hh"
16
17#include <charconv>
18
20
22{
23 b.add_input<decl::String>("String").optional_label();
24
25 const bNode *node = b.node_or_null();
26 if (node != nullptr) {
27 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
28 b.add_output(data_type, "Value");
29 }
30
31 b.add_output<decl::Int>("Length");
32}
33
34static const mf::MultiFunction *get_multi_function(const bNode &bnode)
35{
36 static auto str_to_float_fn = mf::build::SI1_SO2<std::string, float, int>(
37 "String to Value", [](const std::string &s, float &value, int &length) -> void {
38 const auto result = fast_float::from_chars(s.data(), s.data() + s.size(), value);
39 length = BLI_strnlen_utf8(s.data(), result.ptr - s.data());
40 });
41
42 static auto str_to_int_fn = mf::build::SI1_SO2<std::string, int, int>(
43 "String to Value", [](const std::string &s, int &value, int &length) -> void {
44 const auto result = std::from_chars(s.data(), s.data() + s.size(), value);
45 length = BLI_strnlen_utf8(s.data(), result.ptr - s.data());
46 });
47
48 switch (eNodeSocketDatatype(bnode.custom1)) {
49 case SOCK_FLOAT:
50 return &str_to_float_fn;
51 case SOCK_INT:
52 return &str_to_int_fn;
53 default:
55 return nullptr;
56 }
57}
58
60{
61 const mf::MultiFunction *fn = get_multi_function(builder.node());
62 builder.set_matching_fn(fn);
63}
64
65static void node_init(bNodeTree *, bNode *node)
66{
67 node->custom1 = SOCK_FLOAT;
68}
69
71{
72 const eNodeSocketDatatype socket_type = eNodeSocketDatatype(params.other_socket().type);
73 if (params.in_out() == SOCK_IN) {
74 if (socket_type == SOCK_STRING) {
75 params.add_item(IFACE_("String"), [](LinkSearchOpParams &params) {
76 bNode &node = params.add_node("FunctionNodeStringToValue");
77 params.update_and_connect_available_socket(node, "String");
78 });
79 }
80 }
81 else if (params.in_out() == SOCK_OUT) {
82 if (ELEM(socket_type, SOCK_INT, SOCK_BOOLEAN)) {
83 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
84 bNode &node = params.add_node("FunctionNodeStringToValue");
85 node.custom1 = SOCK_INT;
86 params.update_and_connect_available_socket(node, "Value");
87 });
88 }
89 else if (params.node_tree().typeinfo->validate_link(SOCK_FLOAT, socket_type)) {
90 params.add_item(IFACE_("Value"), [](LinkSearchOpParams &params) {
91 bNode &node = params.add_node("FunctionNodeStringToValue");
92 node.custom1 = SOCK_FLOAT;
93 params.update_and_connect_available_socket(node, "Value");
94 });
95 }
96
97 if (socket_type == SOCK_INT) {
98 params.add_item(IFACE_("Length"), [](LinkSearchOpParams &params) {
99 bNode &node = params.add_node("FunctionNodeStringToValue");
100 params.update_and_connect_available_socket(node, "Length");
101 });
102 }
103 }
104}
105
106static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
107{
108 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
109}
110
111static void node_rna(StructRNA *srna)
112{
113 static const EnumPropertyItem data_types[] = {
114 {SOCK_FLOAT, "FLOAT", ICON_NODE_SOCKET_FLOAT, "Float", "Floating-point value"},
115 {SOCK_INT, "INT", ICON_NODE_SOCKET_INT, "Integer", "32-bit integer"},
116 {0, nullptr, 0, nullptr, nullptr}};
117
119 "data_type",
120 "Data Type",
121 "",
122 data_types,
124 SOCK_FLOAT);
125}
126
127static void node_register()
128{
129 static blender::bke::bNodeType ntype;
130
131 fn_node_type_base(&ntype, "FunctionNodeStringToValue");
132 ntype.ui_name = "String to Value";
133 ntype.ui_description = "Derive a numeric value from a given string representation";
135 ntype.declare = node_declare;
136 ntype.initfunc = node_init;
141
142 node_rna(ntype.rna_ext.srna);
143}
145
146} // namespace blender::nodes::node_fn_string_to_value_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:453
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
size_t size_t BLI_strnlen_utf8(const char *strc, size_t strc_maxlen) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
#define ELEM(...)
#define IFACE_(msgid)
@ SOCK_OUT
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_BOOLEAN
@ SOCK_FLOAT
@ SOCK_STRING
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void set_matching_fn(const mf::MultiFunction *fn)
float length(VecOp< float, D >) RET
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static const mf::MultiFunction * get_multi_function(const bNode &bnode)
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 fn_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
StructRNA * srna
int16_t custom1
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:351
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:378
NodeDeclareFunction declare
Definition BKE_node.hh:362
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