[Bf-blender-cvs] [faa11ec04a4] soc-2020-io-performance: Support multiple materials in the same object.

Ankit Meel noreply at git.blender.org
Mon Jun 29 15:50:07 CEST 2020


Commit: faa11ec04a4dbfcbe5ca47b7db684f943d470da2
Author: Ankit Meel
Date:   Mon Jun 29 19:19:58 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rBfaa11ec04a4dbfcbe5ca47b7db684f943d470da2

Support multiple materials in the same object.

Design has slightly changed:
The line with `usemtl` has moved just before a face element is
being written. So whenever a face with a new material is encountered,
it can be labelled.

Appending to the MTL file is still at the same place, writing once every
object.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
index d5b879487a0..9478c0b4521 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter.cc
@@ -115,10 +115,8 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
         frame_writer.write_uv_coords(mesh_to_export, uv_indices);
       }
       if (export_params->export_materials) {
-        /* Write material name just before face elements. */
-        frame_writer.write_usemtl(mesh_to_export);
         MTLWriter mtl_writer(filepath);
-        mtl_writer.append_material(mesh_to_export);
+        mtl_writer.append_materials(mesh_to_export);
       }
       frame_writer.write_poly_indices(mesh_to_export, uv_indices);
     }
@@ -138,14 +136,16 @@ static void export_frame(bContext *C, const OBJExportParams *export_params, cons
  */
 void exporter_main(bContext *C, const OBJExportParams *export_params)
 {
+  /* TODO ankitm: find a better way to exit edit mode that doesn't hit assert
+   * https://hastebin.com/mitihetagi in file F8653460 */
   ED_object_editmode_exit(C, EM_FREEDATA);
   Scene *scene = CTX_data_scene(C);
   const char *filepath = export_params->filepath;
 
   /* Single frame export, i.e. no amimation is to be exported. */
   if (!export_params->export_animation) {
-    export_frame(C, export_params, filepath);
     fprintf(stderr, "Writing to %s\n", filepath);
+    export_frame(C, export_params, filepath);
     return;
   }
 
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
index 7d77215b122..e7888c99c91 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.cc
@@ -229,11 +229,11 @@ void OBJMesh::calc_poly_normal(float r_poly_normal[3], uint poly_index)
 }
 
 /**
- * Set argument pointer to the name of an object's active material.
+ * Set argument pointer to the name of an object's mat_nr-th index material.
  */
-void OBJMesh::set_object_material_name(const char **r_mat_name)
+void OBJMesh::set_object_material_name(const char **r_mat_name, short mat_nr)
 {
-  Material *mat = BKE_object_material_get(_export_object_eval, _export_object_eval->actcol);
+  Material *mat = BKE_object_material_get(_export_object_eval, mat_nr);
   *r_mat_name = mat->id.name + 2;
 }
 
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
index 6a1e31c6bfa..34ba48d759c 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mesh.hh
@@ -78,6 +78,12 @@ class OBJMesh {
     return _tot_edges;
   }
 
+  /** Total materials in the object to export. */
+  uint tot_col()
+  {
+    return _export_mesh_eval->totcol;
+  }
+
   Mesh *export_mesh_eval()
   {
     return _export_mesh_eval;
@@ -88,10 +94,10 @@ class OBJMesh {
     return _export_mesh_eval->mpoly[i];
   }
 
-  /** Return active material of the object. */
-  Material *export_object_material()
+  /** Return mat_nr-th material of the object. */
+  Material *get_export_object_material(short mat_nr)
   {
-    return BKE_object_material_get(_export_object_eval, _export_object_eval->actcol);
+    return BKE_object_material_get(_export_object_eval, mat_nr);
   }
 
   /**
@@ -99,7 +105,8 @@ class OBJMesh {
    */
   void set_object_name(const char **object_name);
   void set_object_data_name(const char **r_object_data_name);
-  void set_object_material_name(const char **r_mat_name);
+  void set_object_material_name(const char **r_mat_name, short mat_nr);
+
   /**
    * Calculate coordinates of the vertex at given index.
    */
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc
index 5be3a76790b..228401c9db7 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.cc
@@ -150,22 +150,8 @@ const char *MTLWriter::get_image_filepath(const bNode *tex_node)
   return nullptr;
 }
 
-/** Append an object's material to the .mtl file. */
-void MTLWriter::append_material(OBJMesh &mesh_to_export)
+void MTLWriter::write_curr_material(const char *object_name)
 {
-  _mtl_outfile = fopen(_mtl_filepath, "a");
-  if (!_mtl_outfile) {
-    fprintf(stderr, "Error in opening file at %s\n", _mtl_filepath);
-    return;
-  }
-
-  const char *object_name;
-  mesh_to_export.set_object_name(&object_name);
-  _export_mtl = mesh_to_export.export_object_material();
-  if (!_export_mtl) {
-    fprintf(stderr, "No active material for the object: %s.\n", object_name);
-    return;
-  }
   fprintf(_mtl_outfile, "\nnewmtl %s\n", _export_mtl->id.name + 2);
 
   init_bsdf_node(object_name);
@@ -309,6 +295,23 @@ void MTLWriter::append_material(OBJMesh &mesh_to_export)
             normal_map_strength,
             tex_image_filepath);
   }
