[Bf-blender-cvs] [1781c6002ae] blender-v3.1-release: Fix T96470 new obj exporter writing material groups

Aras Pranckevicius noreply at git.blender.org
Wed Mar 23 12:23:11 CET 2022


Commit: 1781c6002ae935af9c611dc90e12a56faaa6ed68
Author: Aras Pranckevicius
Date:   Sun Mar 20 08:59:16 2022 -0400
Branches: blender-v3.1-release
https://developer.blender.org/rB1781c6002ae935af9c611dc90e12a56faaa6ed68

Fix T96470 new obj exporter writing material groups

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.

Cherry picked from b9123b806fc4 (D14349) with minor conflict fixes.

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

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 3262397bf63..428ace6cfe5 100644
--- a/source/blender/editors/io/io_obj.c
+++ b/source/blender/editors/io/io_obj.c
@@ -359,7 +359,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 4eea2e51521..302b20783c2 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
@@ -178,31 +178,13 @@ void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
   file_handler_->write<eOBJSyntaxElement::mtllib>(mtl_file_name);
 }
 
-void OBJWriter::write_object_group(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) {
-    file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name +
-                                                          "_" + object_material_name);
-    return;
-  }
-  file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name);
-}
-
 void OBJWriter::write_object_name(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(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();
+    file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + mesh_name);
     return;
   }
   file_handler_->write<eOBJSyntaxElement::object_name>(object_name);
@@ -278,13 +260,14 @@ int16_t OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
     file_handler_->write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
     return current_mat_nr;
   }
-  if (export_params_.export_object_groups) {
-    write_object_group(obj_mesh_data);
-  }
   const char *mat_name = matname_fn(current_mat_nr);
   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();
+    file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + mat_name);
+  }
   file_handler_->write<eOBJSyntaxElement::poly_usemtl>(mat_name);
 
   return current_mat_nr;
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 1bbdb7a848c..b68ca5f5fe0 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
@@ -65,10 +65,6 @@ class OBJWriter : NonMovable, NonCopyable {
    * Write object's name or group.
    */
   void write_object_name(const OBJMesh &obj_mesh_data) const;
-  /**
-   * Write an object's group with mesh and/or material name appended conditionally.
-   */
-  void write_object_group(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 2670272f4b7..fd831aa5674 100644
--- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc
@@ -205,11 +205,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 07b799b80b3..7c10ae5f539 100644
--- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
+++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc
@@ -479,4 +479,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