[Bf-blender-cvs] [51179482857] soc-2020-io-performance: Don't write default scale and translation.

Ankit Meel noreply at git.blender.org
Sat Sep 5 14:14:37 CEST 2020


Commit: 5117948285721a224d91a35d1ec861f9d897d189
Author: Ankit Meel
Date:   Fri Sep 4 16:56:07 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB5117948285721a224d91a35d1ec861f9d897d189

Don't write default scale and translation.

Also fix debug warning of material not found.

Add asserts to catch unchanged values.

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

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 aa8f84bc573..424ce9e8d80 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
@@ -28,6 +28,7 @@
 #include "obj_export_mtl.hh"
 #include "obj_export_nurbs.hh"
 #include "obj_import_mtl.hh"
+#include "string_utils.hh"
 
 namespace blender::io::obj {
 
@@ -430,6 +431,10 @@ void OBJWriter::update_index_offsets(const OBJMesh &obj_mesh_data)
   tot_normals_ = 0;
 }
 
+/* -------------------------------------------------------------------- */
+/** \name MTL writers.
+ * \{ */
+
 /**
  * Open the MTL file in append mode.
  */
@@ -460,57 +465,64 @@ void MTLWriter::append_materials(const OBJMesh &mesh_to_export)
   MaterialWrap mat_wrap(mesh_to_export, mtl_materials);
   mat_wrap.fill_materials();
 
+  auto all_items_positive = [](const float3 &triplet) {
+    return triplet.x >= 0.0f && triplet.y >= 0.0f && triplet.z >= 0.0f;
+  };
+
   for (const MTLMaterial &mtl_material : mtl_materials) {
     fprintf(mtl_outfile_, "\nnewmtl %s\n", mtl_material.name.c_str());
-    fprintf(mtl_outfile_, "Ns %.6f\n", mtl_material.Ns);
-    fprintf(mtl_outfile_,
-            "Ka %0.6f %0.6f %0.6f\n",
-            mtl_material.Ka[0],
-            mtl_material.Ka[1],
-            mtl_material.Ka[2]);
-    fprintf(mtl_outfile_,
-            "Kd %0.6f %0.6f %0.6f\n",
-            mtl_material.Kd[0],
-            mtl_material.Kd[1],
-            mtl_material.Kd[2]);
-    fprintf(mtl_outfile_,
-            "Ks %0.6f %0.6f %0.6f\n",
-            mtl_material.Ks[0],
-            mtl_material.Ks[0],
-            mtl_material.Ks[0]);
-    fprintf(mtl_outfile_,
-            "Ke %0.6f %0.6f %0.6f\n",
-            mtl_material.Ke[0],
-            mtl_material.Ke[1],
-            mtl_material.Ke[2]);
+    BLI_assert(all_items_positive({mtl_material.d, mtl_material.Ns, mtl_material.Ni}) &&
+               mtl_material.illum > 0);
+    BLI_assert(all_items_positive(mtl_material.Ka) && all_items_positive(mtl_material.Kd) &&
+               all_items_positive(mtl_material.Ks) && all_items_positive(mtl_material.Ke));
+
     fprintf(mtl_outfile_,
-            "Ni %0.6f\nd %.6f\nillum %d\n",
+            "Ni %0.6f\n"
+            "d %.6f\n"
+            "Ns %0.6f\n"
+            "illum %d\n",
             mtl_material.Ni,
             mtl_material.d,
+            mtl_material.Ns,
             mtl_material.illum);
+    fprintf(mtl_outfile_, "Ka %s\n", float3_to_string(mtl_material.Ka).c_str());
+    fprintf(mtl_outfile_, "Kd %s\n", float3_to_string(mtl_material.Kd).c_str());
+    fprintf(mtl_outfile_, "Ks %s\n", float3_to_string(mtl_material.Ks).c_str());
+    fprintf(mtl_outfile_, "Ke %s\n", float3_to_string(mtl_material.Ke).c_str());
+
+    /* Write image texture maps. */
     for (const Map<const std::string, tex_map_XX>::Item &texture_map :
          mtl_material.texture_maps.items()) {
       if (texture_map.value.image_path.empty()) {
         continue;
       }
+
       std::string map_bump_strength;
+      std::string scale;
+      std::string translation;
+      /* Texture map keys should have leading spaces. */
       if (texture_map.key == "map_Bump" && mtl_material.map_Bump_strength > 0.0001f) {
-        map_bump_strength = " -bm " + std::to_string(mtl_material.map_Bump_strength);
+        map_bump_strength.append(" -bm ").append(std::to_string(mtl_material.map_Bump_strength));
+      }
+      if (texture_map.value.scale != float3{1.0f, 1.0f, 1.0f}) {
+        scale.append(" -o ").append(float3_to_string(texture_map.value.scale));
+      }
+      if (texture_map.value.translation != float3{0.0f, 0.0f, 0.0f}) {
+        translation.append(" -s ").append(float3_to_string(texture_map.value.translation));
       }
-      /* Always keep only one space between options since filepaths may have leading spaces too.
-       * map_Bump string has its leading space. */
+
+      /* Always keep only one space between options since filepaths may have leading spaces too. */
       fprintf(mtl_outfile_,
-              "%s -o %.6f %.6f %.6f -s %.6f %.6f %.6f%s %s\n",
+              "%s%s%s%s %s\n",
               texture_map.key.c_str(),
-              texture_map.value.translation[0],
-              texture_map.value.translation[1],
-              texture_map.value.translation[2],
-              texture_map.value.scale[0],
-              texture_map.value.scale[1],
-              texture_map.value.scale[2],
+              translation.c_str(),       /* Can be empty. */
+              scale.c_str(),             /* Can be empty. */
               map_bump_strength.c_str(), /* Can be empty. */
               texture_map.value.image_path.c_str());
     }
   }
 }
+
+/** \} */
+
 }  // namespace blender::io::obj
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 d98e38a0c3c..1eb7ab614bf 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_export_mesh.cc
@@ -279,7 +279,9 @@ const char *OBJMesh::get_object_material_name(short mat_nr) const
 {
   const Material *mat = BKE_object_material_get(export_object_eval_, mat_nr);
 #ifdef DEBUG
-  std::cerr << "Material not found for mat_nr = " << mat_nr << std::endl;
+  if (!mat) {
+    std::cerr << "Material not found for mat_nr = " << mat_nr << std::endl;
+  }
 #endif
   return mat ? mat->id.name + 2 : nullptr;
 }



More information about the Bf-blender-cvs mailing list