[Bf-blender-cvs] [99ededd9472] soc-2020-io-performance: Skip parsing extra texture map options, move MTLWriter from Objects code.

Ankit Meel noreply at git.blender.org
Sun Aug 16 16:03:09 CEST 2020


Commit: 99ededd947277a816bee70c76d4fbc17c50f7357
Author: Ankit Meel
Date:   Sun Aug 16 19:31:16 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB99ededd947277a816bee70c76d4fbc17c50f7357

Skip parsing extra texture map options, move MTLWriter from Objects code.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_mesh.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh

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

diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
index d7152412ed5..f368f2afc5d 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.cc
@@ -24,8 +24,6 @@
 #include <fstream>
 #include <iostream>
 
-#include "BKE_context.h"
-
 #include "BLI_map.hh"
 #include "BLI_string_ref.hh"
 #include "BLI_vector.hh"
@@ -441,6 +439,42 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
   }
 }
 
+/**
+ * Skip all texture map options and get the filepath from a "map_" line.
+ */
+static string_view skip_unsupported_options(string_view line)
+{
+  TextureMapOptions map_options;
+  string_view last_option;
+  string_view::size_type last_option_pos = 0;
+
+  /* Find the last texture map option. */
+  for (string_view option : map_options.all_options()) {
+    string_view::size_type pos{line.find(option)};
+    if (pos != string_view::npos && pos > last_option_pos) {
+      last_option = option;
+      last_option_pos = pos;
+    }
+  }
+
+  if (last_option.empty()) {
+    /* No option found, line is the filepath */
+    return line;
+  }
+
+  /* Remove upto start of the last option + size of the last option + space after it. */
+  line.remove_prefix(last_option_pos + last_option.size() + 1);
+  for (int i = 0; i < map_options.number_of_args(last_option); i++) {
+    string_view::size_type pos_space{line.find_first_of(' ')};
+    if (pos_space != string_view::npos) {
+      BLI_assert(pos_space + 1 < line.size());
+      line.remove_prefix(pos_space + 1);
+    }
+  }
+
+  return line;
+}
+
 /**
  * Return a list of all material library filepaths referenced by the OBJ file.
  */
@@ -472,6 +506,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
 
   string line;
   MTLMaterial *current_mtlmaterial = nullptr;
+
   while (std::getline(mtl_file_, line)) {
     string_view line_key{}, rest_line{};
     split_line_key_rest(line, line_key, rest_line);
@@ -480,6 +515,9 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
     }
 
     if (line_key == "newmtl") {
+      if (mtl_materials.remove_as(rest_line)) {
+        fprintf(stderr, "Duplicate material name found, using the new one.\n");
+      }
       current_mtlmaterial = &mtl_materials.lookup_or_add_default_as(string(rest_line));
     }
     else if (line_key == "Ns") {
@@ -518,6 +556,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
     else if (line_key.find("map_") != string::npos) {
       if (!current_mtlmaterial->texture_maps.contains_as(string(line_key))) {
         /* No supported texture map found. */
+        std::cout << "Texture map type not supported:'" << line_key << "'" << std::endl;
         continue;
       }
       tex_map_XX &tex_map = current_mtlmaterial->texture_maps.lookup(string(line_key));
@@ -547,7 +586,9 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
             str_map_xx_split[pos_bm + 1], 0.0f, current_mtlmaterial->map_Bump_strength);
       }
 
-      tex_map.image_path = str_map_xx_split.last();
+      /* Skip all unsupported options and arguments. */
+      tex_map.image_path = string(skip_unsupported_options(rest_line));
+      std::cout << tex_map.image_path << std::endl;
       tex_map.mtl_dir_path = mtl_dir_path_;
     }
   }
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
index 1e9c6d4d5fd..b27a1c492a6 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_file_reader.hh
@@ -23,7 +23,10 @@
 
 #pragma once
 
+#include <fstream>
+
 #include "IO_wavefront_obj.h"
+#include "wavefront_obj_im_mtl.hh"
 #include "wavefront_obj_im_objects.hh"
 
 namespace blender::io::obj {
@@ -44,6 +47,37 @@ class OBJParser {
                       const GlobalVertices &global_vertices);
 };
 