+}
+
+/** Append an object's materials to the .mtl file. */
+void MTLWriter::append_materials(OBJMesh &mesh_to_export)
+{
+  _mtl_outfile = fopen(_mtl_filepath, "a");
+  if (!_mtl_outfile) {
+    fprintf(stderr, "Error in opening file at %s\n", _mtl_filepath);
+    return;
+  }
+
+  const char *object_name;
+  mesh_to_export.set_object_name(&object_name);
+  for (int curr_mat = 0; curr_mat < mesh_to_export.tot_col(); curr_mat++) {
+    _export_mtl = mesh_to_export.get_export_object_material(curr_mat + 1);
+    write_curr_material(object_name);
+  }
 
   fclose(_mtl_outfile);
 }
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh
index 8433efbd4f8..742dd062fd4 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_exporter_mtl.hh
@@ -42,15 +42,18 @@ class MTLWriter {
     BLI_strncpy(_mtl_filepath, obj_filepath, PATH_MAX);
     BLI_path_extension_replace(_mtl_filepath, PATH_MAX, ".mtl");
   }
-  /** Append an object's material to the .mtl file. */
-  void append_material(OBJMesh &mesh_to_export);
+  /** Append an object's materials to the .mtl file. */
+  void append_materials(OBJMesh &mesh_to_export);
 
  private:
   FILE *_mtl_outfile;
   char _mtl_filepath[PATH_MAX];
-  /** An object's material, to be exported. */
-  Material *_export_mtl;
 
+  /** Write _one_ material to the MTL file. */
+  void write_curr_material(const char *object_name);
+
+  /** One of the object's materials, to be exported. */
+  Material *_export_mtl;
   /** First bsdf node encountered in the object's nodes. */
   bNode *_bsdf_node;
   void init_bsdf_node(const char *object_name);
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
index ab7843b1bbc..3713e27e235 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_file_handler.cc
@@ -112,6 +112,11 @@ void OBJWriter::write_mtllib(const char *obj_filepath)
   BLI_path_extension_replace(mtl_filepath, PATH_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);
 
@@ -174,21 +179,29 @@ void OBJWriter::write_poly_normals(OBJMesh &obj_mesh_data)
 }
 
 /**
- * Write material name and material group of an object in the OBJ file.
+ * Write material name and material group of a face in the OBJ file.
  * \note It doesn't write to the material library.
  */
-void OBJWriter::write_usemtl(OBJMesh &obj_mesh_data)
+void OBJWriter::write_poly_material(short &last_face_mat_nr, OBJMesh &obj_mesh_data, short mat_nr)
 {
-  const char *mat_name;
-  obj_mesh_data.set_object_material_name(&mat_name);
-  if (_export_params->export_material_groups) {
-    const char *object_name;
-    const char *object_data_name;
-    obj_mesh_data.set_object_name(&object_name);
-    obj_mesh_data.set_object_data_name(&object_data_name);
-    fprintf(_outfile, "g %s_%s_%s\n", object_name, object_data_name, mat_name);
+  if (_export_params->export_materials == false) {
+    return;
+  }
+  /* Whenever a face with a new material is encountered, write its material and group, otherwise
+   * pass. */
+  if (UNLIKELY(last_face_mat_nr != mat_nr)) {
+    const char *mat_name;
+    obj_mesh_data.set_object_material_name(&mat_name, mat_nr + 1);
+    if (_export_params->export_material_groups) {
+      const char *object_name;
+      const char *object_data_name;
+      obj_mesh_data.set_object_name(&object_name);
+      obj_mesh_data.set_object_data_name(&object_data_name);
+      fprintf(_outfile, "g %s_%s_%s\n", object_name, object_data_name, mat_name);
+    }
+    fprintf(_outfile, "usemtl %s\n", mat_name);
+    last_face_mat_nr = mat_nr;
   }
-  fprintf(_outfile, "usemtl %s\n", mat_name);
 }
 
 /** Define and write a face with at least vertex indices, and conditionally with UV vertex indices
@@ -200,6 +213,8 @@ void OBJWriter::write_poly_indices(OBJMesh &obj_mesh_data, Span<Vector<uint>> uv
   Vector<uint> vertex_indices;
   Vector<uint> normal_indices;
 
+  short last_face_mat_nr = -1;
+
   if (_export_params->export_normals) {
     if (_export_params->export_uv) {
       /* Write both normals and UV indices. */
@@ -207,6 +222,8 @@ void OBJWriter::write_poly_indices(OBJMesh &obj_mesh_data, Span<Vector<uint>> uv
         obj_mesh_data.calc_poly_vertex_indices(vertex_indices, i);
         obj_mesh_data.calc_poly_normal_indices(normal_indices, i);
         const MPoly &poly_to_write = obj_mesh_data.get_ith_poly(i);
+
+        

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list