diff options
Diffstat (limited to 'source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc')
-rw-r--r-- | source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc index 56806449733..3724eaaa361 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc @@ -461,7 +461,7 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, } } else if (parse_keyword(line, "mtllib")) { - mtl_libraries_.append(line.trim()); + add_mtl_library(line.trim()); } /* Comments. */ else if (line.startswith("#")) { @@ -498,6 +498,8 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, memmove(buffer.data(), buffer.data() + last_nl, left_size); buffer_offset = left_size; } + + add_default_mtl_library(); } static eMTLSyntaxElement mtl_line_start_to_enum(StringRef &line) @@ -616,6 +618,30 @@ Span<std::string> OBJParser::mtl_libraries() const return mtl_libraries_; } +void OBJParser::add_mtl_library(const std::string &path) +{ + if (!mtl_libraries_.contains(path)) { + mtl_libraries_.append(path); + } +} + +void OBJParser::add_default_mtl_library() +{ + /* Add any existing .mtl file that's with the same base name as the .obj file + * into candidate .mtl files to search through. This is not technically following the + * spec, but the old python importer was doing it, and there are user files out there + * that contain "mtllib bar.mtl" for a foo.obj, and depend on finding materials + * from foo.mtl (see T97757). */ + char mtl_file_path[FILE_MAX]; + BLI_strncpy(mtl_file_path, import_params_.filepath, sizeof(mtl_file_path)); + BLI_path_extension_replace(mtl_file_path, sizeof(mtl_file_path), ".mtl"); + if (BLI_exists(mtl_file_path)) { + char mtl_file_base[FILE_MAX]; + BLI_split_file_part(mtl_file_path, mtl_file_base, sizeof(mtl_file_base)); + add_mtl_library(mtl_file_base); + } +} + MTLParser::MTLParser(StringRef mtl_library, StringRefNull obj_filepath) { char obj_file_dir[FILE_MAXDIR]; @@ -645,11 +671,12 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mat if (parse_keyword(line, "newmtl")) { line = line.trim(); - if (r_materials.remove_as(line)) { - std::cerr << "Duplicate material found:'" << line - << "', using the last encountered Material definition." << std::endl; + if (r_materials.contains(line)) { + material = nullptr; + } + else { + material = r_materials.lookup_or_add(string(line), std::make_unique<MTLMaterial>()).get(); } - material = r_materials.lookup_or_add(string(line), std::make_unique<MTLMaterial>()).get(); } else if (material != nullptr) { if (parse_keyword(line, "Ns")) { @@ -674,7 +701,10 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mat parse_float(line, 1.0f, material->d); } else if (parse_keyword(line, "illum")) { - parse_int(line, 2, material->illum); + /* Some files incorrectly use a float (T60135). */ + float val; + parse_float(line, 1.0f, val); + material->illum = val; } else { parse_texture_map(line, material, mtl_dir_path_); |