[Bf-blender-cvs] [b9123b806fc] master: Fix T96470 new obj exporter writing material groups

Aras Pranckevicius noreply at git.blender.org
Sun Mar 20 14:01:19 CET 2022


Commit: b9123b806fc4d17d684c0cc7057c0d3c7136d067
Author: Aras Pranckevicius
Date:   Sun Mar 20 08:59:16 2022 -0400
Branches: master
https://developer.blender.org/rBb9123b806fc4d17d684c0cc7057c0d3c7136d067

Fix T96470 new obj exporter writing material groups

This is patch D14349 from Aras Pranckevicius.

The logic in the code was _completely different_ from the documentation
and what the python exporter in 3.0 did. The new code assumed that
"export material groups" meant "append material name to the object name",
and was only ever kicking in when the "export object groups" option was
also checked. But the proper behavior (as in 3.0 exporter & the online docs),
is to emit g objectname_materialname before each usemtl line. Which is something entirely else.

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

M	source/blender/editors/io/io_obj.c
M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
M	source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
M	source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc

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

diff --git a/source/blender/editors/io/io_obj.c b/source/blender/editors/io/io_obj.c
index 8e380e3f2bc..df15191916a 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -342,7 +342,7 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
                   "export_material_groups",
                   false,
                   "Export Material Groups",
-                  "Append mesh name and material name to object name, separated by a '_'");
+                  "Generate an OBJ group for each part of a geometry using a different material");
   RNA_def_boolean(
       ot->srna,
       "export_vertex_groups",
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
index 1668ce3269c..3f9fc3b1dbb 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc
@@ -174,34 +174,14 @@ void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
   fh.write_to_file(outfile_);
 }
 
-void OBJWriter::write_object_group(FormatHandler<eFileType::OBJ> &fh,
-                                   const OBJMesh &obj_mesh_data) const
-{
-  /* "o object_name" is not mandatory. A valid .OBJ file may contain neither
-   * "o name" nor "g group_name". */
-  BLI_assert(export_params_.export_object_groups);
-  if (!export_params_.export_object_groups) {
-    return;
-  }
-  const std::string object_name = obj_mesh_data.get_object_name();
-  const char *object_mesh_name = obj_mesh_data.get_object_mesh_name();
-  const char *object_material_name = obj_mesh_data.get_object_material_name(0);
-  if (export_params_.export_materials && export_params_.export_material_groups &&
-      object_material_name) {
-    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name + "_" +
-                                              object_material_name);
-  }
-  else {
-    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name);
-  }
-}
-
 void OBJWriter::write_object_name(FormatHandler<eFileType::OBJ> &fh,
                                   const OBJMesh &obj_mesh_data) const
 {
   const char *object_name = obj_mesh_data.get_object_name();
   if (export_params_.export_object_groups) {
-    write_object_group(fh, obj_mesh_data);
+    const std::string object_name = obj_mesh_data.get_object_name();
+    const char *mesh_name = obj_mesh_data.get_object_mesh_name();
+    fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + mesh_name);
     return;
   }
   fh.write<eOBJSyntaxElement::object_name>(object_name);
@@ -363,13 +343,14 @@ void OBJWriter::write_poly_elements(FormatHandler<eFileType::OBJ> &fh,
           buf.write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
         }
         else {
-          if (export_params_.export_object_groups) {
-            write_object_group(buf, obj_mesh_data);
-          }
           const char *mat_name = matname_fn(mat);
           if (!mat_name) {
             mat_name = MATERIAL_GROUP_DISABLED;
           }
+          if (export_params_.export_material_groups) {
+            const std::string object_name = obj_mesh_data.get_object_name();
+            fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + mat_name);
+          }
           buf.write<eOBJSyntaxElement::poly_usemtl>(mat_name);
         }
       }
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
index 50de8ad3282..96f7d434338 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.hh
@@ -66,10 +66,6 @@ class OBJWriter : NonMovable, NonCopyable {
    * Write object's name or group.
    */
   void write_object_name(FormatHandler<eFileType::OBJ> &fh, const OBJMesh &obj_mesh_data) const;
-  /**
-   * Write an object's group with mesh and/or material name appended conditionally.
-   */
-  void write_object_group(FormatHandler<eFileType::OBJ> &fh, const OBJMesh &obj_mesh_data) const;
   /**
    * Write file name of Material Library in .OBJ file.
    */
diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
index 5be68565054..7d3e3cd84fd 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
@@ -203,11 +203,6 @@ const Material *OBJMesh::get_object_material(const int16_t mat_nr) const
    */
   Object *obj = const_cast<Object *>(&export_object_eval_);
   const Material *r_mat = BKE_object_material_get(obj, mat_nr + 1);
-#ifdef DEBUG
-  if (!r_mat) {
-    std::cerr << "Material not found for mat_nr = " << mat_nr << std::endl;
-  }
-#endif
   return r_mat;
 }
 
diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
index e96a7d28561..b8fecfb25f3 100644
--- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
+++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
@@ -511,4 +511,17 @@ TEST_F(obj_exporter_regression_test, all_objects)
                                _export.params);
 }
 
+TEST_F(obj_exporter_regression_test, all_objects_mat_groups)
+{
+  OBJExportParamsDefault _export;
+  _export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
+  _export.params.up_axis = OBJ_AXIS_Z_UP;
+  _export.params.export_smooth_groups = true;
+  _export.params.export_material_groups = true;
+  compare_obj_export_to_golden("io_tests/blend_scene/all_objects.blend",
+                               "io_tests/obj/all_objects_mat_groups.obj",
+                               "io_tests/obj/all_objects_mat_groups.mtl",
+                               _export.params);
+}
+
 }  // namespace blender::io::obj



More information about the Bf-blender-cvs mailing list