[Bf-blender-cvs] [e1e9c838899] blender-v3.3-release: Fix T101685: OBJ importer does not assign proper material if "usemtl" is before "o"

Aras Pranckevicius noreply at git.blender.org
Wed Oct 26 10:55:06 CEST 2022


Commit: e1e9c838899dd1a8f55a5c67b2d5716a6a5b88a6
Author: Aras Pranckevicius
Date:   Sun Oct 9 21:21:31 2022 +0300
Branches: blender-v3.3-release
https://developer.blender.org/rBe1e9c838899dd1a8f55a5c67b2d5716a6a5b88a6

Fix T101685: OBJ importer does not assign proper material if "usemtl" is before "o"

The importer logic was wrongly resetting "current material name"
upon encountering a new object ("o" command). However as per OBJ
specification, this is incorrect:

> Specifies the material name for the element following it. Once a
> material is assigned, it cannot be turned off; it can only be
> changed.

Fixes T101685. Test coverage for this was added in svn tests repo.

===================================================================

M	source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc

===================================================================

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 7069e1185e0..78f34eab228 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
@@ -507,6 +507,15 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
       }
       /* Faces. */
       else if (parse_keyword(p, end, "f")) {
+        /* If we don't have a material index assigned yet, get one.
+         * It means "usemtl" state came from the previous object. */
+        if (state_material_index == -1 && !state_material_name.empty() &&
+            curr_geom->material_indices_.is_empty()) {
+          curr_geom->material_indices_.add_new(state_material_name, 0);
+          curr_geom->material_order_.append(state_material_name);
+          state_material_index = 0;
+        }
+
         geom_add_polygon(curr_geom,
                          p,
                          end,
@@ -523,7 +532,10 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries,
       else if (parse_keyword(p, end, "o")) {
         state_shaded_smooth = false;
         state_group_name = "";
-        state_material_name = "";
+        /* Reset object-local material index that's used in face infos.
+         * Note: do not reset the material name; that has to carry over
+         * into the next object if needed. */
+        state_material_index = -1;
         curr_geom = create_geometry(
             curr_geom, GEOM_MESH, StringRef(p, end).trim(), r_all_geometries);
       }



More information about the Bf-blender-cvs mailing list