Blender V5.3
node_geo_import_stl.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
8#include "BLI_listbase.hh"
10#include "BLI_string.hh"
11
12#include "DNA_mesh_types.h"
13
14#include "BKE_report.hh"
15
16#include "IO_stl.hh"
17
19
21{
22 b.add_input<decl::String>("Path"_ustr)
23 .subtype(PROP_FILEPATH)
24 .path_filter("*.stl")
25 .optional_label()
26 .description("Path to a STL file");
27
28 b.add_output<decl::Geometry>("Mesh"_ustr);
29}
30
32 public:
35
36 void count_memory(MemoryCounter &counter) const override
37 {
38 this->geometry.count_memory(counter);
39 }
40};
41
43{
44#ifdef WITH_IO_STL
45 const std::optional<std::string> path = params.ensure_absolute_path(
46 params.extract_input<std::string>("Path"_ustr));
47 if (!path) {
49 return;
50 }
51
52 std::shared_ptr<const LoadStlCache> cached_value = memory_cache::get_loaded<LoadStlCache>(
53 GenericStringKey{"import_stl_node"}, {StringRefNull(*path)}, [&]() {
54 STLImportParams import_params;
55 STRNCPY(import_params.filepath, path->c_str());
56
57 import_params.forward_axis = IO_AXIS_NEGATIVE_Z;
58 import_params.up_axis = IO_AXIS_Y;
59
60 ReportList reports;
61 BKE_reports_init(&reports, RPT_STORE);
62 BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); })
63 import_params.reports = &reports;
64
65 Mesh *mesh = STL_import_mesh(&import_params);
66
67 auto cached_value = std::make_unique<LoadStlCache>();
68 cached_value->geometry = GeometrySet::from_mesh(mesh);
69
70 for (Report &report : (import_params.reports)->list) {
71 cached_value->warnings.append_as(report);
72 }
73
74 return cached_value;
75 });
76
77 for (const NodeWarning &warning : cached_value->warnings) {
78 params.error_message_add(warning.type, warning.message);
79 }
80
81 params.set_output("Mesh"_ustr, cached_value->geometry);
82
83#else
85 TIP_("Disabled, Blender was compiled without STL I/O"));
87#endif
88}
89
90static void node_register()
91{
92 static bke::bNodeType ntype;
93
94 geo_node_type_base(&ntype, "GeometryNodeImportSTL"_ustr, GEO_NODE_IMPORT_STL);
95 ntype.ui_name = "Import STL";
96 ntype.ui_description = "Import a mesh from an STL file";
97 ntype.enum_name_legacy = "IMPORT_STL";
100 ntype.declare = node_declare;
101
103}
105
106} // namespace blender::nodes::node_geo_import_stl
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_IMPORT_STL
#define BLI_SCOPED_DEFER(function_to_defer)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr StringRefNull()
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_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_reports_free(ReportList *reports)
Definition report.cc:106
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:91
Mesh * STL_import_mesh(const STLImportParams *import_params)
Definition IO_stl.cc:29
@ PROP_FILEPATH
Definition RNA_types.hh:284
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
ReportList * reports
Definition IO_stl.hh:35
char filepath[FILE_MAX]
Definition IO_stl.hh:27
void count_memory(MemoryCounter &memory) const
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)