Blender V5.0
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.h"
10#include "BLI_string.h"
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")
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");
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"));
46 if (!path) {
47 params.set_default_remaining_outputs();
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 bke::Instances *instances = new bke::Instances();
65 for (GeometrySet geometry : geometries) {
66 const int handle = instances->add_reference(bke::InstanceReference{std::move(geometry)});
67 instances->add_instance(handle, float4x4::identity());
68 }
69
70 auto cached_value = std::make_unique<LoadObjCache>();
71 cached_value->geometry = GeometrySet::from_instances(instances);
72
73 LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
74 cached_value->warnings.append_as(*report);
75 }
76
77 return cached_value;
78 });
79
80 for (const geo_eval_log::NodeWarning &warning : cached_value->warnings) {
81 params.error_message_add(warning.type, warning.message);
82 }
83
84 params.set_output("Instances", cached_value->geometry);
85#else
86 params.error_message_add(NodeWarningType::Error,
87 TIP_("Disabled, Blender was compiled without OBJ I/O"));
88 params.set_default_remaining_outputs();
89#endif
90}
91
92static void node_register()
93{
94 static blender::bke::bNodeType ntype;
95
96 geo_node_type_base(&ntype, "GeometryNodeImportOBJ", GEO_NODE_IMPORT_OBJ);
97 ntype.ui_name = "Import OBJ";
98 ntype.ui_description = "Import geometry from an OBJ file";
99 ntype.enum_name_legacy = "IMPORT_OBJ";
100 ntype.nclass = NODE_CLASS_INPUT;
102 ntype.declare = node_declare;
103
105}
107
108} // namespace blender::nodes::node_geo_import_obj
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define GEO_NODE_IMPORT_OBJ
@ 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)
void OBJ_import_geometries(const OBJImportParams *import_params, blender::Vector< blender::bke::GeometrySet > &geometries)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
@ PROP_FILEPATH
Definition RNA_types.hh:236
int add_reference(const InstanceReference &reference)
Definition instances.cc:261
void add_instance(int instance_handle, const float4x4 &transform)
Definition instances.cc:204
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)
char filepath[FILE_MAX]
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_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)