[Bf-blender-cvs] [c815ba0a3c0] soc-2020-io-performance: Add a public member function in MaterialWrap.

Ankit Meel noreply at git.blender.org
Mon Aug 17 09:02:28 CEST 2020


Commit: c815ba0a3c06cb7c3e4a52de62df9641569a3912
Author: Ankit Meel
Date:   Mon Aug 17 12:15:33 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBc815ba0a3c06cb7c3e4a52de62df9641569a3912

Add a public member function in MaterialWrap.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
index 102a655615e..9d5b59d2fa8 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.cc
@@ -421,26 +421,37 @@ void OBJWriter::update_index_offsets(const OBJMesh &obj_mesh_data)
   index_offset_[NORMAL_OFF] += obj_mesh_data.tot_polygons();
 }
 
+/**
+ * Open the MTL file in append mode.
+ */
 MTLWriter::MTLWriter(const char *obj_filepath)
 {
-  BLI_strncpy(mtl_filepath_, obj_filepath, FILE_MAX);
-  BLI_path_extension_replace(mtl_filepath_, FILE_MAX, ".mtl");
+  char mtl_filepath[FILE_MAX];
+  BLI_strncpy(mtl_filepath, obj_filepath, FILE_MAX);
+  BLI_path_extension_replace(mtl_filepath, FILE_MAX, ".mtl");
+  mtl_outfile_ = fopen(mtl_filepath, "a");
+  if (!mtl_outfile_) {
+    fprintf(stderr, "Error in opening file at %s\n", mtl_filepath);
+    return;
+  }
 }
 
 MTLWriter::~MTLWriter()
 {
-  fclose(mtl_outfile_);
+  if (mtl_outfile_) {
+    fclose(mtl_outfile_);
+  }
 }
 
 void MTLWriter::append_materials(const OBJMesh &mesh_to_export)
 {
-  mtl_outfile_ = fopen(mtl_filepath_, "a");
   if (!mtl_outfile_) {
-    fprintf(stderr, "Error in opening file at %s\n", mtl_filepath_);
+    /* Error logging in constructor. */
     return;
   }
   Vector<MTLMaterial> mtl_materials;
   MaterialWrap mat_wrap(mesh_to_export, mtl_materials);
+  mat_wrap.fill_materials();
 
   for (const MTLMaterial &mtl_material : mtl_materials) {
     fprintf(mtl_outfile_, "\nnewmtl %s\n", mtl_material.name.c_str());
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
index c0ed02c9f37..663364e432f 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_file_writer.hh
@@ -105,7 +105,6 @@ class OBJWriter {
 class MTLWriter {
  private:
   FILE *mtl_outfile_;
-  char mtl_filepath_[FILE_MAX];
 
  public:
   MTLWriter(const char *obj_filepath);
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
index 3b83ad39e19..895fe38ed76 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
@@ -184,6 +184,9 @@ void MaterialWrap::init_bsdf_node(StringRefNull object_name)
   bsdf_node_ = nullptr;
 }
 
+/**
+ * Store properties found either in p-BSDF node or `Material` of the object. 
+ */
 void MaterialWrap::store_bsdf_properties(MTLMaterial &r_mtl_mat) const
 {
   /* Empirical, and copied from original python exporter. */
@@ -236,6 +239,9 @@ void MaterialWrap::store_bsdf_properties(MTLMaterial &r_mtl_mat) const
   r_mtl_mat.illum = illum;
 }
 
+/**
+ * Store image texture options and filepaths.
+ */
 void MaterialWrap::store_image_textures(MTLMaterial &r_mtl_mat) const
 {
   /* Need to create a NodeTreeRef for a faster way to find linked sockets, as opposed to
@@ -300,20 +306,28 @@ void MaterialWrap::store_image_textures(MTLMaterial &r_mtl_mat) const
 }
 
 /**
- * Append an object's materials to the .mtl file.
+ * Fill the given buffer with MTL material containers.
  */
-MaterialWrap::MaterialWrap(const OBJMesh &obj_mesh_data, Vector<MTLMaterial> &r_mtl_materials)
+void MaterialWrap::fill_materials()
 {
-  r_mtl_materials.resize(obj_mesh_data.tot_col());
-  for (short i = 0; i < obj_mesh_data.tot_col(); i++) {
-    export_mtl_ = obj_mesh_data.get_object_material(i + 1);
+  for (short i = 0; i < obj_mesh_data_.tot_col(); i++) {
+    export_mtl_ = obj_mesh_data_.get_object_material(i + 1);
     if (!export_mtl_) {
       continue;
     }
-    r_mtl_materials[i].name = obj_mesh_data.get_object_material_name(i + 1);
-    init_bsdf_node(obj_mesh_data.get_object_name());
-    store_bsdf_properties(r_mtl_materials[i]);
-    store_image_textures(r_mtl_materials[i]);
+    r_mtl_materials_[i].name = obj_mesh_data_.get_object_material_name(i + 1);
+    init_bsdf_node(obj_mesh_data_.get_object_name());
+    store_bsdf_properties(r_mtl_materials_[i]);
+    store_image_textures(r_mtl_materials_[i]);
   }
 }
+
+/**
+ * Append an object's materials to the .mtl file.
+ */
+MaterialWrap::MaterialWrap(const OBJMesh &obj_mesh_data, Vector<MTLMaterial> &r_mtl_materials)
+    : obj_mesh_data_(obj_mesh_data), r_mtl_materials_(r_mtl_materials)
+{
+  r_mtl_materials.resize(obj_mesh_data.tot_col());
+}
 }  // namespace blender::io::obj
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh
index 2b18f820fcc..24b6d727758 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.hh
@@ -28,6 +28,8 @@
 namespace blender::io::obj {
 class MaterialWrap {
  private:
+  const OBJMesh &obj_mesh_data_;
+  Vector<MTLMaterial> &r_mtl_materials_;
   /**
    * One of the object's materials, to be exported.
    */
@@ -39,6 +41,7 @@ class MaterialWrap {
 
  public:
   MaterialWrap(const OBJMesh &obj_mesh_data, Vector<MTLMaterial> &r_mtl_materials);
+  void fill_materials();
 
  private:
   void init_bsdf_node(StringRefNull object_name);



More information about the Bf-blender-cvs mailing list