[Bf-blender-cvs] [047189baf03] soc-2020-io-performance: Create MTLWriter object early, add methods for filename and file status.

Ankit Meel noreply at git.blender.org
Wed Sep 16 13:05:54 CEST 2020


Commit: 047189baf03010fe0ba7567b93a8159e4efd9d07
Author: Ankit Meel
Date:   Wed Sep 16 12:09:07 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB047189baf03010fe0ba7567b93a8159e4efd9d07

Create MTLWriter object early, add methods for filename and file status.

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

M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.cc
M	source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
M	source/blender/io/wavefront_obj/intern/obj_exporter.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 1cd414316ee..d42ce309679 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
@@ -121,23 +121,9 @@ bool OBJWriter::init_writer(const char *filepath)
 
 /**
  * Write file name of Material Library in OBJ file.
- * Also create an empty Material Library file, or truncate the existing one.
  */
-void OBJWriter::write_mtllib(const char *obj_filepath) const
+void OBJWriter::write_mtllib_name(const char *mtl_filepath) const
 {
-  char mtl_filepath[FILE_MAX];
-  BLI_strncpy(mtl_filepath, obj_filepath, FILE_MAX);
-  BLI_path_extension_replace(mtl_filepath, FILE_MAX, ".mtl");
-
-  FILE *mtl_outfile = fopen(mtl_filepath, "w");
-  if (!mtl_outfile) {
-    fprintf(stderr, "Error opening Material Library file:%s", mtl_filepath);
-    return;
-  }
-  fprintf(stderr, "Material Library: %s\n", mtl_filepath);
-  fprintf(mtl_outfile, "# Blender %s\n# www.blender.org\n", BKE_blender_version_string());
-  fclose(mtl_outfile);
-
   /* Split MTL file path into parent directory and filename. */
   char mtl_file_name[FILE_MAXFILE];
   char mtl_dir_name[FILE_MAXDIR];
@@ -452,14 +438,15 @@ void OBJWriter::update_index_offsets(const OBJMesh &obj_mesh_data)
  */
 MTLWriter::MTLWriter(const char *obj_filepath)
 {
-  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");
+  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_) {
     std::perror(std::string("Error in creating the file at: ").append(mtl_filepath_).c_str());
     return;
   }
+  fprintf(stderr, "Material Library: %s\n", mtl_filepath_);
+  fprintf(mtl_outfile_, "# Blender %s\n# www.blender.org\n", BKE_blender_version_string());
 }
 
 MTLWriter::~MTLWriter()
@@ -470,6 +457,20 @@ MTLWriter::~MTLWriter()
   }
 }
 
+/**
+ * \return if the MTL file is writable.
+ */
+bool MTLWriter::good() const
+{
+  return mtl_outfile_ != nullptr;
+}
+
+const char *MTLWriter::mtl_file_path() const
+{
+  BLI_assert(this->good());
+  return mtl_filepath_;
+}
+
 void MTLWriter::write_bsdf_properties(const blender::io::obj::MTLMaterial &mtl_material)
 {
   fprintf(mtl_outfile_,
@@ -519,7 +520,7 @@ void MTLWriter::write_texture_map(const MTLMaterial &mtl_material,
 
 void MTLWriter::append_materials(const OBJMesh &mesh_to_export)
 {
-  assert(mtl_outfile_);
+  BLI_assert(this->good());
   if (!mtl_outfile_) {
     /* Error logging in constructor. */
     return;
diff --git a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
index 7cfb9e79fe9..083319548c3 100644
--- a/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
+++ b/source/blender/io/wavefront_obj/intern/obj_export_file_writer.hh
@@ -81,7 +81,7 @@ class OBJWriter {
   bool init_writer(const char *filepath);
 
   void write_object_name(const OBJMesh &obj_mesh_data) const;
-  void write_mtllib(const char *obj_filepath) const;
+  void write_mtllib_name(const char *obj_filepath) const;
   void write_vertex_coords(const OBJMesh &obj_mesh_data) const;
   void write_uv_coords(OBJMesh &obj_mesh_data) const;
   void write_poly_normals(OBJMesh &obj_mesh_data) const;
@@ -124,12 +124,15 @@ class OBJWriter {
  */
 class MTLWriter {
  private:
+  char mtl_filepath_[FILE_MAX];
   FILE *mtl_outfile_;
 
  public:
   MTLWriter(const char *obj_filepath);
   ~MTLWriter();
 
+  bool good() const;
+  const char *mtl_file_path() const;
   void append_materials(const OBJMesh &mesh_to_export);
 
  private:
diff --git a/source/blender/io/wavefront_obj/intern/obj_exporter.cc b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
index 2333cf9e931..759a8ed7433 100644
--- a/source/blender/io/wavefront_obj/intern/obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_exporter.cc
@@ -111,6 +111,7 @@ static void export_frame(ViewLayer *view_layer,
                          const char *filepath)
 {
   OBJWriter frame_writer(export_params);
+  std::unique_ptr<MTLWriter> mtl_writer = nullptr;
   if (!frame_writer.init_writer(filepath)) {
     return;
   }
@@ -123,8 +124,10 @@ static void export_frame(ViewLayer *view_layer,
       view_layer, depsgraph, export_params, exportable_as_mesh, exportable_as_nurbs);
 
   if (export_params.export_materials) {
-    /* Create an empty MTL file in the beginning, to be appended later. */
-    frame_writer.write_mtllib(filepath);
+    mtl_writer.reset(new MTLWriter(filepath));
+    if (mtl_writer->good()) {
+      frame_writer.write_mtllib_name(mtl_writer->mtl_file_path());
+    }
   }
   for (int i = 0; i < exportable_as_mesh.size(); i++) {
     /* Smooth groups and UV vertex indices may take huge memory, so objects should be freed right
@@ -143,9 +146,8 @@ static void export_frame(ViewLayer *view_layer,
       if (export_params.export_uv) {
         frame_writer.write_uv_coords(*mesh_to_export);
       }
-      if (export_params.export_materials) {
-        MTLWriter mtl_writer(filepath);
-        mtl_writer.append_materials(*mesh_to_export);
+      if (mtl_writer->good()) {
+        mtl_writer->append_materials(*mesh_to_export);
       }
       frame_writer.write_poly_elements(*mesh_to_export);
     }



More information about the Bf-blender-cvs mailing list