[Bf-blender-cvs] [606b0d7da34] soc-2020-io-performance: Fix several material import bugs, remove wrong assert.

Ankit Meel noreply at git.blender.org
Thu Aug 13 14:48:56 CEST 2020


Commit: 606b0d7da341d6b03afb940032c489bf0ec19837
Author: Ankit Meel
Date:   Thu Aug 13 18:00:56 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB606b0d7da341d6b03afb940032c489bf0ec19837

Fix several material import bugs, remove wrong assert.

Images were not being loaded since the parent directory was
missing in the path. So add it in tex_map_XX.

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

M	source/blender/io/wavefront_obj/intern/wavefront_obj_ex_mtl.cc
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_objects.cc
M	source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
M	source/blender/io/wavefront_obj/intern/wavefront_obj_importer.cc

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

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 1768e9dae53..d7d50a1fee5 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
@@ -45,7 +45,6 @@ static void copy_property_from_node(MutableSpan<float> r_property,
                                     const bNode *curr_node,
                                     const char *identifier)
 {
-  BLI_assert(curr_node);
   if (!curr_node) {
     return;
   }
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 089673a9efd..7d0b4cd65ac 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
@@ -436,7 +436,7 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &all_geometrie
       /* Curves mark their end this way. */
     }
     else if (line_key == "usemtl") {
-      current_geometry->material_name_.append(string(rest_line));
+      current_geometry->material_names_.append(string(rest_line));
     }
   }
 }
@@ -452,11 +452,12 @@ Span<std::string> OBJParser::mtl_libraries() const
 /**
  * Open material library file.
  */
-MTLParser::MTLParser(StringRef mtl_library, StringRefNull obj_filepath) : mtl_library_(mtl_library)
+MTLParser::MTLParser(StringRef mtl_library, StringRefNull obj_filepath)
 {
   char obj_file_dir[FILE_MAXDIR];
   BLI_split_dir_part(obj_filepath.data(), obj_file_dir, FILE_MAXDIR);
-  BLI_path_join(mtl_file_path_, FILE_MAX, obj_file_dir, mtl_library_.data(), NULL);
+  BLI_path_join(mtl_file_path_, FILE_MAX, obj_file_dir, mtl_library.data(), NULL);
+  BLI_split_dir_part(mtl_file_path_, mtl_dir_path_, FILE_MAXDIR);
   mtl_file_.open(mtl_file_path_);
 }
 
@@ -519,7 +520,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
         /* No supported texture map found. */
         continue;
       }
-      tex_map_XX &tex_map = current_mtlmaterial->texture_maps.lookup(string(rest_line));
+      tex_map_XX &tex_map = current_mtlmaterial->texture_maps.lookup(string(line_key));
       Vector<string_view> str_map_xx_split{};
       split_by_char(rest_line, ' ', str_map_xx_split);
 
@@ -547,6 +548,7 @@ void MTLParser::parse_and_store(Map<string, MTLMaterial> &mtl_materials)
       }
 
       tex_map.image_path = str_map_xx_split.last();
