Blender V5.3
node_geo_import_obj.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 "BKE_instances.hh"
13#include "BKE_report.hh"
14
15#include "IO_wavefront_obj.hh"
16
18
20{
21 b.add_input<decl::String>("Path"_ustr)
22 .subtype(PROP_FILEPATH)
23 .path_filter("*.obj")
24 .optional_label()
25 .description("Path to a OBJ file");
26
27 b.add_output<decl::Geometry>("Instances"_ustr);
28}
29
31 public:
34
35 void count_memory(MemoryCounter &counter) const override
36 {
37 this->geometry.count_memory(counter);
38 }
39};
40
42{
43#ifdef WITH_IO_WAVEFRONT_OBJ
44 const std::optional<std::string> path = params.ensure_absolute_path(
45 params.extract_input<std::string>("Path"_ustr));
46 if (!path) {
48 return;
49 }
50
51 std::shared_ptr<const LoadObjCache> cached_value = memory_cache::get_loaded<LoadObjCache>(
52 GenericStringKey{"import_obj_node"}, {StringRefNull(*path)}, [&]() {
53 OBJImportParams import_params;
54 STRNCPY(import_params.filepath, path->c_str());
55
56 ReportList reports;
57 BKE_reports_init(&reports, RPT_STORE);
58 BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); });
59 import_params.reports = &reports;
60
61 Vector<bke::GeometrySet> geometries;
62 OBJ_import_geometries(&import_params, geometries);
63
64 auto instances = std::make_unique<bke::Instances>(geometries.size());
65 MutableSpan<int> handles = instances->reference_handles_for_write();
66 instances->transforms_for_write().fill(float4x4::identity());
67 for (const int i : geometries.index_range()) {
68 handles[i] = instances->add_reference(bke::InstanceReference{std::move(geometries[i])});
69 }
70
71 auto cached_value = std::make_unique<LoadObjCache>();
72 cached_value->geometry = GeometrySet::from_instances(std::move(instances));
73
74 for (Report &report : (import_params.reports)->list) {
75 cached_value->warnings.append_as(report);
76 }
77
78 return cached_value;
79 });
80
81 for (const NodeWarning &warning : cached_value->warnings) {
82 params.error_message_add(warning.type, warning.message);
83 }
84
85 params.set_output("Instances"_ustr, cached_value->geometry);
86#else
88 TIP_("Disabled, Blender was compiled without OBJ I/O"));
90#endif
91}
92
93static void node_register()
94{
95 static bke::bNodeType ntype;
96
97 geo_node_type_base(&ntype, "GeometryNodeImportOBJ"_ustr, GEO_NODE_IMPORT_OBJ);
98 ntype.ui_name = "Import OBJ";
99 ntype.ui_description = "Import geometry from an OBJ file";
100 ntype.enum_name_legacy = "IMPORT_OBJ";
101 ntype.nclass = NODE_CLASS_INPUT;
103 ntype.declare = node_declare;
104
106}
108
109} // namespace blender::nodes::node_geo_import_obj
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_IMPORT_OBJ
#define BLI_SCOPED_DEFER(function_to_defer)
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr StringRefNull()
int64_t size() const
IndexRange index_range() const
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
@ PROP_FILEPATH
Definition RNA_types.hh:284
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void OBJ_import_geometries(const OBJImportParams *import_params, Vector< bke::GeometrySet > &geometries)
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_instances(std::unique_ptr< Instances > instances)