Blender V5.3
stl_import.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
8
9#include <cstdio>
10
11#include "BKE_context.hh"
12#include "BKE_layer.hh"
13#include "BKE_library.hh"
14#include "BKE_mesh.hh"
15#include "BKE_object.hh"
16#include "BKE_report.hh"
17
19#include "DNA_layer_types.h"
20#include "DNA_scene_types.h"
21
22#include "BLI_fileops.hh"
23#include "BLI_math_matrix_c.hh"
25#include "BLI_memory_utils.hh"
26#include "BLI_string.hh"
27
28#include "DNA_object_types.h"
29
30#include "DEG_depsgraph.hh"
32
33#include "stl_data.hh"
34#include "stl_import.hh"
37
38#include "CLG_log.h"
39
40namespace blender {
41
42static CLG_LogRef LOG = {"io.stl"};
43
44namespace io::stl {
45
47{
48 CLOG_ERROR(&LOG, "STL Importer: failed to read file");
49 if (feof(file)) {
50 CLOG_ERROR(&LOG, "End of file reached");
51 }
52 else if (ferror(file)) {
53 perror("Error");
54 }
55}
56
57Mesh *read_stl_file(const STLImportParams &import_params)
58{
59 FILE *file = BLI_fopen(import_params.filepath, "rb");
60 if (!file) {
61 CLOG_ERROR(&LOG, "Failed to open STL file:'%s'.", import_params.filepath);
62 BKE_reportf(import_params.reports,
64 "STL Import: Cannot open file '%s'",
65 import_params.filepath);
66 return nullptr;
67 }
68 BLI_SCOPED_DEFER([&]() { fclose(file); });
69
70 /* Detect STL file type by comparing file size with expected file size,
71 * could check if file starts with "solid", but some files do not adhere,
72 * this is the same as the old Python importer.
73 */
74 uint32_t num_tri = 0;
75 size_t file_size = BLI_file_size(import_params.filepath);
76 fseek(file, BINARY_HEADER_SIZE, SEEK_SET);
77 if (fread(&num_tri, sizeof(uint32_t), 1, file) != 1) {
79 BKE_reportf(import_params.reports,
81 "STL Import: Failed to read file '%s'",
82 import_params.filepath);
83 return nullptr;
84 }
85 bool is_ascii_stl = (file_size != (BINARY_HEADER_SIZE + 4 + BINARY_STRIDE * num_tri));
86
87 Mesh *mesh = is_ascii_stl ?
88 read_stl_ascii(import_params.filepath, import_params.use_facet_normal) :
89 read_stl_binary(file, import_params.use_facet_normal);
90
91 if (mesh == nullptr) {
92 CLOG_ERROR(&LOG, "STL Importer: Failed to import mesh '%s'", import_params.filepath);
93 BKE_reportf(import_params.reports,
95 "STL Import: Failed to import mesh from file '%s'",
96 import_params.filepath);
97 return nullptr;
98 }
99
100 if (import_params.use_mesh_validate) {
101 bool verbose_validate = false;
102#ifndef NDEBUG
103 verbose_validate = true;
104#endif
105 bke::mesh_validate(*mesh, verbose_validate);
106 }
107
108 return mesh;
109}
110
111void importer_main(const bContext *C, const STLImportParams &import_params)
112{
113 Main *bmain = CTX_data_main(C);
114 Scene *scene = CTX_data_scene(C);
115 ViewLayer *view_layer = CTX_data_view_layer(C);
116 importer_main(bmain, scene, view_layer, import_params);
117}
118
119void importer_main(Main *bmain,
120 Scene *scene,
121 ViewLayer *view_layer,
122 const STLImportParams &import_params)
123{
124 /* Name used for both mesh and object. */
125 char ob_name[FILE_MAX];
126 STRNCPY(ob_name, BLI_path_basename(import_params.filepath));
128
129 Mesh *mesh = read_stl_file(import_params);
130 if (!mesh) {
131 return;
132 }
133
134 Mesh *mesh_in_main = BKE_mesh_add(bmain, ob_name);
135 BKE_mesh_nomain_to_mesh(mesh, mesh_in_main, nullptr);
136 BKE_view_layer_base_deselect_all(*bmain, scene, view_layer);
138 if (!ID_IS_EDITABLE(lc->collection)) {
139 BKE_report(import_params.reports,
141 "Could not find an editable collection in current scene, imported data will not be "
142 "instantiated");
143 }
144 Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name);
145 obj->data = id_cast<ID *>(mesh_in_main);
147 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
148 if (Base *base = BKE_view_layer_base_find(view_layer, obj)) {
149 /* `base` will be nullptr if the Object could not be instantiated in the current viewlayer. */
151 }
152
153 float global_scale = import_params.global_scale;
154 if ((scene->unit.system != USER_UNIT_NONE) && import_params.use_scene_unit) {
155 global_scale /= scene->unit.scale_length;
156 }
157 float scale_vec[3] = {global_scale, global_scale, global_scale};
158 float obmat3x3[3][3];
159 unit_m3(obmat3x3);
160 float obmat4x4[4][4];
161 unit_m4(obmat4x4);
162 /* +Y-forward and +Z-up are the Blender's default axis settings. */
164 IO_AXIS_Y, IO_AXIS_Z, import_params.forward_axis, import_params.up_axis, obmat3x3);
165 copy_m4_m3(obmat4x4, obmat3x3);
166 rescale_m4(obmat4x4, scale_vec);
167 BKE_object_apply_mat4(obj, obmat4x4, true, false);
168
172 DEG_id_tag_update_ex(bmain, &obj->id, flags);
175}
176} // namespace io::stl
177} // namespace blender
General operations, lookup, etc. for blender objects.
File and directory operations.
#define BLI_SCOPED_DEFER(function_to_defer)
#define FILE_MAX
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
bool mesh_validate(Mesh &mesh, bool verbose=false, bool allow_missing_edges=false)
Mesh * read_stl_binary(FILE *file, const bool use_custom_normals)
constexpr size_t BINARY_STRIDE
Definition stl_data.hh:22
void importer_main(const bContext *C, const STLImportParams &import_params)
constexpr size_t BINARY_HEADER_SIZE
Definition stl_data.hh:21
void stl_import_report_error(FILE *file)
Definition stl_import.cc:46
Mesh * read_stl_ascii(const char *filepath, const bool use_custom_normals)
Mesh * read_stl_file(const STLImportParams &import_params)
Definition stl_import.cc:57
BMesh Iterators.
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
LayerCollection * BKE_layer_collection_get_active_editable(ViewLayer *view_layer)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
@ RPT_WARNING
Definition BKE_report.hh:40
void copy_m4_m3(float m1[4][4], const float m2[3][3])
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob, bool process_shape_keys=true)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_object_apply_mat4(Object *ob, const float mat[4][4], bool use_compat, bool use_parent)
Definition object.cc:3663
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
unsigned int uint32_t
Definition BKE_text.h:81
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void unit_m4(float m[4][4])
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
size_t BLI_file_size(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:228
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
void unit_m3(float m[3][3])
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1104
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void rescale_m4(float mat[4][4], const float scale[3])
static CLG_LogRef LOG
Definition logging.cpp:12
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
Mesh * BKE_mesh_add(Main *bmain, const char *name)
struct Collection * collection
ReportList * reports
Definition IO_stl.hh:35
char filepath[FILE_MAX]
Definition IO_stl.hh:27
struct UnitSettings unit