Blender V5.0
node_geo_import_text.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_fileops.h"
9#include "BLI_string_utf8.h"
10
11#include "node_geometry_util.hh"
12
13#include "fmt/core.h"
14
16
18{
19 b.add_input<decl::String>("Path")
20 .subtype(PROP_FILEPATH)
21 .path_filter("*.txt")
22 .optional_label()
23 .description("Path to a text file");
24
25 b.add_output<decl::String>("String");
26}
27
29 public:
30 std::string text;
32
33 void count_memory(MemoryCounter &counter) const override
34 {
35 counter.add(this->text.size());
36 }
37};
38
40{
41 const std::optional<std::string> path = params.ensure_absolute_path(
42 params.extract_input<std::string>("Path"));
43 if (!path) {
44 params.set_default_remaining_outputs();
45 return;
46 }
47
48 std::shared_ptr<const LoadTextCache> cached_value = memory_cache::get_loaded<LoadTextCache>(
49 GenericStringKey{"import_text_node"}, {StringRefNull(*path)}, [&]() {
50 auto cached_value = std::make_unique<LoadTextCache>();
51
52 size_t buffer_len;
53 void *buffer = BLI_file_read_text_as_mem(path->c_str(), 0, &buffer_len);
54 if (!buffer) {
55 const std::string message = fmt::format(fmt::runtime(TIP_("Cannot open file: {}")),
56 *path);
57 cached_value->warnings.append({NodeWarningType::Error, message});
58 return cached_value;
59 }
60 BLI_SCOPED_DEFER([&]() { MEM_freeN(buffer); });
61 if (BLI_str_utf8_invalid_byte(static_cast<const char *>(buffer), buffer_len) != -1) {
62 cached_value->warnings.append(
63 {NodeWarningType::Error, TIP_("File contains invalid UTF-8 characters")});
64 return cached_value;
65 }
66 cached_value->text = std::string(static_cast<char *>(buffer), buffer_len);
67 return cached_value;
68 });
69
70 for (const geo_eval_log::NodeWarning &warning : cached_value->warnings) {
71 params.error_message_add(warning.type, warning.message);
72 }
73
74 params.set_output("String", cached_value->text);
75}
76
77static void node_register()
78{
79 static blender::bke::bNodeType ntype;
80
81 geo_node_type_base(&ntype, "GeometryNodeImportText");
82 ntype.ui_name = "Import Text";
83 ntype.ui_description = "Import a string from a text file";
86 ntype.declare = node_declare;
87
89}
91
92} // namespace blender::nodes::node_geo_import_text
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
File and directory operations.
void * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:511
#define BLI_SCOPED_DEFER(function_to_defer)
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FILEPATH
Definition RNA_types.hh:236
std::optional< std::string > path_filter
void count_memory(MemoryCounter &counter) const override
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void MEM_freeN(void *vmemh)
Definition mallocn.cc:113
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
std::shared_ptr< const T > get_loaded(const GenericKey &loader_key, Span< StringRefNull > file_paths, FunctionRef< std::unique_ptr< T >()> load_fn)
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
NodeDeclareFunction declare
Definition BKE_node.hh:362