[Bf-blender-cvs] [ded6377ea0c] soc-2020-io-performance: Write empty usemtl if a polygon has no material.

Ankit Meel noreply at git.blender.org
Mon Sep 21 13:35:39 CEST 2020


Commit: ded6377ea0ce31601695b623ec33407dc2f3b859
Author: Ankit Meel
Date:   Wed Sep 16 17:56:35 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBded6377ea0ce31601695b623ec33407dc2f3b859

Write empty usemtl if a polygon has no material.

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

M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_mesh.cc

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

diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
index c8fbe7aa71f..03a20a2fda8 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
@@ -247,6 +247,14 @@ void OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
   if (r_last_face_mat_nr == curr_mat_nr) {
     return;
   }
+  r_last_face_mat_nr = curr_mat_nr;
+  if (curr_mat_nr == NOT_FOUND) {
+    /* Once a material is assigned, it cannot be turned off; it can only be changed.
+     * If a material name is not specified, a white material is used.
+     * http://www.martinreddy.net/gfx/3d/OBJ.spec */
+    fprintf(outfile_, "usemtl\n");
+    return;
+  }
   const char *mat_name = obj_mesh_data.get_object_material_name(curr_mat_nr);
   if (export_params_.export_material_groups) {
     const char *object_name = obj_mesh_data.get_object_name();
@@ -254,7 +262,6 @@ void OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
     fprintf(outfile_, "g %s_%s_%s\n", object_name, object_mesh_name, mat_name);
   }
   fprintf(outfile_, "usemtl %s\n", mat_name);
-  r_last_face_mat_nr = curr_mat_nr;
 }
 
 /**
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
index f30b0a7a3b5..353066b59cf 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -240,10 +240,15 @@ bool OBJMesh::is_ith_poly_smooth(const uint poly_index) const
   return export_mesh_eval_->mpoly[poly_index].flag & ME_SMOOTH;
 }
 
+/**
+ * Returns a zero-based index of a polygon's material indexing into
+ * the Object's material slots.
+ */
 short OBJMesh::ith_poly_matnr(const uint poly_index) const
 {
   BLI_assert(poly_index < export_mesh_eval_->totpoly);
-  return export_mesh_eval_->mpoly[poly_index].mat_nr;
+  const short r_mat_nr = export_mesh_eval_->mpoly[poly_index].mat_nr;
+  return r_mat_nr > 0 ? r_mat_nr : NOT_FOUND;
 }
 
 int OBJMesh::ith_poly_totloop(const uint poly_index) const



More information about the Bf-blender-cvs mailing list