Blender V5.3
node_fn_format_string.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 <charconv>
6#include <fmt/format.h>
7#include <regex>
8
9#include "RNA_enum_types.hh"
10
12#include "UI_resources.hh"
13
14#include "BLO_read_write.hh"
15
22
23#include "BKE_path_templates.hh"
24
25#include "node_function_util.hh"
26#include "node_shader_util.hh"
27
28namespace blender {
29
31
33
35{
36 b.is_function_node();
37 b.use_custom_socket_order();
38 b.allow_any_socket_order();
39
40 b.add_input<decl::String>("Format"_ustr)
42 .description(
43 "Format string using a Python and path template compatible syntax. For example, "
44 "\"Count: "
45 "{}\" would replace the {} with the first input value.");
46 b.add_output<decl::String>("String"_ustr).align_with_previous();
47
48 const bNodeTree *ntree = b.tree_or_null();
49 const bNode *node = b.node_or_null();
50 if (!ntree || !node) {
51 return;
52 }
53
54 const NodeFunctionFormatString &storage = node_storage(*node);
55 for (const int i : IndexRange(storage.items_num)) {
56 const NodeFunctionFormatStringItem &item = storage.items[i];
57 const eNodeSocketDatatype socket_type = item.socket_type;
58 const UString name(item.name);
59 const std::string identifier = FormatStringItemsAccessor::socket_identifier_for_item(item);
60 b.add_input(socket_type, name, UString(identifier))
61 .socket_name_ptr(&ntree->id, *FormatStringItemsAccessor::item_srna, &item, "name");
62 }
63
64 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr)
65 .custom_draw(socket_items::ui::draw_extend_socket_fn<FormatStringItemsAccessor>());
66}
67
68static void node_init(bNodeTree * /*tree*/, bNode *node)
69{
70 NodeFunctionFormatString *data = MEM_new<NodeFunctionFormatString>(__func__);
71 node->storage = data;
72}
73
74static void node_copy_storage(bNodeTree * /*tree*/, bNode *dst_node, const bNode *src_node)
75{
76 const NodeFunctionFormatString &src_storage = node_storage(*src_node);
77 auto *dst_storage = MEM_new<NodeFunctionFormatString>(__func__, dna::shallow_copy(src_storage));
78 dst_node->storage = dst_storage;
79
81}
82
88
94
99
100static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
101{
102 bNodeTree &tree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
103 bNode &node = *ptr->data_as<bNode>();
104 if (ui::Layout *panel = layout.panel(C, "format_string_items", false, IFACE_("Format Items"))) {
105 socket_items::ui::draw_items_list_with_operators<FormatStringItemsAccessor>(
106 C, panel, tree, node);
107 socket_items::ui::draw_active_item_props<FormatStringItemsAccessor>(
108 tree, node, [&](PointerRNA *item_ptr) {
109 panel->use_property_split_set(true);
110 panel->use_property_decorate_set(false);
111 panel->prop(item_ptr, "socket_type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
112 });
113 }
114}
115
116static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
117{
119}
120
125
126static std::optional<StringRef> find_format_specifier(const StringRef format)
127{
128 BLI_assert(format[0] == '{');
129 int64_t braces_depth = 1;
130 for (const char &c : format.substr(1)) {
131 if (c == '{') {
132 braces_depth++;
133 }
134 else if (c == '}') {
135 braces_depth--;
136 }
137 if (braces_depth == 0) {
138 const int length = &c - format.data() + 1;
139 return format.substr(0, length);
140 }
141 }
142 return std::nullopt;
143}
144
147 const int64_t start,
148 std::string &r_out,
149 std::optional<std::string> &r_error)
150{
151 int64_t i = start;
152 while (i < format.size()) {
153 const char c = format[i];
154 switch (c) {
155 case '{': {
156 /* '{{' is an escaped '{'. */
157 if (i + 1 < format.size()) {
158 const char next_c = format[i + 1];
159 if (next_c == c) {
160 i += 2;
161 r_out += c;
162 continue;
163 }
164 }
165 /* Not escaped, so this is the start of the next format group. */
166 return i;
167 }
168 case '}': {
169 /* '}}' is an escaped '}'. */
170 if (i + 1 < format.size()) {
171 const char next_c = format[i + 1];
172 if (next_c == c) {
173 i += 2;
174 r_out += c;
175 continue;
176 }
177 }
178 if (!r_error) {
179 r_error = fmt::format("{}: \"{}\"", TIP_("Unescaped '}' in format string"), format);
180 }
181 return -1;
182 }
183 default: {
184 r_out += c;
185 i++;
186 break;
187 }
188 }
189 }
190 return format.size();
191}
192
194 std::string pattern_str;
195 std::regex pattern;
197 std::optional<int> precision_group;
198};
199
202{
203 std::string pattern;
204 int groups_num = 0;
205
206 /* Beginning of string. */
207 pattern += '^';
208 /* Fill and Align. */
209 pattern += "([^{}]?[<>^])?";
210 groups_num += 1;
211 if (type.is<float>() || type.is<int>()) {
212 /* Sign. */
213 pattern += "[+\\- ]?";
214 /* '#' for alternate form is omitted for better potential future compatibility with
215 * path templates (#BKE_path_apply_template). */
216 /* Sign-aware zero padding. */
217 pattern += "0?";
218 }
219 /* A width cannot start with 0, as 0 is parsed as the padding flag. */
220 const std::string width_integer_or_identifier = "([1-9]\\d*|(\\{.*\\}))";
221 pattern += width_integer_or_identifier;
222 pattern += "?";
223 groups_num += 2;
224 const int width_group = groups_num;
225
226 std::optional<int> precision_group;
227 if (type.is<float>() || type.is<std::string>()) {
228 /* Precision is allowed to be 0. */
229 const std::string precision_integer_or_identifier = "(\\d+|(\\{.*\\}))";
230 pattern += "(\\.";
231 pattern += precision_integer_or_identifier;
232 pattern += ")?";
233 groups_num += 3;
234 precision_group = groups_num;
235 }
236 /* "L" is omitted, because we take the current locale into account in Geometry Nodes. */
237 /* Allowed type specifiers vary by data type. */
238 if (type.is<std::string>()) {
239 pattern += "[s\\?]?";
240 }
241 else if (type.is<int>()) {
242 pattern += "[bBcdoxX]?";
243 }
244 else if (type.is<float>()) {
245 pattern += "[aAeEfFgG]?";
246 }
247 /* End of string. */
248 pattern += '$';
249 return {pattern, std::regex{pattern}, width_group, precision_group};
250}
251
253{
254 if (type.is<float>()) {
256 return &info;
257 }
258 if (type.is<int>()) {
260 return &info;
261 }
262 if (type.is<std::string>()) {
264 return &info;
265 }
266 return nullptr;
267}
268
270 private:
271 const Span<GVArray> inputs_;
272 const VectorSet<std::string> &input_names_;
273 int64_t next_auto_index_ = 0;
278 bool non_auto_index_used_ = false;
279
280 public:
282 : inputs_(inputs), input_names_(input_names)
283 {
284 }
285
286 const GVArray *find_next_input(const StringRef identifier, std::optional<std::string> &r_error)
287 {
288 const std::optional<int64_t> input_index = this->find_next_input_index(identifier, r_error);
289 if (!input_index.has_value()) {
290 return nullptr;
291 }
292 return &inputs_[*input_index];
293 }
294
295 std::optional<int64_t> find_next_input_index(const StringRef identifier,
296 std::optional<std::string> &r_error)
297 {
298 if (identifier.is_empty()) {
299 if (non_auto_index_used_) {
300 /* Once the first explicit identifier is used, it's not allowed to use the auto-index
301 * anymore. Only other explicit identifiers are allowed. */
302 if (!r_error) {
303 r_error = TIP_(
304 "Empty identifier cannot be used when explicit identifier was used before. For "
305 "example, \"{} {x}\" is ok but \"{x} {}\" is not.");
306 }
307 return std::nullopt;
308 }
309 if (next_auto_index_ == inputs_.size()) {
310 /* Not enough inputs provided. */
311 if (!r_error) {
312 r_error = TIP_("Format uses more inputs than provided.");
313 }
314 return std::nullopt;
315 }
316 return next_auto_index_++;
317 }
318 non_auto_index_used_ = true;
319 if (std::isdigit(identifier[0])) {
321 std::from_chars_result res = std::from_chars(identifier.begin(), identifier.end(), index);
322 if (res.ec != std::errc()) {
323 if (!r_error) {
324 r_error = fmt::format(fmt::runtime(TIP_("Invalid identifier: \"{}\"")), identifier);
325 }
326 return std::nullopt;
327 }
328 if (res.ptr < identifier.end()) {
329 /* There are other characters after the number. */
330 if (!r_error) {
331 r_error = fmt::format(
332 fmt::runtime(TIP_("An input name cannot start with a digit: \"{}\"")), identifier);
333 }
334 return std::nullopt;
335 }
336 if (index >= inputs_.size()) {
337 if (!r_error) {
338 if (inputs_.is_empty()) {
339 r_error = fmt::format(fmt::runtime(TIP_("There are no inputs.")), identifier);
340 }
341 else {
342 r_error = fmt::format(
343 fmt::runtime(TIP_("Input with index {} does not exist. Currently, the maximum "
344 "possible index is {}. Did you mean to use {{:{}}}?")),
345 identifier,
346 inputs_.size() - 1,
347 identifier);
348 }
349 }
350 return std::nullopt;
351 }
352 return index;
353 }
354 const int index = input_names_.index_of_try_as(identifier);
355 if (index == -1) {
356 if (!r_error) {
357 r_error = fmt::format(fmt::runtime(TIP_("Input does not exist: \"{}\"")), identifier);
358 }
359 return std::nullopt;
360 }
361 return index;
362 }
363};
364
366 const GVArray *widths = nullptr;
367 const GVArray *precisions = nullptr;
372 std::string fmt_format_str;
373};
374
376 const StringRef format_outer,
377 const FormatPatternInfo &pattern)
378{
379 for (const char c : format) {
380 if (pattern.pattern_str.find(c) == std::string::npos && std::isprint(c) && !std::isdigit(c)) {
381 return fmt::format(
382 fmt::runtime(TIP_("Format contains unsupported \"{}\" character: \"{}\"")),
383 c,
384 format_outer);
385 }
386 }
387 return fmt::format(fmt::runtime(TIP_("Invalid format: \"{}\"")), format_outer);
388}
389
390static std::optional<ProcessedPythonCompatibleFormat> preprocess_python_compatible_syntax(
391 const StringRef format,
392 const StringRef format_outer,
393 const CPPType &type,
394 FormatInputsLookup &inputs_lookup,
395 std::optional<std::string> &r_error)
396{
397 const FormatPatternInfo *allowed_pattern = get_pattern_by_type(type);
398 if (!allowed_pattern) {
399 /* The type can't be formatted. The user shouldn't be able to trigger this error but nice to
400 * handle it anyway. */
401 if (!r_error) {
402 r_error = fmt::format(fmt::runtime(TIP_("Type \"{}\" cannot be formatted")), type.name());
403 }
404 return std::nullopt;
405 }
406
407 /* Check the syntax of the format string with what is allowed. */
408 std::cmatch m;
409 if (!std::regex_search(format.begin(), format.end(), m, allowed_pattern->pattern)) {
410 if (!r_error) {
412 format, format_outer, *allowed_pattern);
413 }
414 return std::nullopt;
415 }
416
418
419 /* Identifiers that are used to specify the width or precision will be replaced with {}. */
420 Vector<std::string> formats_to_replace;
421
422 /* Check if a dynamic width is specified. */
423 const std::string width_outer = m.str(allowed_pattern->width_group);
424 if (!width_outer.empty()) {
425 const StringRef width_inner = StringRef(width_outer).drop_prefix(1).drop_suffix(1);
426 result.widths = inputs_lookup.find_next_input(width_inner, r_error);
427 if (!result.widths) {
428 return std::nullopt;
429 }
430 if (!result.widths->type().is<int>()) {
431 if (!r_error) {
432 r_error = fmt::format(
433 fmt::runtime(TIP_("Only integer inputs can be used as dynamic width: \"{}\"")),
434 format_outer);
435 }
436 return std::nullopt;
437 }
438 formats_to_replace.append(width_outer);
439 }
440
441 /* Check if a dynamic precision is specified. */
442 if (allowed_pattern->precision_group.has_value()) {
443 const std::string precision_outer = m.str(*allowed_pattern->precision_group);
444 if (!precision_outer.empty()) {
445 const StringRef precision_inner = StringRef(precision_outer).drop_prefix(1).drop_suffix(1);
446 result.precisions = inputs_lookup.find_next_input(precision_inner, r_error);
447 if (!result.precisions) {
448 return std::nullopt;
449 }
450 if (!result.precisions->type().is<int>()) {
451 if (!r_error) {
452 r_error = fmt::format(
453 fmt::runtime(TIP_("Only integer inputs can be used as dynamic precision: \"{}\"")),
454 format_outer);
455 }
456 return std::nullopt;
457 }
458 formats_to_replace.append(precision_outer);
459 }
460 }
461
462 result.fmt_format_str = "{:";
463 result.fmt_format_str.append(format.begin(), format.end());
464 result.fmt_format_str += '}';
465
466 /* Replace identifiers with {}, because the source identifiers are not passed to fmt. */
467 for (const std::string &old : formats_to_replace) {
468 const int64_t old_start = result.fmt_format_str.find(old);
469 if (old_start != std::string::npos) {
470 result.fmt_format_str.replace(old_start, old.size(), "{}");
471 }
472 }
473
474 return result;
475}
476
477static void format_with_fmt(const fmt::runtime_format_string<> format,
478 const GVArray &input,
479 const GVArray *widths,
480 const GVArray *precisions,
481 const IndexMask &mask,
482 MutableSpan<std::string> r_formatted_strings)
483{
484 const auto append_single_formatted_string = [&](const auto &varray) {
485 mask.foreach_index([&](const int64_t i) {
486 std::string &output = r_formatted_strings[i];
487 auto output_inserter = std::back_inserter(output);
488 try {
489 if (precisions) {
490 const int precision = std::max(0, precisions->get<int>(i));
491 if (widths) {
492 const int width = std::max(0, widths->get<int>(i));
493 fmt::format_to(output_inserter, format, varray[i], width, precision);
494 }
495 else {
496 fmt::format_to(output_inserter, format, varray[i], precision);
497 }
498 }
499 else {
500 if (widths) {
501 const int width = std::max(0, widths->get<int>(i));
502 fmt::format_to(output_inserter, format, varray[i], width);
503 }
504 else {
505 fmt::format_to(output_inserter, format, varray[i]);
506 }
507 }
508 }
509 catch (const fmt::format_error & /*error*/) {
510 /* Invalid patterns should have been caught before already. */
512 }
513 });
514 };
515
516 const CPPType &type = input.type();
517 if (type.is<float>()) {
518 append_single_formatted_string(input.typed<float>());
519 }
520 else if (type.is<int>()) {
521 append_single_formatted_string(input.typed<int>());
522 }
523 else if (type.is<std::string>()) {
524 append_single_formatted_string(input.typed<std::string>());
525 }
526 else {
527 /* The input type should have been checked earlier already. */
529 }
530}
531
532static void format_with_python_compatible_syntax(const StringRef format_pattern,
533 const StringRef format_outer,
534 const GVArray &input,
535 const IndexMask &mask,
536 FormatInputsLookup &inputs_lookup,
537 MutableSpan<std::string> r_formatted_strings,
538 std::optional<std::string> &r_error)
539{
540 const CPPType &type = input.type();
541 /* Extract information like width and precision inputs. */
542 std::optional<ProcessedPythonCompatibleFormat> processed_format =
544 format_pattern, format_outer, type, inputs_lookup, r_error);
545 if (!processed_format.has_value()) {
546 BLI_assert(r_error);
547 return;
548 }
549 format_with_fmt(fmt::runtime(processed_format->fmt_format_str),
550 input,
551 processed_format->widths,
552 processed_format->precisions,
553 mask,
554 r_formatted_strings);
555}
556
557static void format_with_hash_syntax(const StringRef format_pattern,
558 const GVArray &input,
559 const IndexMask &mask,
560 MutableSpan<std::string> r_formatted_strings,
561 std::optional<std::string> &r_error)
562{
563 const CPPType &type = input.type();
564 if (type.is<float>()) {
565 mask.foreach_index([&](const int64_t i) {
566 std::string &output = r_formatted_strings[i];
567 const float value = input.get<float>(i);
568 if (const std::optional<std::string> value_str = BKE_path_template_format_float(
569 format_pattern, value))
570 {
571 output.append(*value_str);
572 }
573 else if (!r_error) {
574 r_error = fmt::format(fmt::runtime(TIP_("Invalid format specifier: \"{}\"")),
575 format_pattern);
576 }
577 });
578 }
579 else if (type.is<int>()) {
580 mask.foreach_index([&](const int64_t i) {
581 std::string &output = r_formatted_strings[i];
582 const int64_t value = input.get<int>(i);
583 if (const std::optional<std::string> value_str = BKE_path_template_format_int(format_pattern,
584 value))
585 {
586 output.append(*value_str);
587 }
588 else if (!r_error) {
589 r_error = fmt::format(fmt::runtime(TIP_("Invalid format specifier: \"{}\"")),
590 format_pattern);
591 }
592 });
593 }
594 else if (type.is<std::string>()) {
595 if (!r_error) {
596 r_error = fmt::format(fmt::runtime(TIP_("Invalid format specifier for string: \"{}\"")),
597 format_pattern);
598 }
599 }
600 else if (!r_error) {
601 r_error = fmt::format(fmt::runtime(TIP_("Type \"{}\" cannot be formatted")), type.name());
602 }
603}
604
606 const IndexMask &mask,
607 MutableSpan<std::string> r_formatted_strings,
608 std::optional<std::string> &r_error)
609{
610 const CPPType &type = input.type();
611 if (type.is<float>()) {
612 mask.foreach_index([&](const int64_t i) {
613 const float value = input.get<float>(i);
614 std::string &output = r_formatted_strings[i];
615 std::string value_str = fmt::format("{}", value);
616 /* Add ".0" if there are no decimals yet to match Python. */
617 if (StringRef(value_str).find_first_not_of("-0123456789") == StringRef::not_found) {
618 value_str.append(".0");
619 }
620 output += value_str;
621 });
622 }
623 else if (type.is<int>()) {
624 mask.foreach_index([&](const int64_t i) {
625 const int64_t value = input.get<int>(i);
626 std::string &output = r_formatted_strings[i];
627 output += fmt::format("{}", value);
628 });
629 }
630 else if (type.is<std::string>()) {
631 mask.foreach_index([&](const int64_t i) {
632 const std::string value = input.get<std::string>(i);
633 std::string &output = r_formatted_strings[i];
634 output += value;
635 });
636 }
637 else if (!r_error) {
638 r_error = fmt::format(fmt::runtime(TIP_("Type \"{}\" cannot be formatted")), type.name());
639 }
640}
641
642static bool format_strings(const StringRef format,
643 const Span<GVArray> inputs,
644 const VectorSet<std::string> &input_names,
645 const IndexMask &mask,
646 MutableSpan<std::string> r_formatted_strings,
647 std::optional<std::string> &r_error)
648{
649 CPPType::get<std::string>().value_initialize_indices(r_formatted_strings.data(), mask);
650
651 FormatInputsLookup inputs_lookup{inputs, input_names};
652
653 int64_t current_index = 0;
654 while (current_index < format.size()) {
655 /* Find the string until the next format starts or the string ends. */
656 std::string copy_str;
657 const int64_t next_format_start_or_end = find_next_format_start_or_end(
658 format, current_index, copy_str, r_error);
659 if (next_format_start_or_end < 0) {
660 BLI_assert(r_error);
661 return false;
662 }
663
664 /* Append the non-formatted string to the outputs. */
665 if (!copy_str.empty()) {
666 mask.foreach_index([&](const int64_t i) {
667 std::string &output = r_formatted_strings[i];
668 output.append(copy_str);
669 });
670 }
671
672 /* The string has ended, so return successfully. */
673 if (next_format_start_or_end == format.size()) {
674 break;
675 }
676 current_index = next_format_start_or_end;
677
678 /* Find the format specifier starting at the current index. */
679 const std::optional<StringRef> format_outer = find_format_specifier(
680 format.substr(current_index));
681 if (!format_outer.has_value()) {
682 if (!r_error) {
683 r_error = fmt::format(fmt::runtime(TIP_("Format specifier is not closed: \"{}\"")),
684 format.substr(current_index));
685 }
686 return false;
687 }
688 const StringRef format_inner = format_outer->substr(1, format_outer->size() - 2);
689
690 /* Extract the identifier and the pattern which are split by a colon. */
691 StringRef identifier;
692 StringRef format_pattern;
693 const int64_t colon_index = format_inner.find(':');
694 if (colon_index == StringRef::not_found) {
695 identifier = format_inner;
696 }
697 else {
698 identifier = format_inner.substr(0, colon_index);
699 format_pattern = format_inner.substr(colon_index + 1);
700 }
701
702 /* Find the typed input values and get the corresponding allowed pattern. */
703 const GVArray *input = inputs_lookup.find_next_input(identifier, r_error);
704 if (!input) {
705 return false;
706 }
707
708 if (format_pattern.is_empty()) {
709 format_without_format_specifier(*input, mask, r_formatted_strings, r_error);
710 }
711 else if (format_pattern.find('#') == StringRef::not_found) {
713 *format_outer,
714 *input,
715 mask,
716 inputs_lookup,
717 r_formatted_strings,
718 r_error);
719 }
720 else {
721 format_with_hash_syntax(format_pattern, *input, mask, r_formatted_strings, r_error);
722 }
723 if (r_error) {
724 return false;
725 }
726
727 current_index += format_outer->size();
728 }
729 return true;
730}
731
732class FormatStringMultiFunction : public mf::MultiFunction {
733 private:
735 std::shared_ptr<const bNodeTree> shared_tree_;
736 const bNode &node_;
737 VectorSet<std::string> input_names_;
738 mf::Signature signature_;
739
740 public:
741 FormatStringMultiFunction(const bNode &node, std::shared_ptr<const bNodeTree> shared_tree)
742 : shared_tree_(std::move(shared_tree)), node_(node)
743 {
744 const NodeFunctionFormatString &storage = node_storage(node);
745
746 mf::SignatureBuilder builder{"Format String", signature_};
747 builder.single_input<std::string>("Format");
748 for (const int i : IndexRange(storage.items_num)) {
749 const NodeFunctionFormatStringItem &item = storage.items[i];
750 const eNodeSocketDatatype socket_type = item.socket_type;
751 const CPPType &type = *bke::socket_type_to_geo_nodes_base_cpp_type(socket_type);
752 builder.single_input(item.name, type);
753 input_names_.add_new(StringRef(item.name));
754 }
755
756 builder.single_output<std::string>("String");
757
758 this->set_signature(&signature_);
759 }
760
761 void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
762 {
763 const NodeFunctionFormatString &storage = node_storage(node_);
764
765 const VArray<std::string> formats = params.readonly_single_input<std::string>(0, "Format");
766 MutableSpan<std::string> outputs = params.uninitialized_single_output<std::string>(
767 storage.items_num + 1, "String");
768
769 Array<GVArray> inputs(storage.items_num);
770 for (const int i : IndexRange(storage.items_num)) {
771 inputs[i] = params.readonly_single_input(i + 1);
772 }
773
774 std::optional<std::string> error_message;
775
776 if (const std::optional<std::string> single_format = formats.get_if_single()) {
777 if (!format_strings(*single_format, inputs, input_names_, mask, outputs, error_message)) {
778 mask.foreach_index([&](const int64_t i) { outputs[i].clear(); });
779 }
780 }
781 else {
782 mask.foreach_index(
783 [&](const int64_t i) {
784 const std::string format = formats[i];
785 if (!format_strings(format,
786 inputs,
787 input_names_,
789 outputs,
790 error_message))
791 {
792 outputs[i].clear();
793 }
794 },
796 }
797
798 if (error_message.has_value()) {
799 report_from_multi_function(context, NodeWarningType::Error, std::move(*error_message));
800 }
801 }
802};
803
809
810static void node_register()
811{
812 static bke::bNodeType ntype;
813
814 common_node_type_base(&ntype, "FunctionNodeFormatString"_ustr);
815 ntype.ui_name = "Format String";
816 ntype.ui_description =
817 "Insert values into a string using a Python and path template compatible formatting syntax";
819 bke::node_type_storage(ntype, "NodeFunctionFormatString", node_free_storage, node_copy_storage);
820 ntype.declare = node_declare;
822 ntype.initfunc = node_init;
829}
831
832} // namespace nodes::node_fn_format_string_cc
833
834namespace nodes {
835
836StructRNA **FormatStringItemsAccessor::item_srna = &RNA_NodeFunctionFormatStringItem;
837
839{
840 writer->write_string(item.name);
841}
842
847
848} // namespace nodes
849} // namespace blender
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
Functions and classes for evaluating template expressions in filepaths.
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
#define output
static const CPPType & get()
StringRefNull name() const
bool is() const
void get(int64_t index, void *r_value) const
constexpr IndexRange()=default
static constexpr IndexRange from_single(const int64_t index)
constexpr T * data() const
Definition BLI_span.hh:545
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
constexpr StringRef()
UString()=default
std::optional< T > get_if_single() const
void append(const T &value)
constexpr const char * end() const
constexpr const char * begin() const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
void set_signature(const Signature *signature)
const std::shared_ptr< const bNodeTree > & shared_tree()
FormatInputsLookup(const Span< GVArray > inputs, const VectorSet< std::string > &input_names)
std::optional< int64_t > find_next_input_index(const StringRef identifier, std::optional< std::string > &r_error)
const GVArray * find_next_input(const StringRef identifier, std::optional< std::string > &r_error)
void call(const IndexMask &mask, mf::Params params, mf::Context context) const override
FormatStringMultiFunction(const bNode &node, std::shared_ptr< const bNodeTree > shared_tree)
#define storage(slot, qualifiers)
float length(VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5969
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
constexpr ParallelGrainSize grain_size(int grain_size)
static FormatPatternInfo get_pattern_by_type_impl(const CPPType &type)
static void format_without_format_specifier(const GVArray &input, const IndexMask &mask, MutableSpan< std::string > r_formatted_strings, std::optional< std::string > &r_error)
static bool format_strings(const StringRef format, const Span< GVArray > inputs, const VectorSet< std::string > &input_names, const IndexMask &mask, MutableSpan< std::string > r_formatted_strings, std::optional< std::string > &r_error)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static void node_build_multi_function(NodeMultiFunctionBuilder &builder)
static std::optional< ProcessedPythonCompatibleFormat > preprocess_python_compatible_syntax(const StringRef format, const StringRef format_outer, const CPPType &type, FormatInputsLookup &inputs_lookup, std::optional< std::string > &r_error)
static void format_with_fmt(const fmt::runtime_format_string<> format, const GVArray &input, const GVArray *widths, const GVArray *precisions, const IndexMask &mask, MutableSpan< std::string > r_formatted_strings)
static int64_t find_next_format_start_or_end(const StringRef format, const int64_t start, std::string &r_out, std::optional< std::string > &r_error)
static const FormatPatternInfo * get_pattern_by_type(const CPPType &type)
static void node_declare(NodeDeclarationBuilder &b)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
static void format_with_python_compatible_syntax(const StringRef format_pattern, const StringRef format_outer, const GVArray &input, const IndexMask &mask, FormatInputsLookup &inputs_lookup, MutableSpan< std::string > r_formatted_strings, std::optional< std::string > &r_error)
static std::string create_invalid_python_compatible_format_error(const StringRef format, const StringRef format_outer, const FormatPatternInfo &pattern)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static std::optional< StringRef > find_format_specifier(const StringRef format)
static void format_with_hash_syntax(const StringRef format_pattern, const GVArray &input, const IndexMask &mask, MutableSpan< std::string > r_formatted_strings, std::optional< std::string > &r_error)
static void node_init(bNodeTree *, bNode *node)
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)
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 report_from_multi_function(const mf::Context &context, NodeWarningType type, std::string message)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
BMesh const char void * data
std::optional< std::string > BKE_path_template_format_float(StringRef format_specifier, double value)
std::optional< std::string > BKE_path_template_format_int(StringRef format_specifier, int64_t value)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void write_string(const char *data)
T * data_as() const
Definition RNA_types.hh:139
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
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
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
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 void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const NodeFunctionFormatStringItem &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)