Blender V5.0
node_geo_import_csv.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <fmt/format.h>
6
8#include "BLI_listbase.h"
10#include "BLI_string.h"
11
12#include "BKE_report.hh"
13
14#include "IO_csv.hh"
15
16#include "node_geometry_util.hh"
17
19
21{
22 b.add_input<decl::String>("Path")
23 .subtype(PROP_FILEPATH)
24 .path_filter("*.csv")
25 .optional_label()
26 .description("Path to a CSV file");
27 b.add_input<decl::String>("Delimiter").default_value(",");
28
29 b.add_output<decl::Geometry>("Point Cloud");
30}
31
33 public:
36
37 void count_memory(MemoryCounter &counter) const override
38 {
39 this->geometry.count_memory(counter);
40 }
41};
42
44{
45 const std::optional<std::string> path = params.ensure_absolute_path(
46 params.extract_input<std::string>("Path"));
47 if (!path) {
48 params.set_default_remaining_outputs();
49 return;
50 }
51 const std::string delimiter = params.extract_input<std::string>("Delimiter");
52 if (delimiter.size() != 1) {
53 params.error_message_add(NodeWarningType::Error, TIP_("Delimiter must be a single character"));
54 params.set_default_remaining_outputs();
55 return;
56 }
57 if (ELEM(delimiter[0], '\n', '\r', '"', '\\')) {
58 params.error_message_add(NodeWarningType::Error,
59 TIP_("Delimiter must not be \\n, \\r, \" or \\"));
60 params.set_default_remaining_outputs();
61 return;
62 }
63
64 /* Encode delimiter in key because it affects the result. */
65 const std::string loader_key = fmt::format("import_csv_node_{}", delimiter[0]);
66 std::shared_ptr<const LoadCsvCache> cached_value = memory_cache::get_loaded<LoadCsvCache>(
67 GenericStringKey{loader_key}, {StringRefNull(*path)}, [&]() {
69 import_params.delimiter = delimiter[0];
70 STRNCPY(import_params.filepath, path->c_str());
71
72 ReportList reports;
73 BKE_reports_init(&reports, RPT_STORE);
74 BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); });
75 import_params.reports = &reports;
76
77 PointCloud *pointcloud = blender::io::csv::import_csv_as_pointcloud(import_params);
78
79 auto cached_value = std::make_unique<LoadCsvCache>();
80 cached_value->geometry = GeometrySet::from_pointcloud(pointcloud);
81
82 LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
83 cached_value->warnings.append_as(*report);
84 }
85 return cached_value;
86 });
87
88 for (const geo_eval_log::NodeWarning &warning : cached_value->warnings) {
89 params.error_message_add(warning.type, warning.message);
90 }
91
92 params.set_output("Point Cloud", cached_value->geometry);
93}
94
95static void node_register()
96{
97 static blender::bke::bNodeType ntype;
98
99 geo_node_type_base(&ntype, "GeometryNodeImportCSV");
100 ntype.ui_name = "Import CSV";
101 ntype.ui_description = "Import geometry from an CSV file";
102 ntype.nclass = NODE_CLASS_INPUT;
104 ntype.declare = node_declare;
105
107}
109
110} // namespace blender::nodes::node_geo_import_csv
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
@ RPT_STORE
Definition BKE_report.hh:56
void BKE_reports_free(ReportList *reports)
Definition report.cc:97
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:82
#define LISTBASE_FOREACH(type, var, list)
#define BLI_SCOPED_DEFER(function_to_defer)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
#define ELEM(...)
#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 node_register_type(bNodeType &ntype)
Definition node.cc:2416
PointCloud * import_csv_as_pointcloud(const CSVImportParams &import_params)
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_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void count_memory(MemoryCounter &memory) const
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
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)