Geometry Nodes: support custom delimiter in Import CSV node

Previously, only `,` was a valid delimiter, but it's also common to have e.g.
tab or semicolon. Only single characters are supported as delimiters. A few
characters are not allowed because they have other meanings: `\n`, `\r`, `"`,
`\\`.

Note: The best way to get a tab character is the use the Special Characters node
currently.

Pull Request: https://projects.blender.org/blender/blender/pulls/135468
This commit is contained in:
Jacques Lucke
2025-03-04 23:58:02 +01:00
parent 0ee642611c
commit c233955ef9
3 changed files with 16 additions and 0 deletions

View File

@@ -18,6 +18,7 @@ namespace blender::io::csv {
struct CSVImportParams {
/** Full path to the source CSV file to import. */
char filepath[FILE_MAX];
char delimiter = ',';
ReportList *reports = nullptr;
};

View File

@@ -281,6 +281,7 @@ PointCloud *import_csv_as_pointcloud(const CSVImportParams &import_params)
LinearAllocator<> allocator;
Array<ColumnInfo> columns_info;
csv_parse::CsvParseOptions parse_options;
parse_options.delimiter = import_params.delimiter;
const auto parse_header = [&](const csv_parse::CsvRecord &record) {
columns_info.reinitialize(record.size());

View File

@@ -20,6 +20,7 @@ static void node_declare(NodeDeclarationBuilder &b)
.path_filter("*.csv")
.hide_label()
.description("Path to a CSV file");
b.add_input<decl::String>("Delimiter").default_value(",");
b.add_output<decl::Geometry>("Point Cloud");
}
@@ -33,8 +34,21 @@ static void node_geo_exec(GeoNodeExecParams params)
params.set_default_remaining_outputs();
return;
}
const std::string delimiter = params.extract_input<std::string>("Delimiter");
if (delimiter.size() != 1) {
params.error_message_add(NodeWarningType::Error, TIP_("Delimiter must be a single character"));
params.set_default_remaining_outputs();
return;
}
if (ELEM(delimiter[0], '\n', '\r', '"', '\\')) {
params.error_message_add(NodeWarningType::Error,
TIP_("Delimiter must not be \\n, \\r, \" or \\"));
params.set_default_remaining_outputs();
return;
}
blender::io::csv::CSVImportParams import_params{};
import_params.delimiter = delimiter[0];
STRNCPY(import_params.filepath, path->c_str());
ReportList reports;