Blender V5.3
obj_import_file_reader.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 "BKE_report.hh"
10
11#include "BLI_fileops.hh"
12#include "BLI_map.hh"
13#include "BLI_math_color_c.hh"
14#include "BLI_math_vector_c.hh"
16#include "BLI_mmap.hh"
17#include "BLI_string.hh"
18#include "BLI_string_ref.hh"
19#include "BLI_vector.hh"
20
21#include "IO_string_utils.hh"
22#include "IO_validate.hh"
23
24#include "obj_export_mtl.hh"
26
27#include <algorithm>
28#include <charconv>
29
30#include <fcntl.h>
31#ifndef WIN32
32# include <unistd.h>
33#else
34# include <io.h>
35#endif
36
37#include "CLG_log.h"
38
39namespace blender {
40
41static CLG_LogRef LOG = {"io.obj"};
42
43namespace io::obj {
44
45using std::string;
46
51static Geometry *create_geometry(Geometry *const prev_geometry,
52 const eGeometryType new_type,
53 StringRef name,
54 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
55{
56 auto new_geometry = [&]() {
57 r_all_geometries.append(std::make_unique<Geometry>());
58 Geometry *g = r_all_geometries.last().get();
59 g->geom_type_ = new_type;
60 g->geometry_name_ = name.is_empty() ? "New object" : name;
61 return g;
62 };
63
64 if (prev_geometry && prev_geometry->geom_type_ == GEOM_MESH) {
65 /* After the creation of a Geometry instance, at least one element has been found in the OBJ
66 * file that indicates that it is a mesh (faces or edges). */
67 if (!prev_geometry->face_elements_.is_empty() || !prev_geometry->edges_.is_empty()) {
68 return new_geometry();
69 }
70 if (new_type == GEOM_MESH) {
71 /* A Geometry created initially with a default name now found its name. */
72 prev_geometry->geometry_name_ = name;
73 return prev_geometry;
74 }
75 if (new_type == GEOM_CURVE) {
76 /* The object originally created is not a mesh now that curve data
77 * follows the vertex coordinates list. */
78 prev_geometry->geom_type_ = GEOM_CURVE;
79 return prev_geometry;
80 }
81 }
82
83 return new_geometry();
84}
85
86static void geom_add_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
87{
88 r_global_vertices.flush_mrgb_block();
89 float3 vert;
90 p = parse_floats(p, end, 0.0f, vert, 3);
91 r_global_vertices.vertices.append(vert);
92 /* OBJ extension: `xyzrgb` vertex colors, when the vertex position
93 * is followed by 3 more RGB color components. See
94 * http://paulbourke.net/dataformats/obj/colour.html */
95 if (p < end) {
96 float3 srgb;
97 p = parse_floats(p, end, -1.0f, srgb, 3);
98 if (srgb.x >= 0 && srgb.y >= 0 && srgb.z >= 0) {
99 float3 linear;
100 srgb_to_linearrgb_v3_v3(linear, srgb);
101 r_global_vertices.set_vertex_color(r_global_vertices.vertices.size() - 1, linear);
102 }
103 else if (srgb.x > 0) {
104 /* Treats value in srgb.x as weight. */
105 r_global_vertices.set_vertex_weight(r_global_vertices.vertices.size() - 1, srgb.x);
106 }
107 }
108 UNUSED_VARS(p);
109}
110
111static void geom_add_mrgb_colors(const char *p, const char *end, GlobalVertices &r_global_vertices)
112{
113 /* MRGB color extension, in the form of
114 * "#MRGB MMRRGGBBMMRRGGBB ..."
115 * http://paulbourke.net/dataformats/obj/colour.html */
116 p = drop_whitespace(p, end);
117 const int mrgb_length = 8;
118 while (p + mrgb_length <= end) {
119 uint32_t value = 0;
120 std::from_chars_result res = std::from_chars(p, p + mrgb_length, value, 16);
121 if (ELEM(res.ec, std::errc::invalid_argument, std::errc::result_out_of_range)) {
122 return;
123 }
124 uchar srgb[4];
125 srgb[0] = (value >> 16) & 0xFF;
126 srgb[1] = (value >> 8) & 0xFF;
127 srgb[2] = value & 0xFF;
128 srgb[3] = 0xFF;
129 float linear[4];
130 srgb_to_linearrgb_uchar4(linear, srgb);
131
132 r_global_vertices.mrgb_block.append(float3(linear[0], linear[1], linear[2]));
133
134 p += mrgb_length;
135 }
136}
137
138static void geom_add_vertex_normal(const char *p,
139 const char *end,
140 GlobalVertices &r_global_vertices)
141{
142 float3 normal;
143 parse_floats(p, end, 0.0f, normal, 3);
144 /* Normals can be printed with only several digits in the file,
145 * making them ever-so-slightly non unit length. Make sure they are
146 * normalized. */
147 normalize_v3(normal);
148 r_global_vertices.vert_normals.append(normal);
149}
150
151static void geom_add_uv_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
152{
153 float2 uv;
154 parse_floats(p, end, 0.0f, uv, 2);
155 r_global_vertices.uv_vertices.append(uv);
156}
157
165static const char *parse_vertex_index(const char *p, const char *end, size_t n_elems, int &r_index)
166{
167 p = parse_int(p, end, INT32_MAX, r_index, false);
168 if (r_index != INT32_MAX) {
169 r_index += r_index < 0 ? n_elems : -1;
170 if (r_index < 0 || r_index >= n_elems) {
171 CLOG_WARN(&LOG, "Invalid vertex index %i (valid range [0, %zu))", r_index, n_elems);
172 r_index = INT32_MAX;
173 }
174 }
175 return p;
176}
177
187static void geom_add_polyline(Geometry *geom,
188 const char *p,
189 const char *end,
190 GlobalVertices &r_global_vertices)
191{
192 int last_vertex_index;
193 p = drop_whitespace(p, end);
194 p = parse_vertex_index(p, end, r_global_vertices.vertices.size(), last_vertex_index);
195
196 if (last_vertex_index == INT32_MAX) {
197 CLOG_WARN(&LOG, "Skipping invalid OBJ polyline.");
198 return;
199 }
200 geom->track_vertex_index(last_vertex_index);
201
202 while (p < end) {
203 int vertex_index;
204
205 /* Lines can contain texture coordinate indices, just ignore them. */
206 p = drop_non_whitespace(p, end);
207 /* Skip whitespace to get to the next vertex. */
208 p = drop_whitespace(p, end);
209
210 p = parse_vertex_index(p, end, r_global_vertices.vertices.size(), vertex_index);
211 if (vertex_index == INT32_MAX) {
212 break;
213 }
214
215 geom->edges_.append({last_vertex_index, vertex_index});
216 geom->track_vertex_index(vertex_index);
217 last_vertex_index = vertex_index;
218 }
219}
220
221static void geom_add_polygon(Geometry *geom,
222 const char *p,
223 const char *end,
224 const GlobalVertices &global_vertices,
225 const int material_index,
226 const int group_index,
227 const bool shaded_smooth)
228{
229 FaceElem curr_face;
230 curr_face.shaded_smooth = shaded_smooth;
231 curr_face.material_index = material_index;
232 if (group_index >= 0) {
233 curr_face.vertex_group_index = group_index;
234 geom->has_vertex_groups_ = true;
235 }
236
237 const int orig_corners_size = geom->face_corners_.size();
238 curr_face.start_index_ = orig_corners_size;
239
240 bool face_valid = true;
241 p = drop_whitespace(p, end);
242 while (p < end && face_valid) {
243 FaceCorner corner;
244 bool got_uv = false, got_normal = false;
245 /* Parse vertex index. */
246 p = parse_int(p, end, INT32_MAX, corner.vert_index, false);
247
248 /* Skip parsing when we reach start of the comment. */
249 if (*p == '#') {
250 break;
251 }
252
253 face_valid &= corner.vert_index != INT32_MAX;
254 if (p < end && *p == '/') {
255 /* Parse UV index. */
256 ++p;
257 if (p < end && *p != '/') {
258 p = parse_int(p, end, INT32_MAX, corner.uv_vert_index, false);
259 got_uv = corner.uv_vert_index != INT32_MAX;
260 }
261 /* Parse normal index. */
262 if (p < end && *p == '/') {
263 ++p;
264 p = parse_int(p, end, INT32_MAX, corner.vertex_normal_index, false);
265 got_normal = corner.vertex_normal_index != INT32_MAX;
266 }
267 }
268 /* Always keep stored indices non-negative and zero-based. */
269 corner.vert_index += corner.vert_index < 0 ? global_vertices.vertices.size() : -1;
270 if (corner.vert_index < 0 || corner.vert_index >= global_vertices.vertices.size()) {
271 CLOG_WARN(&LOG,
272 "Invalid vertex index %i (valid range [0, %zu)), ignoring face",
273 corner.vert_index,
274 size_t(global_vertices.vertices.size()));
275 face_valid = false;
276 }
277 else {
278 geom->track_vertex_index(corner.vert_index);
279 }
280 /* Ignore UV index, if the geometry does not have any UVs (#103212). */
281 if (got_uv && !global_vertices.uv_vertices.is_empty()) {
282 corner.uv_vert_index += corner.uv_vert_index < 0 ? global_vertices.uv_vertices.size() : -1;
283 if (corner.uv_vert_index < 0 || corner.uv_vert_index >= global_vertices.uv_vertices.size()) {
284 CLOG_WARN(&LOG,
285 "Invalid UV index %i (valid range [0, %zu)), ignoring face",
286 corner.uv_vert_index,
287 size_t(global_vertices.uv_vertices.size()));
288 face_valid = false;
289 }
290 }
291 /* Ignore corner normal index, if the geometry does not have any normals.
292 * Some obj files out there do have face definitions that refer to normal indices,
293 * without any normals being present (#98782). */
294 if (got_normal && !global_vertices.vert_normals.is_empty()) {
295 corner.vertex_normal_index += corner.vertex_normal_index < 0 ?
296 global_vertices.vert_normals.size() :
297 -1;
298 if (corner.vertex_normal_index < 0 ||
299 corner.vertex_normal_index >= global_vertices.vert_normals.size())
300 {
301 CLOG_WARN(&LOG,
302 "Invalid normal index %i (valid range [0, %zu)), ignoring face",
303 corner.vertex_normal_index,
304 size_t(global_vertices.vert_normals.size()));
305 face_valid = false;
306 }
307 }
308 geom->face_corners_.append(corner);
309 curr_face.corner_count_++;
310
311 /* Some files contain extra stuff per face (e.g. 4 indices); skip any remainder (#103441). */
312 p = drop_non_whitespace(p, end);
313 /* Skip whitespace to get to the next face corner. */
314 p = drop_whitespace(p, end);
315 }
316
317 if (face_valid) {
318 geom->face_elements_.append(curr_face);
319 geom->total_corner_ += curr_face.corner_count_;
320 }
321 else {
322 /* Remove just-added corners for the invalid face. */
323 geom->face_corners_.resize(orig_corners_size);
324 geom->has_invalid_faces_ = true;
325 }
326}
327
329 const char *p,
330 const char *end,
331 const StringRef group_name,
332 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
333{
334 p = drop_whitespace(p, end);
335 if (!StringRef(p, end).startswith("bspline") && !StringRef(p, end).startswith("rat bspline")) {
336 CLOG_WARN(&LOG, "Curve type not supported: '%s'", string(p, end).c_str());
337 return geom;
338 }
339 geom = create_geometry(geom, GEOM_CURVE, group_name, r_all_geometries);
340 geom->nurbs_element_.group_ = group_name;
341 return geom;
342}
343
344static void geom_set_curve_degree(Geometry *geom, const char *p, const char *end)
345{
346 parse_int(p, end, 3, geom->nurbs_element_.degree);
347}
348
350 const char *p,
351 const char *end,
352 const GlobalVertices &global_vertices)
353{
354 /* Parse curve parameter range. */
355 p = parse_floats(p, end, 0, geom->nurbs_element_.range, 2);
356 /* Parse indices. */
357 while (p < end) {
358 int index;
359 p = parse_int(p, end, INT32_MAX, index);
360 if (index == INT32_MAX) {
361 return;
362 }
363 /* Always keep stored indices non-negative and zero-based. */
364 index += index < 0 ? global_vertices.vertices.size() : -1;
365 if (!validate::index_in_range(index, global_vertices.vertices.size())) {
366 index = 0;
367 }
369 }
370}
371
372static void geom_add_curve_parameters(Geometry *geom, const char *p, const char *end)
373{
374 p = drop_whitespace(p, end);
375 if (p == end) {
376 CLOG_ERROR(&LOG, "Invalid OBJ curve parm line");
377 return;
378 }
379 if (*p != 'u') {
380 CLOG_WARN(&LOG, "OBJ curve surfaces are not supported, found '%c'", *p);
381 return;
382 }
383 ++p;
384
385 while (p < end) {
386 float val;
387 p = parse_float(p, end, FLT_MAX, val);
388 if (val != FLT_MAX) {
389 geom->nurbs_element_.parm.append(val);
390 }
391 else {
392 CLOG_ERROR(&LOG, "OBJ curve parm line has invalid number");
393 return;
394 }
395 }
396}
397
398static void geom_update_group(const StringRef rest_line, string &r_group_name)
399{
400 if (rest_line.find("off") != string::npos || rest_line.find("null") != string::npos ||
401 rest_line.find("default") != string::npos)
402 {
403 /* Set group for future elements like faces or curves to empty. */
404 r_group_name = "";
405 return;
406 }
407 r_group_name = rest_line;
408}
409
410static void geom_update_smooth_group(const char *p, const char *end, bool &r_state_shaded_smooth)
411{
412 p = drop_whitespace(p, end);
413 /* Some implementations use "0" and "null" too, in addition to "off". */
414 const StringRef line = StringRef(p, end);
415 if (line == "0" || line.startswith("off") || line.startswith("null")) {
416 r_state_shaded_smooth = false;
417 return;
418 }
419
420 int smooth = 0;
421 parse_int(p, end, 0, smooth);
422 r_state_shaded_smooth = smooth != 0;
423}
424
425static void geom_new_object(const char *p,
426 const char *end,
427 bool &r_state_shaded_smooth,
428 string &r_state_group_name,
429 int &r_state_material_index,
430 Geometry *&r_curr_geom,
431 Vector<std::unique_ptr<Geometry>> &r_all_geometries)
432{
433 r_state_shaded_smooth = false;
434 r_state_group_name = "";
435 /* Reset object-local material index that's used in face information.
436 * NOTE: do not reset the material name; that has to carry over
437 * into the next object if needed. */
438 r_state_material_index = -1;
439 r_curr_geom = create_geometry(
440 r_curr_geom, GEOM_MESH, StringRef(p, end).trim(), r_all_geometries);
441}
442
443OBJParser::OBJParser(const OBJImportParams &import_params) : import_params_(import_params)
444{
445 const int obj_file = BLI_open(import_params_.filepath, O_BINARY | O_RDONLY, 0);
446 if (obj_file == -1) {
447 CLOG_ERROR(&LOG, "Cannot read from OBJ file:'%s'.", import_params_.filepath);
448 BKE_reportf(import_params_.reports,
449 RPT_ERROR,
450 "OBJ Import: Cannot open file '%s'",
451 import_params_.filepath);
452 return;
453 }
454 mmap_file_ = BLI_mmap_open(obj_file);
455 close(obj_file);
456 if (mmap_file_ == nullptr) {
457 CLOG_ERROR(&LOG, "Cannot mmap OBJ file:'%s'.", import_params_.filepath);
458 BKE_reportf(import_params_.reports,
459 RPT_ERROR,
460 "OBJ Import: Cannot mmap file '%s'",
461 import_params_.filepath);
462 return;
463 }
464}
465
467{
468 if (mmap_file_ != nullptr) {
469 BLI_mmap_free(mmap_file_);
470 }
471}
472
473/* If line starts with keyword followed by whitespace, returns true and drops it from the line. */
474static bool parse_keyword(const char *&p, const char *end, StringRef keyword)
475{
476 const size_t keyword_len = keyword.size();
477 if (end - p < keyword_len + 1) {
478 return false;
479 }
480 if (memcmp(p, keyword.data(), keyword_len) != 0) {
481 return false;
482 }
483 /* Treat any ASCII control character as white-space;
484 * don't use `isspace()` for performance reasons. */
485 if (p[keyword_len] > ' ') {
486 return false;
487 }
488 p += keyword_len + 1;
489 return true;
490}
491
492/* Special case: if there were no faces/edges in any geometries,
493 * treat all the vertices as a point cloud. */
495 const Span<std::unique_ptr<Geometry>> all_geometries,
496 const GlobalVertices &global_vertices)
497{
498 if (!global_vertices.vertices.is_empty() && geom && geom->geom_type_ == GEOM_MESH) {
499 if (std::all_of(all_geometries.begin(),
500 all_geometries.end(),
501 [](const std::unique_ptr<Geometry> &g) { return g->get_vertex_count() == 0; }))
502 {
503 geom->track_all_vertices(global_vertices.vertices.size());
504 }
505 }
506}
507
508/* OBJ file format supports "line continuations", which
509 * are back-slashes, optionally followed by whitespace.
510 * The line virtually extends to the next line in that case. */
511StringRef OBJParser::read_next_obj_line(StringRef &buffer)
512{
513 const char *start = buffer.begin();
514 const char *end = buffer.end();
515 const char *ptr = start;
516
517 /* Scan until newline or backslash. */
518 while (ptr < end && *ptr != '\n' && *ptr != '\\') {
519 ++ptr;
520 }
521
522 /* Common case: no backslash found, return reference to input data. */
523 if (ptr >= end || *ptr == '\n') {
524 size_t len = ptr - start;
525 buffer = StringRef(ptr < end ? ptr + 1 : ptr, end);
526 return StringRef(start, len);
527 }
528
529 /* We have backslash. Copy into line buffer, replace
530 * line continuation with space, return result. */
531
532 line_buffer_.assign(start, ptr);
533
534 while (ptr < end) {
535 char c = *ptr++;
536 if (c == '\\') {
537 /* Scan ahead, skipping whitespace until newline. */
538 const char *ahead = ptr;
539 while (ahead < end && *ahead <= ' ' && *ahead != '\n') {
540 ++ahead;
541 }
542 if (ahead < end && *ahead == '\n') {
543 /* Line continuation: replace backslash & newline with space. */
544 line_buffer_ += ' ';
545 ptr = ahead + 1; /* Continue after the newline. */
546 }
547 else {
548 /* Not a continuation: keep the backslash. */
549 line_buffer_ += c;
550 }
551 }
552 else if (c == '\n') {
553 break;
554 }
555 else {
556 line_buffer_ += c;
557 }
558 }
559
560 buffer = StringRef(ptr, end);
561 return line_buffer_;
562}
563
564void OBJParser::parse_string_buffer(StringRef &buffer_str,
565 Vector<std::unique_ptr<Geometry>> &r_all_geometries,
566 GlobalVertices &r_global_vertices,
567 Geometry *&curr_geom)
568{
569 /* State variables: once set, they remain the same for the remaining
570 * elements in the object. */
571 bool state_shaded_smooth = false;
572 string state_group_name;
573 int state_group_index = -1;
574 string state_material_name;
575 int state_material_index = -1;
576
577 while (!buffer_str.is_empty()) {
578 StringRef line = read_next_obj_line(buffer_str);
579 const char *p = line.begin(), *end = line.end();
580 p = drop_whitespace(p, end);
581 if (p == end) {
582 continue;
583 }
584 /* Most common things that start with 'v': vertices, normals, UVs. */
585 if (*p == 'v') {
586 if (parse_keyword(p, end, "v")) {
587 geom_add_vertex(p, end, r_global_vertices);
588 }
589 else if (parse_keyword(p, end, "vn")) {
590 geom_add_vertex_normal(p, end, r_global_vertices);
591 }
592 else if (parse_keyword(p, end, "vt")) {
593 geom_add_uv_vertex(p, end, r_global_vertices);
594 }
595 }
596 /* Faces. */
597 else if (parse_keyword(p, end, "f")) {
598 /* If we don't have a material index assigned yet, get one.
599 * It means "usemtl" state came from the previous object. */
600 if (state_material_index == -1 && !state_material_name.empty() &&
601 curr_geom->material_indices_.is_empty())
602 {
603 curr_geom->material_indices_.add_new(state_material_name, 0);
604 curr_geom->material_order_.append(state_material_name);
605 state_material_index = 0;
606 }
607
608 geom_add_polygon(curr_geom,
609 p,
610 end,
611 r_global_vertices,
612 state_material_index,
613 state_group_index,
614 state_shaded_smooth);
615 }
616 /* Faces. */
617 else if (parse_keyword(p, end, "l")) {
618 geom_add_polyline(curr_geom, p, end, r_global_vertices);
619 }
620 /* Objects. */
621 else if (parse_keyword(p, end, "o")) {
622 if (import_params_.use_split_objects) {
624 end,
625 state_shaded_smooth,
626 state_group_name,
627 state_material_index,
628 curr_geom,
629 r_all_geometries);
630 }
631 }
632 /* Groups. */
633 else if (parse_keyword(p, end, "g")) {
634 if (import_params_.use_split_groups) {
636 end,
637 state_shaded_smooth,
638 state_group_name,
639 state_material_index,
640 curr_geom,
641 r_all_geometries);
642 }
643 else {
644 geom_update_group(StringRef(p, end).trim(), state_group_name);
645 int new_index = curr_geom->group_indices_.size();
646 state_group_index = curr_geom->group_indices_.lookup_or_add(state_group_name, new_index);
647 if (new_index == state_group_index) {
648 curr_geom->group_order_.append(state_group_name);
649 }
650 }
651 }
652 /* Smoothing groups. */
653 else if (parse_keyword(p, end, "s")) {
654 geom_update_smooth_group(p, end, state_shaded_smooth);
655 }
656 /* Materials and their libraries. */
657 else if (parse_keyword(p, end, "usemtl")) {
658 state_material_name = StringRef(p, end).trim();
659 int new_mat_index = curr_geom->material_indices_.size();
660 state_material_index = curr_geom->material_indices_.lookup_or_add(state_material_name,
661 new_mat_index);
662 if (new_mat_index == state_material_index) {
663 curr_geom->material_order_.append(state_material_name);
664 }
665 }
666 else if (parse_keyword(p, end, "mtllib")) {
667 add_mtl_library(StringRef(p, end).trim());
668 }
669 else if (parse_keyword(p, end, "#MRGB")) {
670 geom_add_mrgb_colors(p, end, r_global_vertices);
671 }
672 /* Comments. */
673 else if (*p == '#') {
674 /* Nothing to do. */
675 }
676 /* Curve related things. */
677 else if (parse_keyword(p, end, "cstype")) {
678 curr_geom = geom_set_curve_type(curr_geom, p, end, state_group_name, r_all_geometries);
679 }
680 else if (parse_keyword(p, end, "deg")) {
681 geom_set_curve_degree(curr_geom, p, end);
682 }
683 else if (parse_keyword(p, end, "curv")) {
684 geom_add_curve_vertex_indices(curr_geom, p, end, r_global_vertices);
685 }
686 else if (parse_keyword(p, end, "parm")) {
687 geom_add_curve_parameters(curr_geom, p, end);
688 }
689 else if (StringRef(p, end).startswith("end")) {
690 /* End of curve definition, nothing else to do. */
691 }
692 else {
693 CLOG_WARN(&LOG, "OBJ element not recognized: '%s'", string(p, end).c_str());
694 }
695 }
696}
697
698void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
699 GlobalVertices &r_global_vertices)
700{
701 if (!mmap_file_) {
702 return;
703 }
704
705 /* Use the filename as the default name given to the initial object. */
706 char ob_name[FILE_MAXFILE];
707 STRNCPY(ob_name, BLI_path_basename(import_params_.filepath));
709
710 Geometry *curr_geom = create_geometry(nullptr, GEOM_MESH, ob_name, r_all_geometries);
711
712 const char *file_data = static_cast<const char *>(BLI_mmap_get_pointer(mmap_file_));
713 size_t file_size = BLI_mmap_get_length(mmap_file_);
714 StringRef buffer_str{file_data, int64_t(file_size)};
715 OBJParser::parse_string_buffer(buffer_str, r_all_geometries, r_global_vertices, curr_geom);
716
717 r_global_vertices.flush_mrgb_block();
718 use_all_vertices_if_no_faces(curr_geom, r_all_geometries, r_global_vertices);
719 add_default_mtl_library();
720}
721
722static MTLTexMapType mtl_line_start_to_texture_type(const char *&p, const char *end)
723{
724 if (parse_keyword(p, end, "map_Kd")) {
726 }
727 if (parse_keyword(p, end, "map_Ks")) {
729 }
730 if (parse_keyword(p, end, "map_Ns")) {
732 }
733 if (parse_keyword(p, end, "map_d")) {
735 }
736 if (parse_keyword(p, end, "refl") || parse_keyword(p, end, "map_refl")) {
738 }
739 if (parse_keyword(p, end, "map_Ke")) {
741 }
742 if (parse_keyword(p, end, "bump") || parse_keyword(p, end, "map_Bump") ||
743 parse_keyword(p, end, "map_bump"))
744 {
746 }
747 if (parse_keyword(p, end, "map_Pr")) {
749 }
750 if (parse_keyword(p, end, "map_Pm")) {
752 }
753 if (parse_keyword(p, end, "map_Ps")) {
755 }
757}
758
759static const std::pair<StringRef, int> unsupported_texture_options[] = {
760 {"-blendu", 1},
761 {"-blendv", 1},
762 {"-boost", 1},
763 {"-cc", 1},
764 {"-clamp", 1},
765 {"-imfchan", 1},
766 {"-mm", 2},
767 {"-t", 3},
768 {"-texres", 1},
769};
770
771static bool parse_texture_option(const char *&p,
772 const char *end,
773 MTLMaterial *material,
774 MTLTexMap &tex_map)
775{
776 p = drop_whitespace(p, end);
777 if (parse_keyword(p, end, "-o")) {
778 p = parse_floats(p, end, 0.0f, tex_map.translation, 3, true);
779 return true;
780 }
781 if (parse_keyword(p, end, "-s")) {
782 p = parse_floats(p, end, 1.0f, tex_map.scale, 3, true);
783 return true;
784 }
785 if (parse_keyword(p, end, "-bm")) {
786 p = parse_float(p, end, 1.0f, material->normal_strength, true, true);
787 return true;
788 }
789 if (parse_keyword(p, end, "-type")) {
790 p = drop_whitespace(p, end);
791 /* Only sphere is supported. */
793 const StringRef line = StringRef(p, end);
794 if (!line.startswith("sphere")) {
795 CLOG_WARN(&LOG,
796 "Only the 'sphere' MTL projection type is supported, found: '%s'",
797 string(line).c_str());
798 }
799 p = drop_non_whitespace(p, end);
800 return true;
801 }
802 /* Check for unsupported options and skip them. */
803 for (const auto &opt : unsupported_texture_options) {
804 if (parse_keyword(p, end, opt.first)) {
805 /* Drop the arguments. */
806 for (int i = 0; i < opt.second; ++i) {
807 p = drop_whitespace(p, end);
808 p = drop_non_whitespace(p, end);
809 }
810 return true;
811 }
812 }
813
814 return false;
815}
816
817static void parse_texture_map(const char *p,
818 const char *end,
819 MTLMaterial *material,
820 const char *mtl_dir_path)
821{
822 const StringRef line = StringRef(p, end);
823 bool is_map = line.startswith("map_");
824 bool is_refl = line.startswith("refl");
825 bool is_bump = line.startswith("bump");
826 if (!is_map && !is_refl && !is_bump) {
827 return;
828 }
830 if (key == MTLTexMapType::Count) {
831 /* No supported texture map found. */
832 CLOG_WARN(&LOG, "MTL texture map type not supported: '%s'", string(line).c_str());
833 return;
834 }
835 MTLTexMap &tex_map = material->tex_map_of_type(key);
836 tex_map.mtl_dir_path = mtl_dir_path;
837
838 /* Parse texture map options. */
839 while (parse_texture_option(p, end, material, tex_map)) {
840 }
841
842 /* What remains is the image path. */
843 tex_map.image_path = StringRef(p, end).trim();
844}
845
847{
848 return mtl_libraries_;
849}
850
851void OBJParser::add_mtl_library(StringRef path)
852{
853 /* Remove any quotes from start and end (#67266, #97794). */
854 if (path.size() > 2 && path.startswith("\"") && path.endswith("\"")) {
855 path = path.drop_prefix(1).drop_suffix(1);
856 }
857
858 if (!mtl_libraries_.contains(path)) {
859 mtl_libraries_.append(path);
860 }
861}
862
863void OBJParser::add_default_mtl_library()
864{
865 /* Add any existing `.mtl` file that's with the same base name as the `.obj` file
866 * into candidate `.mtl` files to search through. This is not technically following the
867 * spec, but the old python importer was doing it, and there are user files out there
868 * that contain "mtllib bar.mtl" for a foo.obj, and depend on finding materials
869 * from foo.mtl (see #97757). */
870 char mtl_file_path[FILE_MAX];
871 STRNCPY(mtl_file_path, import_params_.filepath);
872 BLI_path_extension_replace(mtl_file_path, sizeof(mtl_file_path), ".mtl");
873 if (BLI_exists(mtl_file_path)) {
874 char mtl_file_base[FILE_MAX];
875 BLI_path_split_file_part(mtl_file_path, mtl_file_base, sizeof(mtl_file_base));
876 add_mtl_library(mtl_file_base);
877 }
878}
879
881{
882 char obj_file_dir[FILE_MAXDIR];
883 BLI_path_split_dir_part(obj_filepath.data(), obj_file_dir, FILE_MAXDIR);
884 BLI_path_join(mtl_file_path_, FILE_MAX, obj_file_dir, mtl_library.data());
885
886 /* Normalize the path to handle different paths pointing to the same file */
887 BLI_path_normalize(mtl_file_path_);
888
889 BLI_path_split_dir_part(mtl_file_path_, mtl_dir_path_, FILE_MAXDIR);
890}
891
892void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_materials)
893{
894 size_t buffer_len;
895 char *buffer = BLI_file_read_text_as_mem(mtl_file_path_, 0, &buffer_len);
896 if (buffer == nullptr) {
897 CLOG_ERROR(&LOG, "OBJ import: cannot read from MTL file: '%s'", mtl_file_path_);
898 return;
899 }
900
901 MTLMaterial *material = nullptr;
902
903 StringRef buffer_str{buffer, int64_t(buffer_len)};
904 while (!buffer_str.is_empty()) {
905 const StringRef line = read_next_line(buffer_str);
906 const char *p = line.begin(), *end = line.end();
907 p = drop_whitespace(p, end);
908 if (p == end) {
909 continue;
910 }
911
912 if (parse_keyword(p, end, "newmtl")) {
913 StringRef mat_name = StringRef(p, end).trim();
914 /* Always try to get or create the material, even if it already exists */
915 material =
916 r_materials.lookup_or_add(string(mat_name), std::make_unique<MTLMaterial>()).get();
917 }
918 else if (material != nullptr) {
919 if (parse_keyword(p, end, "Ns")) {
920 parse_float(p, end, 324.0f, material->spec_exponent);
921 }
922 else if (parse_keyword(p, end, "Ka")) {
923 parse_floats(p, end, 0.0f, material->ambient_color, 3);
924 }
925 else if (parse_keyword(p, end, "Kd")) {
926 parse_floats(p, end, 0.8f, material->color, 3);
927 }
928 else if (parse_keyword(p, end, "Ks")) {
929 parse_floats(p, end, 0.5f, material->spec_color, 3);
930 }
931 else if (parse_keyword(p, end, "Ke")) {
932 parse_floats(p, end, 0.0f, material->emission_color, 3);
933 }
934 else if (parse_keyword(p, end, "Ni")) {
935 parse_float(p, end, 1.45f, material->ior);
936 }
937 else if (parse_keyword(p, end, "d")) {
938 parse_float(p, end, 1.0f, material->alpha);
939 }
940 else if (parse_keyword(p, end, "illum")) {
941 /* Some files incorrectly use a float (#60135). */
942 float val;
943 parse_float(p, end, 1.0f, val);
944 material->illum_mode = val;
945 }
946 else if (parse_keyword(p, end, "Pr")) {
947 parse_float(p, end, 0.5f, material->roughness);
948 }
949 else if (parse_keyword(p, end, "Pm")) {
950 parse_float(p, end, 0.0f, material->metallic);
951 }
952 else if (parse_keyword(p, end, "Ps")) {
953 parse_float(p, end, 0.0f, material->sheen);
954 }
955 else if (parse_keyword(p, end, "Pc")) {
956 parse_float(p, end, 0.0f, material->cc_thickness);
957 }
958 else if (parse_keyword(p, end, "Pcr")) {
959 parse_float(p, end, 0.0f, material->cc_roughness);
960 }
961 else if (parse_keyword(p, end, "aniso")) {
962 parse_float(p, end, 0.0f, material->aniso);
963 }
964 else if (parse_keyword(p, end, "anisor")) {
965 parse_float(p, end, 0.0f, material->aniso_rot);
966 }
967 else if (parse_keyword(p, end, "Kt") || parse_keyword(p, end, "Tf")) {
968 parse_floats(p, end, 0.0f, material->transmit_color, 3);
969 }
970 else {
971 parse_texture_map(p, end, material, mtl_dir_path_);
972 }
973 }
974 }
975
976 MEM_delete(buffer);
977}
978} // namespace io::obj
979} // namespace blender
#define INT32_MAX
File and directory operations.
#define O_BINARY
#define FILE_MAXFILE
#define FILE_MAX
#define BLI_path_join(...)
#define FILE_MAXDIR
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define UNUSED_VARS(...)
#define ELEM(...)
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
constexpr StringRef()
void append(const T &value)
constexpr StringRef trim() const
constexpr const char * end() const
constexpr bool startswith(StringRef prefix) const
constexpr const char * begin() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr int64_t find(char c, int64_t pos=0) const
constexpr const char * data() const
constexpr bool endswith(StringRef suffix) const
void parse_and_store(Map< std::string, std::unique_ptr< MTLMaterial > > &r_materials)
MTLParser(StringRefNull mtl_library_, StringRefNull obj_filepath)
OBJParser(const OBJImportParams &import_params)
Span< std::string > mtl_libraries() const
void parse(Vector< std::unique_ptr< Geometry > > &r_all_geometries, GlobalVertices &r_global_vertices)
#define FLT_MAX
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
static void use_all_vertices_if_no_faces(Geometry *geom, const Span< std::unique_ptr< Geometry > > all_geometries, const GlobalVertices &global_vertices)
static void geom_add_curve_vertex_indices(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices)
static void geom_add_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_mrgb_colors(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void parse_texture_map(const char *p, const char *end, MTLMaterial *material, const char *mtl_dir_path)
static void geom_add_uv_vertex(const char *p, const char *end, GlobalVertices &r_global_vertices)
static const char * parse_vertex_index(const char *p, const char *end, size_t n_elems, int &r_index)
static MTLTexMapType mtl_line_start_to_texture_type(const char *&p, const char *end)
static void geom_add_polyline(Geometry *geom, const char *p, const char *end, GlobalVertices &r_global_vertices)
static bool parse_keyword(const char *&p, const char *end, StringRef keyword)
static bool parse_texture_option(const char *&p, const char *end, MTLMaterial *material, MTLTexMap &tex_map)
static void geom_add_vertex_normal(const char *p, const char *end, GlobalVertices &r_global_vertices)
static void geom_add_curve_parameters(Geometry *geom, const char *p, const char *end)
static void geom_set_curve_degree(Geometry *geom, const char *p, const char *end)
static void geom_update_smooth_group(const char *p, const char *end, bool &r_state_shaded_smooth)
static void geom_new_object(const char *p, const char *end, bool &r_state_shaded_smooth, string &r_state_group_name, int &r_state_material_index, Geometry *&r_curr_geom, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static void geom_update_group(const StringRef rest_line, string &r_group_name)
static Geometry * geom_set_curve_type(Geometry *geom, const char *p, const char *end, const StringRef group_name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
static const std::pair< StringRef, int > unsupported_texture_options[]
static void geom_add_polygon(Geometry *geom, const char *p, const char *end, const GlobalVertices &global_vertices, const int material_index, const int group_index, const bool shaded_smooth)
static Geometry * create_geometry(Geometry *const prev_geometry, const eGeometryType new_type, StringRef name, Vector< std::unique_ptr< Geometry > > &r_all_geometries)
bool index_in_range(const int64_t index, const int64_t size)
const char * parse_float(const char *p, const char *end, float fallback, float &dst, bool skip_space, bool require_trailing_space)
const char * parse_floats(const char *p, const char *end, float fallback, float *dst, int count, bool require_trailing_space)
const char * drop_non_whitespace(const char *p, const char *end)
StringRef read_next_line(StringRef &buffer)
const char * parse_int(const char *p, const char *end, int fallback, int &dst, bool skip_space)
const char * drop_whitespace(const char *p, const char *end)
BMesh Iterators.
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
size_t BLI_mmap_get_length(const BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:484
BLI_mmap_file * BLI_mmap_open(int fd) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:369
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
void srgb_to_linearrgb_v3_v3(float linear[3], const float srgb[3])
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
int BLI_open(const char *filepath, int oflag, int pmode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void BLI_mmap_free(BLI_mmap_file *file) ATTR_NONNULL(1)
Definition BLI_mmap.cc:494
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
char * BLI_file_read_text_as_mem(const char *filepath, size_t pad_bytes, size_t *r_size)
Definition storage.cc:539
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
VecBase< float, 2 > float2
unsigned char uchar
Definition BKE_text.h:58
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
MINLINE float normalize_v3(float n[3])
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void * BLI_mmap_get_pointer(BLI_mmap_file *file) ATTR_WARN_UNUSED_RESULT
Definition BLI_mmap.cc:479
static CLG_LogRef LOG
Definition logging.cpp:12
VecBase< float, 3 > float3
Vector< FaceCorner > face_corners_
void set_vertex_weight(int64_t index, float weight)
void set_vertex_color(int64_t index, float3 color)
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const