+      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 2dcc5686017..1e9c6d4d5fd 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
@@ -46,8 +46,8 @@ class OBJParser {
 
 class MTLParser {
  private:
-  StringRef mtl_library_;
-  char mtl_file_path_[FILE_MAX]{};
+  char mtl_file_path_[FILE_MAX];
+  char mtl_dir_path_[FILE_MAX];
   std::ifstream mtl_file_;
 
  public:
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 d4dc3eeba01..e3cd1f29312 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
@@ -193,13 +193,14 @@ void MeshFromGeometry::create_uv_verts()
 void MeshFromGeometry::create_materials(Main *bmain,
                                         const Map<std::string, MTLMaterial> &materials)
 {
-  for (const Map<std::string, MTLMaterial>::Item &curr_mat : materials.items()) {
+  for (StringRef material_name : mesh_geometry_.material_names()) {
+    const MTLMaterial &curr_mat = materials.lookup_as(material_name);
     BKE_object_material_slot_add(bmain, blender_object_.get());
-    Material *mat = BKE_material_add(bmain, curr_mat.key.c_str());
+    Material *mat = BKE_material_add(bmain, material_name.data());
     BKE_object_material_assign(
         bmain, blender_object_.get(), mat, blender_object_.get()->totcol, BKE_MAT_ASSIGN_USERPREF);
 
-    ShaderNodetreeWrap mat_wrap{bmain, curr_mat.value};
+    ShaderNodetreeWrap mat_wrap{bmain, curr_mat};
     mat->use_nodes = true;
     mat->nodetree = mat_wrap.get_nodetree();
   }
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 4b55a756145..5aac94fe11d 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
@@ -90,24 +90,31 @@ static std::string replace_all_occurences(StringRef path, StringRef to_remove, S
  * Load image for Image Texture node.
  * Return success if Image can be loaded successfully.
  */
-static bool set_img_filepath(Main *bmain, StringRef value, bNode *r_node)
+static bool set_img_filepath(Main *bmain, const tex_map_XX &tex_map, bNode *r_node)
 {
   BLI_assert(r_node);
-  Image *tex_image = BKE_image_load(bmain, value.data());
+  std::string tex_file_path{tex_map.mtl_dir_path + tex_map.image_path};
+  Image *tex_image = BKE_image_load(bmain, tex_file_path.c_str());
   if (!tex_image) {
-    fprintf(stderr, "Cannot load image file:'%s'", value.data());
+    /* Could be absolute, so load the image directly. */
+    fprintf(stderr, "Cannot load image file:'%s'\n", tex_file_path.c_str());
+    tex_image = BKE_image_load(bmain, tex_map.image_path.c_str());
+  }
+  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(value, "\"", "")};
+    std::string no_quote_path{replace_all_occurences(tex_file_path, "\"", "")};
     tex_image = BKE_image_load(nullptr, no_quote_path.c_str());
-    fprintf(stderr, "Cannot load image file:'%s'", no_quote_path.data());
+    fprintf(stderr, "Cannot load image file:'%s'\n", no_quote_path.data());
     if (!tex_image) {
       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'", no_underscore_path.data());
+      fprintf(stderr, "Cannot load image file:'%s'\n", no_underscore_path.data());
     }
   }
   BLI_assert(tex_image);
-  if (!tex_image) {
+  if (tex_image) {
+    fprintf(stderr, "Loaded image from:'%s'\n", tex_image->filepath);
     r_node->id = reinterpret_cast<ID *>(tex_image);
     return true;
   }
@@ -187,8 +194,10 @@ void ShaderNodetreeWrap::link_sockets(unique_node_ptr from_node,
  */
 void ShaderNodetreeWrap::set_bsdf_socket_values()
 {
-  set_property_of_socket(SOCK_FLOAT, "Specular", {mtl_mat_->Ns}, bsdf_.get());
-  /* Only one value is taken for Metallic. */
+  const float specular_exponent{1 - sqrt(mtl_mat_->Ns) / 30};
+  set_property_of_socket(SOCK_FLOAT, "Roughness", {specular_exponent}, bsdf_.get());
+  /* Only one value is taken for Metallic and Specular. */
+  set_property_of_socket(SOCK_FLOAT, "Specular", {mtl_mat_->Ks[0]}, bsdf_.get());
   set_property_of_socket(SOCK_FLOAT, "Metallic", {mtl_mat_->Ka[0]}, bsdf_.get());
   set_property_of_socket(SOCK_FLOAT, "IOR", {mtl_mat_->Ni}, bsdf_.get());
   set_property_of_socket(SOCK_FLOAT, "Alpha", {mtl_mat_->d}, bsdf_.get());
@@ -202,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<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. */
       continue;
@@ -210,13 +220,14 @@ void ShaderNodetreeWrap::add_image_textures(Main *bmain)
     unique_node_ptr tex_node{add_node_to_tree(SH_NODE_TEX_IMAGE)};
     unique_node_ptr vector_node{add_node_to_tree(SH_NODE_MAPPING)};
     unique_node_ptr normal_map_node = nullptr;
+    unique_node_ptr texture_coordinate(add_node_to_tree(SH_NODE_TEX_COORD));
     if (texture_map.key == "map_Bump") {
       normal_map_node.reset(add_node_to_tree(SH_NODE_NORMAL_MAP));
       set_property_of_socket(
           SOCK_FLOAT, "Strength", {mtl_mat_->map_Bump_strength}, normal_map_node.get());
     }
 
-    if (!set_img_filepath(bmain, texture_map.value.image_path, tex_node.get())) {
+    if (!set_img_filepath(bmain, texture_map.value, image_texture.get())) {
       /* Image cannot be added, so don't link image texture, vector, normal map nodes. */
       continue;
     }
@@ -228,6 +239,7 @@ void ShaderNodetreeWrap::add_image_textures(Main *bmain)
     if (normal_map_node) {
       link_sockets(std::move(tex_node), "Color", normal_map_node.get(), "Color");
       link_sockets(std::move(normal_map_node), "Normal", bsdf_.get(), "Normal");
+    link_sockets(std::move(texture_coordinate), "UV", mapping.get(), "Vector");
     }
     else {
       link_sockets(std::move(tex_node), "Color", bsdf_.get(), texture_map.value.dest_socket_id);
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
index 5eab745cf3f..059a6913235 100644
--- a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
+++ b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.cc
@@ -109,6 +109,11 @@ int Geometry::tot_normals() const
   return tot_normals_;
 }
 
+Span<std::string> Geometry::material_names() const
+{
+  return material_names_;
+}
+
 const NurbsElement &Geometry::nurbs_elem() const
 {
   return nurbs_element_;
diff --git a/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh b/source/blender/io/wavefront_obj/intern/wavefront_obj_im_objects.hh
index ddc7b1a016f..2228795ca82 100644
--- a/source/blender/io/wavefront_obj/intern/wavef

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list