Blender V4.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
7#include "BLI_string.h"
8
9#include "BKE_instances.hh"
10#include "BKE_mesh.hh"
11#include "BKE_report.hh"
12
13#include "IO_wavefront_obj.hh"
14
15#include "node_geometry_util.hh"
16
18
20{
21 b.add_input<decl::String>("Path")
22 .subtype(PROP_FILEPATH)
23 .hide_label()
24 .description("Path to a OBJ file");
25
26 b.add_output<decl::Geometry>("Instances");
27}
28
30{
31#ifdef WITH_IO_WAVEFRONT_OBJ
32 const std::string path = params.extract_input<std::string>("Path");
33 if (path.empty()) {
34 params.set_default_remaining_outputs();
35 return;
36 }
37
38 OBJImportParams import_params;
39 STRNCPY(import_params.filepath, path.c_str());
40
41 ReportList reports;
42 BKE_reports_init(&reports, RPT_STORE);
43 BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); });
44 import_params.reports = &reports;
45
46 Vector<bke::GeometrySet> geometries;
47 OBJ_import_geometries(&import_params, geometries);
48
49 LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
50 NodeWarningType type;
51 switch (report->type) {
52 case RPT_ERROR:
53 type = NodeWarningType::Error;
54 break;
55 default:
56 type = NodeWarningType::Info;
57 break;
58 }
59 params.error_message_add(type, TIP_(report->message));
60 }
61
62 if (geometries.is_empty()) {
63 params.set_default_remaining_outputs();
64 return;
65 }
66
67 bke::Instances *instances = new bke::Instances();
68 for (GeometrySet geometry : geometries) {
69 const int handle = instances->add_reference(bke::InstanceReference{std::move(geometry)});
70 instances->add_instance(handle, float4x4::identity());
71 }
72
73 params.set_output("Instances", GeometrySet::from_instances(instances));
74#else
75 params.error_message_add(NodeWarningType::Error,
76 TIP_("Disabled, Blender was compiled without OBJ I/O"));
77 params.set_default_remaining_outputs();
78#endif
79}
80
81static void node_register()
82{
83 static blender::bke::bNodeType ntype;
84
85 geo_node_type_base(&ntype, GEO_NODE_IMPORT_OBJ, "Import OBJ", NODE_CLASS_INPUT);
86
88 ntype.declare = node_declare;
90
92}
94
95} // namespace blender::nodes::node_geo_import_obj
#define NODE_CLASS_INPUT
Definition BKE_node.hh:404
void BKE_reports_free(ReportList *reports)
Definition report.cc:69
void BKE_reports_init(ReportList *reports, int flag)
Definition report.cc:54
#define LISTBASE_FOREACH(type, var, list)
#define BLI_SCOPED_DEFER(function_to_defer)
#define STRNCPY(dst, src)
Definition BLI_string.h:593
#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:139
local_group_size(16, 16) .push_constant(Type b
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType *ntype)
Definition node.cc:1708
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
void search_link_ops_for_import_node(GatherLinkSearchOpParams &params)
void geo_node_type_base(blender::bke::bNodeType *ntype, int type, const char *name, short nclass)
ReportList * reports
char filepath[FILE_MAX]
static GeometrySet from_instances(Instances *instances, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:218
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:339
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:363
NodeDeclareFunction declare
Definition BKE_node.hh:347