Blender V5.0
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.h"
10#include "BLI_string.h"
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")
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");
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"));
47 if (!path) {
48 params.set_default_remaining_outputs();
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 LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
71 cached_value->warnings.append_as(*report);
72 }
73
74 return cached_value;
75 });
76
77 for (const geo_eval_log::NodeWarning &warning : cached_value->warnings) {
78 params.error_message_add(warning.type, warning.message);
79 }
80
81 params.set_output("Mesh", cached_value->geometry);
82
83#else
84 params.error_message_add(NodeWarningType::Error,
85 TIP_("Disabled, Blender was compiled without STL I/O"));
86 params.set_default_remaining_outputs();
87#endif
88}
89
90static void node_register()
91{
92 static blender::bke::bNodeType ntype;
93
94 geo_node_type_base(&ntype, "GeometryNodeImportSTL", 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:447
#define GEO_NODE_IMPORT_STL
@ 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 TIP_(msgid)
@ IO_AXIS_Y
@ IO_AXIS_NEGATIVE_Z
Mesh * STL_import_mesh(const STLImportParams *import_params)
Definition IO_stl.cc:27
#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
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)
eIOAxis up_axis
Definition IO_stl.hh:25
eIOAxis forward_axis
Definition IO_stl.hh:24
char filepath[FILE_MAX]
Definition IO_stl.hh:23
ReportList * reports
Definition IO_stl.hh:31
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
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)