+class TextureMapOptions {
+ private:
+  Map<const std::string, int> tex_map_options;
+
+ public:
+  TextureMapOptions()
+  {
+    tex_map_options.add_new("-blendu", 1);
+    tex_map_options.add_new("-blendv", 1);
+    tex_map_options.add_new("-boost", 1);
+    tex_map_options.add_new("-mm", 2);
+    tex_map_options.add_new("-o", 3);
+    tex_map_options.add_new("-s", 3);
+    tex_map_options.add_new("-t", 3);
+    tex_map_options.add_new("-texres", 1);
+    tex_map_options.add_new("-clamp", 1);
+    tex_map_options.add_new("-bm", 1);
+    tex_map_options.add_new("-imfchan", 1);
+  }
+
+  Map<const std::string, int>::KeyIterator all_options() const
+  {
+    return tex_map_options.keys();
+  }
+
+  int number_of_args(std::string_view option) const
+  {
+    return tex_map_options.lookup_as(std::string(option));
+  }
+};
+
 class MTLParser {
  private:
   char mtl_file_path_[FILE_MAX];
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mesh.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mesh.cc
index e3cd1f29312..23729fe2e98 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mesh.cc
@@ -25,6 +25,8 @@
 
 #include "BKE_customdata.h"
 #include "BKE_material.h"
+#include "BKE_mesh.h"
+#include "BKE_object.h"
 #include "BKE_object_deform.h"
 
 #include "BLI_map.hh"
@@ -35,8 +37,6 @@
 #include "DNA_meshdata_types.h"
 
 #include "wavefront_obj_im_mesh.hh"
-#include "wavefront_obj_im_mtl.hh"
-#include "wavefront_obj_im_objects.hh"
 
 namespace blender::io::obj {
 /**
@@ -194,6 +194,11 @@ void MeshFromGeometry::create_materials(Main *bmain,
                                         const Map<std::string, MTLMaterial> &materials)
 {
   for (StringRef material_name : mesh_geometry_.material_names()) {
+    if (!materials.contains_as(material_name)) {
+      std::cerr << "Material named '" << material_name << "' not found in material library."
+                << std::endl;
+      continue;
+    }
     const MTLMaterial &curr_mat = materials.lookup_as(material_name);
     BKE_object_material_slot_add(bmain, blender_object_.get());
     Material *mat = BKE_material_add(bmain, material_name.data());
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
index f2594a14ff6..2de4c762673 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.cc
@@ -25,7 +25,6 @@
 #include "BKE_node.h"
 
 #include "BLI_map.hh"
-#include "BLI_string_utf8.h"
 
 #include "DNA_node_types.h"
 
@@ -102,11 +101,12 @@ static bool set_img_filepath(Main *bmain, const tex_map_XX &tex_map, bNode *r_no
   }
   if (!tex_image) {
     fprintf(stderr, "Cannot load image file:'%s'\n", tex_map.image_path.c_str());
-    /* Remove quotes from the filename which has now been added to filepath. */
-    std::string no_quote_path{replace_all_occurences(tex_file_path, "\"", "")};
+    /* Remove quotes from the filepath. */
+    std::string no_quote_path{tex_map.mtl_dir_path +
+                              replace_all_occurences(tex_map.image_path, "\"", "")};
     tex_image = BKE_image_load(nullptr, no_quote_path.c_str());
-    fprintf(stderr, "Cannot load image file:'%s'\n", no_quote_path.data());
     if (!tex_image) {
+      fprintf(stderr, "Cannot load image file:'%s'\n", no_quote_path.data());
       std::string no_underscore_path{replace_all_occurences(no_quote_path, "_", " ")};
       tex_image = BKE_image_load(nullptr, no_underscore_path.c_str());
       fprintf(stderr, "Cannot load image file:'%s'\n", no_underscore_path.data());
@@ -211,7 +211,8 @@ void ShaderNodetreeWrap::set_bsdf_socket_values()
  */
 void ShaderNodetreeWrap::add_image_textures(Main *bmain)
 {
-  for (const Map<std::string, tex_map_XX>::Item &texture_map : mtl_mat_->texture_maps.items()) {
+  for (const Map<const std::string, tex_map_XX>::Item &texture_map :
+       mtl_mat_->texture_maps.items()) {
 
     if (texture_map.value.image_path.empty()) {
       /* No Image texture node of this map type to add in this material. */
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.hh
index d14f6ef91ba..5ff301356a2 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.hh
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_mtl.hh
@@ -23,18 +23,71 @@
 
 #pragma once
 
+//#include <memory>
+
 #include "MEM_guardedalloc.h"
-#include <memory>
 
-#include "BKE_lib_id.h"
-#include "BKE_node.h"
+#include "DNA_node_types.h"
 
+#include "BLI_float3.hh"
 #include "BLI_map.hh"
 #include "BLI_string_ref.hh"
 
-#include "wavefront_obj_im_objects.hh"
-
 namespace blender::io::obj {
+/**
+ * Used for storing parameters for all kinds of texture maps from MTL file.
+ */
+struct tex_map_XX {
+  tex_map_XX(StringRef to_socket_id) : dest_socket_id(to_socket_id){};
+
+  const std::string dest_socket_id{};
+  float3 translation = {0.0f, 0.0f, 0.0f};
+  float3 scale = {1.0f, 1.0f, 1.0f};
+  std::string image_path{};
+  std::string mtl_dir_path;
+};
+
+/**
+ * Store material data parsed from MTL file.
+ */
+struct MTLMaterial {
+  MTLMaterial()
+  {
+    texture_maps.add("map_Kd", tex_map_XX("Base Color"));
+    texture_maps.add("map_Ks", tex_map_XX("Specular"));
+    texture_maps.add("map_Ns", tex_map_XX("Roughness"));
+    texture_maps.add("map_d", tex_map_XX("Alpha"));
+    texture_maps.add("map_refl", tex_map_XX("Metallic"));
+    texture_maps.add("map_Ke", tex_map_XX("Emission"));
+    texture_maps.add("map_Bump", tex_map_XX("Normal"));
+  }
+
+  /**
+   * Return a reference to the texture map corresponding to the given ID
+   * Caller must ensure that the lookup key given exists in the Map.
+   */
+  tex_map_XX &tex_map_of_type(StringRef map_string)
+  {
+    {
+      BLI_assert(texture_maps.contains_as(map_string));
+      return texture_maps.lookup_as(map_string);
+    }
+  }
+


@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list