Blender V5.3
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 "DNA_ID.h"
6
7#include "BKE_main.hh"
8
9#include "BLI_fileops.hh"
12#include "BLI_memory_counter.hh"
13#include "BLI_path_utils.hh"
14#include "BLI_string.hh"
15#include "BLI_string_utf8.hh"
16
17#include "COM_node_operation.hh"
18
19#include "node_geometry_util.hh"
20
21#include "fmt/core.h"
22
24
26{
27 b.add_input<decl::String>("Path"_ustr)
28 .subtype(PROP_FILEPATH)
29 .path_filter("*.txt")
30 .optional_label()
31 .description("Path to a text file");
32
33 b.add_output<decl::String>("String"_ustr);
34}
35
37 public:
38 std::string text;
40
41 void count_memory(MemoryCounter &counter) const override
42 {
43 counter.add(this->text.size());
44 }
45};
46
47static std::shared_ptr<const LoadTextCache> get_cached_text(const StringRefNull path)
48{
49 auto load_fn = [path]() {
50 auto cached_value = std::make_unique<LoadTextCache>();
51
52 size_t buffer_len;
53 char *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: {}")), path);
56 cached_value->warnings.append({NodeWarningType::Error, message});
57 return cached_value;
58 }
59 BLI_SCOPED_DEFER([&]() { MEM_delete(buffer); });
60 if (BLI_str_utf8_invalid_byte(buffer, buffer_len) != -1) {
61 cached_value->warnings.append(
62 {NodeWarningType::Error, TIP_("File contains invalid UTF-8 characters")});
63 return cached_value;
64 }
65 cached_value->text = std::string(buffer, buffer_len);
66 return cached_value;
67 };
68
70 GenericStringKey{"import_text_node"}, {path}, load_fn);
71}
72
74{
75 const std::optional<std::string> path = params.ensure_absolute_path(
76 params.extract_input<std::string>("Path"_ustr));
77 if (!path) {
79 return;
80 }
81
82 std::shared_ptr<const LoadTextCache> cached_value = get_cached_text(*path);
83
84 for (const NodeWarning &warning : cached_value->warnings) {
85 params.error_message_add(warning.type, warning.message);
86 }
87
88 params.set_output("String"_ustr, cached_value->text);
89}
90
92 public:
94
95 void execute() override
96 {
97 compositor::Result &result = this->get_result("String");
98 if (!result.should_compute()) {
99 return;
100 }
101
102 const std::optional<std::string> path = this->ensure_absolute_path(
103 this->get_input("Path").get_single_value_default<std::string>());
104 if (!path) {
105 result.allocate_single_value();
106 result.set_single_value(std::string(""));
107 return;
108 }
109
110 std::shared_ptr<const LoadTextCache> cached_value = get_cached_text(*path);
111
112 for (const NodeWarning &warning : cached_value->warnings) {
113 this->add_warning(warning.type, warning.message);
114 }
115
116 result.allocate_single_value();
117 result.set_single_value(cached_value->text);
118 }
119
120 private:
121 std::optional<std::string> ensure_absolute_path(const StringRefNull path) const
122 {
123 if (path.is_empty()) {
124 return std::nullopt;
125 }
126 if (!BLI_path_is_rel(path.c_str())) {
127 return std::string(path);
128 }
129 const Main &bmain = this->context().get_main();
130 const bNodeTree &tree = this->node().owner_tree();
131 const char *base_path = ID_BLEND_PATH(&bmain, &tree.id);
132 if (!base_path || base_path[0] == '\0') {
133 return std::nullopt;
134 }
135 char absolute_path[FILE_MAX];
136 STRNCPY(absolute_path, path.c_str());
137 BLI_path_abs(absolute_path, base_path);
138 return absolute_path;
139 }
140};
141
143 const bNode &node)
144{
145 return new ImportTextOperation(context, node);
146}
147
148static void node_register()
149{
150 static bke::bNodeType ntype;
151
152 geo_cmp_node_type_base(&ntype, "GeometryNodeImportText"_ustr);
153 ntype.ui_name = "Import Text";
154 ntype.ui_description = "Import a string from a text file";
155 ntype.nclass = NODE_CLASS_INPUT;
157 ntype.declare = node_declare;
159
161}
163
164} // namespace blender::nodes::node_geo_import_text
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define TIP_(msgid)
ID and Library types, which are fundamental for SDNA.
#define ID_BLEND_PATH(_bmain, _id)
Definition DNA_ID.h:688
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr const char * c_str() const
constexpr bool is_empty() const
virtual const Main & get_main() const =0
NodeOperation(Context &context, const bNode &node)
void add_warning(nodes::NodeWarningType type, std::string message)
Result & get_result(StringRef identifier)
Definition operation.cc:40
Context & context() const
Definition operation.cc:57
Result & get_input(StringRef identifier) const
Definition operation.cc:35
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
std::optional< std::string > ensure_absolute_path(StringRefNull path) const
void set_output(const UString identifier, T &&value)
std::optional< std::string > path_filter
void count_memory(MemoryCounter &counter) const override
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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 compositor::NodeOperation * get_compositor_operation(compositor::Context &context, const bNode &node)
static void node_declare(NodeDeclarationBuilder &b)
static std::shared_ptr< const LoadTextCache > get_cached_text(const StringRefNull path)
char * STRNCPY(char(&dst)[N], const char *src)
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
bool BLI_path_abs(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
bool BLI_path_is_rel(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
@ PROP_FILEPATH
Definition RNA_types.hh:284
void geo_cmp_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGetCompositorOperationFunction get_compositor_operation
Definition BKE_node.hh:379
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393