37 b.use_custom_socket_order();
38 b.allow_any_socket_order();
43 "Format string using a Python and path template compatible syntax. For example, "
45 "{}\" would replace the {} with the first input value.");
46 b.add_output<
decl::String>(
"String"_ustr).align_with_previous();
50 if (!ntree || !
node) {
60 b.add_input(socket_type, name,
UString(identifier))
65 .custom_draw(socket_items::ui::draw_extend_socket_fn<FormatStringItemsAccessor>());
77 auto *dst_storage = MEM_new<NodeFunctionFormatString>(__func__,
dna::shallow_copy(src_storage));
78 dst_node->
storage = dst_storage;
105 socket_items::ui::draw_items_list_with_operators<FormatStringItemsAccessor>(
106 C, panel, tree,
node);
107 socket_items::ui::draw_active_item_props<FormatStringItemsAccessor>(
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);
130 for (
const char &c : format.substr(1)) {
137 if (braces_depth == 0) {
138 const int length = &c - format.data() + 1;
139 return format.substr(0,
length);
149 std::optional<std::string> &r_error)
152 while (i < format.size()) {
153 const char c = format[i];
157 if (i + 1 < format.size()) {
158 const char next_c = format[i + 1];
170 if (i + 1 < format.size()) {
171 const char next_c = format[i + 1];
179 r_error = fmt::format(
"{}: \"{}\"",
TIP_(
"Unescaped '}' in format string"), format);
190 return format.size();
209 pattern +=
"([^{}]?[<>^])?";
211 if (type.
is<
float>() || type.
is<
int>()) {
213 pattern +=
"[+\\- ]?";
220 const std::string width_integer_or_identifier =
"([1-9]\\d*|(\\{.*\\}))";
221 pattern += width_integer_or_identifier;
224 const int width_group = groups_num;
226 std::optional<int> precision_group;
227 if (type.
is<
float>() || type.
is<std::string>()) {
229 const std::string precision_integer_or_identifier =
"(\\d+|(\\{.*\\}))";
231 pattern += precision_integer_or_identifier;
234 precision_group = groups_num;
238 if (type.
is<std::string>()) {
239 pattern +=
"[s\\?]?";
241 else if (type.
is<
int>()) {
242 pattern +=
"[bBcdoxX]?";
244 else if (type.
is<
float>()) {
245 pattern +=
"[aAeEfFgG]?";
249 return {pattern, std::regex{pattern}, width_group, precision_group};
254 if (type.
is<
float>()) {
258 if (type.
is<
int>()) {
262 if (type.
is<std::string>()) {
278 bool non_auto_index_used_ =
false;
282 : inputs_(inputs), input_names_(input_names)
289 if (!input_index.has_value()) {
292 return &inputs_[*input_index];
296 std::optional<std::string> &r_error)
299 if (non_auto_index_used_) {
304 "Empty identifier cannot be used when explicit identifier was used before. For "
305 "example, \"{} {x}\" is ok but \"{x} {}\" is not.");
309 if (next_auto_index_ == inputs_.size()) {
312 r_error =
TIP_(
"Format uses more inputs than provided.");
316 return next_auto_index_++;
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()) {
324 r_error = fmt::format(fmt::runtime(
TIP_(
"Invalid identifier: \"{}\"")), identifier);
328 if (res.ptr < identifier.
end()) {
331 r_error = fmt::format(
332 fmt::runtime(
TIP_(
"An input name cannot start with a digit: \"{}\"")), identifier);
336 if (
index >= inputs_.size()) {
338 if (inputs_.is_empty()) {
339 r_error = fmt::format(fmt::runtime(
TIP_(
"There are no inputs.")), identifier);
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 {{:{}}}?")),
354 const int index = input_names_.index_of_try_as(identifier);
357 r_error = fmt::format(fmt::runtime(
TIP_(
"Input does not exist: \"{}\"")), identifier);
379 for (
const char c : format) {
380 if (pattern.
pattern_str.find(c) == std::string::npos && std::isprint(c) && !std::isdigit(c)) {
382 fmt::runtime(
TIP_(
"Format contains unsupported \"{}\" character: \"{}\"")),
387 return fmt::format(fmt::runtime(
TIP_(
"Invalid format: \"{}\"")), format_outer);
395 std::optional<std::string> &r_error)
398 if (!allowed_pattern) {
402 r_error = fmt::format(fmt::runtime(
TIP_(
"Type \"{}\" cannot be formatted")), type.
name());
409 if (!std::regex_search(format.begin(), format.end(), m, allowed_pattern->
pattern)) {
412 format, format_outer, *allowed_pattern);
423 const std::string width_outer = m.str(allowed_pattern->
width_group);
424 if (!width_outer.empty()) {
430 if (!
result.widths->type().is<
int>()) {
432 r_error = fmt::format(
433 fmt::runtime(
TIP_(
"Only integer inputs can be used as dynamic width: \"{}\"")),
438 formats_to_replace.
append(width_outer);
443 const std::string precision_outer = m.str(*allowed_pattern->
precision_group);
444 if (!precision_outer.empty()) {
450 if (!
result.precisions->type().is<
int>()) {
452 r_error = fmt::format(
453 fmt::runtime(
TIP_(
"Only integer inputs can be used as dynamic precision: \"{}\"")),
458 formats_to_replace.
append(precision_outer);
462 result.fmt_format_str =
"{:";
463 result.fmt_format_str.append(format.begin(), format.end());
464 result.fmt_format_str +=
'}';
467 for (
const std::string &old : formats_to_replace) {
469 if (old_start != std::string::npos) {
470 result.fmt_format_str.replace(old_start, old.size(),
"{}");
484 const auto append_single_formatted_string = [&](
const auto &varray) {
486 std::string &
output = r_formatted_strings[i];
487 auto output_inserter = std::back_inserter(
output);
490 const int precision = std::max(0, precisions->
get<
int>(i));
492 const int width = std::max(0, widths->
get<
int>(i));
493 fmt::format_to(output_inserter, format, varray[i], width, precision);
496 fmt::format_to(output_inserter, format, varray[i], precision);
501 const int width = std::max(0, widths->
get<
int>(i));
502 fmt::format_to(output_inserter, format, varray[i], width);
505 fmt::format_to(output_inserter, format, varray[i]);
509 catch (
const fmt::format_error & ) {
517 if (type.
is<
float>()) {
518 append_single_formatted_string(input.
typed<
float>());
520 else if (type.
is<
int>()) {
521 append_single_formatted_string(input.
typed<
int>());
523 else if (type.
is<std::string>()) {
524 append_single_formatted_string(input.
typed<std::string>());
538 std::optional<std::string> &r_error)
542 std::optional<ProcessedPythonCompatibleFormat> processed_format =
544 format_pattern, format_outer, type, inputs_lookup, r_error);
545 if (!processed_format.has_value()) {
551 processed_format->widths,
552 processed_format->precisions,
554 r_formatted_strings);
561 std::optional<std::string> &r_error)
564 if (type.
is<
float>()) {
566 std::string &
output = r_formatted_strings[i];
567 const float value = input.
get<
float>(i);
569 format_pattern, value))
571 output.append(*value_str);
574 r_error = fmt::format(fmt::runtime(
TIP_(
"Invalid format specifier: \"{}\"")),
579 else if (type.
is<
int>()) {
581 std::string &
output = r_formatted_strings[i];
586 output.append(*value_str);
589 r_error = fmt::format(fmt::runtime(
TIP_(
"Invalid format specifier: \"{}\"")),
594 else if (type.
is<std::string>()) {
596 r_error = fmt::format(fmt::runtime(
TIP_(
"Invalid format specifier for string: \"{}\"")),
601 r_error = fmt::format(fmt::runtime(
TIP_(
"Type \"{}\" cannot be formatted")), type.
name());
608 std::optional<std::string> &r_error)
611 if (type.
is<
float>()) {
613 const float value = input.
get<
float>(i);
614 std::string &
output = r_formatted_strings[i];
615 std::string value_str = fmt::format(
"{}", value);
618 value_str.append(
".0");
623 else if (type.
is<
int>()) {
626 std::string &
output = r_formatted_strings[i];
627 output += fmt::format(
"{}", value);
630 else if (type.
is<std::string>()) {
632 const std::string value = input.
get<std::string>(i);
633 std::string &
output = r_formatted_strings[i];
638 r_error = fmt::format(fmt::runtime(
TIP_(
"Type \"{}\" cannot be formatted")), type.
name());
647 std::optional<std::string> &r_error)
654 while (current_index < format.size()) {
656 std::string copy_str;
658 format, current_index, copy_str, r_error);
659 if (next_format_start_or_end < 0) {
665 if (!copy_str.empty()) {
667 std::string &
output = r_formatted_strings[i];
673 if (next_format_start_or_end == format.size()) {
676 current_index = next_format_start_or_end;
680 format.substr(current_index));
681 if (!format_outer.has_value()) {
683 r_error = fmt::format(fmt::runtime(
TIP_(
"Format specifier is not closed: \"{}\"")),
684 format.substr(current_index));
688 const StringRef format_inner = format_outer->
substr(1, format_outer->size() - 2);
693 const int64_t colon_index = format_inner.
find(
':');
695 identifier = format_inner;
698 identifier = format_inner.
substr(0, colon_index);
699 format_pattern = format_inner.
substr(colon_index + 1);
727 current_index += format_outer->
size();
735 std::shared_ptr<const bNodeTree> shared_tree_;
738 mf::Signature signature_;
742 : shared_tree_(std::move(shared_tree)), node_(
node)
746 mf::SignatureBuilder builder{
"Format String", signature_};
747 builder.single_input<std::string>(
"Format");
752 builder.single_input(item.
name, type);
756 builder.single_output<std::string>(
"String");
765 const VArray<std::string> formats = params.readonly_single_input<std::string>(0,
"Format");
767 storage.items_num + 1,
"String");
771 inputs[i] = params.readonly_single_input(i + 1);
774 std::optional<std::string> error_message;
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(); });
784 const std::string format = formats[i];
798 if (error_message.has_value()) {
815 ntype.
ui_name =
"Format String";
817 "Insert values into a string using a Python and path template compatible formatting syntax";
#define NODE_CLASS_CONVERTER
#define NODE_STORAGE_FUNCS(StorageT)
Functions and classes for evaluating template expressions in filepaths.
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
static const CPPType & get()
StringRefNull name() const
const CPPType & type() const
void get(int64_t index, void *r_value) const
VArray< T > typed() const
constexpr IndexRange()=default
static constexpr IndexRange from_single(const int64_t index)
constexpr T * data() const
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
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)
void construct_and_set_matching_fn(Args &&...args)
const std::shared_ptr< const bNodeTree > & shared_tree()
#define storage(slot, qualifiers)
float length(VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
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))
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
constexpr ParallelGrainSize grain_size(int grain_size)
void make_common_operators()
void blend_write(BlendWriter *writer, const bNode &node)
void destruct_array(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)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
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)
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void write_string(const char *data)
NodeBlendWriteFunction blend_write_storage_content
void(*) register_operators()
std::string ui_description
NodeBlendDataReadFunction blend_data_read_storage_content
NodeMultiFunctionBuildFunction build_multi_function
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
void(*) initfunc(bNodeTree *ntree, bNode *node)
NodeDeclareFunction declare
bool(*) insert_link(NodeInsertLinkParams ¶ms)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)