[Bf-blender-cvs] [6b62935e896] soc-2020-io-performance: Fix importing multiple material for one object.

Ankit Meel noreply at git.blender.org
Wed Sep 2 20:41:37 CEST 2020


Commit: 6b62935e8963cccb1d0a324097f035f011806ee1
Author: Ankit Meel
Date:   Thu Sep 3 00:10:45 2020 +0530
Branches: soc-2020-io-performance
https://developer.blender.org/rB6b62935e8963cccb1d0a324097f035f011806ee1

Fix importing multiple material for one object.

Also add sphere type reflection to Metallic socket.

Remove double inline warnings, and also trust compiler to inline
what it deems fit.

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

M	source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
M	source/blender/io/wavefront_obj/intern/obj_import_mesh.cc
M	source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
M	source/blender/io/wavefront_obj/intern/obj_import_mtl.hh
M	source/blender/io/wavefront_obj/intern/obj_import_objects.cc
M	source/blender/io/wavefront_obj/intern/obj_import_objects.hh
M	source/blender/io/wavefront_obj/intern/obj_importer.cc

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

diff --git a/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
index 92a4f4625b5..457bc8c59dd 100644
--- a/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_import_file_reader.cc
@@ -42,7 +42,7 @@ using std::string;
 static void read_next_line(std::ifstream &file, string &r_line)
 {
   std::string new_line;
-  while (file.good() && r_line.back() == '\\') {
+  while (file.good() && !r_line.empty() && r_line.back() == '\\') {
     new_line.clear();
     bool ok = static_cast<bool>(std::getline(file, new_line));
     /* Remove the last backslash character. */
@@ -158,9 +158,9 @@ static void copy_string_to_float(StringRef src, const float fallback_value, floa
  * Catches exception if any string cannot be converted to a float. The destination
  * float is set to the given fallback value in that case.
  */
-static BLI_INLINE void copy_string_to_float(Span<StringRef> src,
-                                     const float fallback_value,
-                                     MutableSpan<float> r_dst)
+static void copy_string_to_float(Span<StringRef> src,
+                                 const float fallback_value,
+                                 MutableSpan<float> r_dst)
 {
   BLI_assert(src.size() >= r_dst.size());
   for (int i = 0; i < r_dst.size(); ++i) {
@@ -174,7 +174,7 @@ static BLI_INLINE void copy_string_to_float(Span<StringRef> src,
  * Catches exception if the string cannot be converted to an integer. The destination
  * int is set to the given fallback value in that case.
  */
-static BLI_INLINE void copy_string_to_int(StringRef src, const int fallback_value, int &r_dst)
+static void copy_string_to_int(StringRef src, const int fallback_value, int &r_dst)
 {
   try {
     r_dst = std::stoi(string(src));
@@ -196,9 +196,9 @@ static BLI_INLINE void copy_string_to_int(StringRef src, const int fallback_valu
  * Catches exception if any string cannot be converted to an integer. The destination
  * int is set to the given fallback value in that case.
  */
-static BLI_INLINE void copy_string_to_int(Span<StringRef> src,
-                                   const int fallback_value,
-                                   MutableSpan<int> r_dst)
+static void copy_string_to_int(Span<StringRef> src,
+                               const int fallback_value,
+                               MutableSpan<int> r_dst)
 {
   BLI_assert(src.size() >= r_dst.size());
   for (int i = 0; i < r_dst.size(); ++i) {
@@ -292,6 +292,7 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometr
    * elements in the object. */
   bool shaded_smooth = false;
   string object_group{};
+  string material_name;
 
   while (std::getline(obj_file_, line)) {
     /* Keep reading new lines if the last character is `\`. */
@@ -310,6 +311,7 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometr
     else if (line_key == "o") {
       shaded_smooth = false;
       object_group = {};
+      material_name = "";
       current_geometry = create_geometry(
           current_geometry, GEOM_MESH, rest_line, r_global_vertices, r_all_geometries, offset);
     }
@@ -375,6 +377,9 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometr
       BLI_assert(current_geometry);
       FaceElement curr_face;
       curr_face.shaded_smooth = shaded_smooth;
+      if (!material_name.empty()) {
+        curr_face.material_name = material_name;
+      }
       if (!object_group.empty()) {
         curr_face.vertex_group = object_group;
         /* Yes it repeats several times, but another if-check will not reduce steps either. */
@@ -470,7 +475,9 @@ void OBJParser::parse_and_store(Vector<std::unique_ptr<Geometry>> &r_all_geometr
       /* Curves mark their end this way. */
     }
     else if (line_key == "usemtl") {
-      current_geometry->material_names_.append(string(rest_line));
+      /* Materials may repeat if faces are written without sorting. */
+      current_geometry->material_names_.add(string(rest_line));
+      material_name = rest_line;
     }
   }
 }
@@ -513,6 +520,22 @@ static StringRef skip_unsupported_options(StringRef line)
   return line;
 }
 
+/**
+ * Fix incoming texture map line keys for variations due to other exporters.
+ */
+static string fix_bad_map_keys(StringRef map_key)
+{
+  string new_map_key(map_key);
+  if (map_key == "refl") {
+    new_map_key = "map_refl";
+  }
+  if (map_key.find("bump") != StringRef::not_found) {
+    /* Handles both "bump" and "map_Bump" */
+    new_map_key = "map_Bump";
+  }
+  return new_map_key;
+}
+
 /**
  * Return a list of all material library filepaths referenced by the OBJ file.
  */
@@ -557,6 +580,10 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mtl
       continue;
     }
 
+    /* Fix lower case/ incomplete texture map identifiers. */
+    const string fixed_key = fix_bad_map_keys(line_key);
+    line_key = fixed_key;
+
     if (line_key == "newmtl") {
       if (r_mtl_materials.remove_as(rest_line)) {
         std::cerr << "Duplicate material found:'" << rest_line
@@ -632,6 +659,15 @@ void MTLParser::parse_and_store(Map<string, std::unique_ptr<MTLMaterial>> &r_mtl
         copy_string_to_float(
             str_map_xx_split[pos_bm + 1], 0.0f, current_mtlmaterial->map_Bump_strength);
       }
+      const int64_t pos_projection{str_map_xx_split.first_index_of_try("-type")};
+      if (pos_projection != -1 && pos_projection + 1 < str_map_xx_split.size()) {
+        /* Only Sphere is supported, so whatever the type is, set it to Sphere.  */
+        tex_map.projection_type = SHD_PROJ_SPHERE;
+        if (str_map_xx_split[pos_projection + 1] != "sphere") {
+          std::cerr << "Using projection type 'sphere', not:'"
+                    << str_map_xx_split[pos_projection + 1] << "'." << std::endl;
+        }
+      }
 
       /* Skip all unsupported options and arguments. */
       tex_map.image_path = string(skip_unsupported_options(rest_line));
diff --git a/source/blender/io/wavefront_obj/intern/obj_import_mesh.cc b/source/blender/io/wavefront_obj/intern/obj_import_mesh.cc
index 881df8feb99..bc47eed00d7 100644
--- a/source/blender/io/wavefront_obj/intern/obj_import_mesh.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_import_mesh.cc
@@ -28,6 +28,7 @@
 #include "BKE_customdata.h"
 #include "BKE_material.h"
 #include "BKE_mesh.h"
+#include "BKE_node.h"
 #include "BKE_object.h"
 #include "BKE_object_deform.h"
 
@@ -138,6 +139,7 @@ std::pair<int64_t, int64_t> MeshFromGeometry::tessellate_polygons(
                                                               face_vert_indices);
     for (Span<int> triangle : new_polygon_indices) {
       r_new_faces.append({curr_face.vertex_group,
+                          curr_face.material_name,
                           curr_face.shaded_smooth,
                           {{face_vert_indices[triangle[0]],
                             face_uv_indices[triangle[0]],
@@ -147,8 +149,10 @@ std::pair<int64_t, int64_t> MeshFromGeometry::tessellate_polygons(
                             face_normal_indices[triangle[1]]},
                            {face_vert_indices[triangle[2]],
                             face_uv_indices[triangle[2]],
-                            face_normal_indices[triangle[2]]}}});
+                            face_normal_indices[triangle[2]]}},
+                          false});
     }
+
     if (new_polygon_indices.size() > 1) {
       Set<std::pair<int, int>> edge_users;
       for (Span<int> triangle : new_polygon_indices) {
@@ -267,6 +271,7 @@ void MeshFromGeometry::create_polys_loops(Span<FaceElement> all_faces)
     if (curr_face.shaded_smooth) {
       mpoly.flag |= ME_SMOOTH;
     }
+    mpoly.mat_nr = mesh_geometry_.material_names().index_of_try(curr_face.material_name);
 
     for (const FaceCorner &curr_corner : curr_face.face_corners) {
       MLoop &mloop = blender_mesh_->mloop[tot_loop_idx];
@@ -369,15 +374,16 @@ void MeshFromGeometry::create_materials(
                 << std::endl;
       continue;
     }
-    const MTLMaterial &curr_mat = *materials.lookup_as(material_name).get();
     BKE_object_material_slot_add(bmain, mesh_object_.get());
     Material *mat = BKE_material_add(bmain, material_name.data());
     BKE_object_material_assign(
         bmain, mesh_object_.get(), mat, mesh_object_.get()->totcol, BKE_MAT_ASSIGN_USERPREF);
 
+    const MTLMaterial &curr_mat = *materials.lookup_as(material_name).get();
     ShaderNodetreeWrap mat_wrap{bmain, curr_mat};
     mat->use_nodes = true;
     mat->nodetree = mat_wrap.get_nodetree();
+    ntreeUpdateTree(bmain, mat->nodetree);
   }
 }
 
diff --git a/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc b/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
index 152f2747674..bb932a3cc85 100644
--- a/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
+++ b/source/blender/io/wavefront_obj/intern/obj_import_mtl.cc
@@ -53,7 +53,7 @@ static void set_property_of_socket(eNodeSocketDatatype property_type,
       break;
     }
     case SOCK_RGBA: {
-      /* Alpha will be manually added. It is not read from the MTL file either. */
+      /* Alpha will be added manually. It is not read from the MTL file either. */
       BLI_assert(value.size() == 3);
       copy_v3_v3(static_cast<bNodeSocketValueRGBA *>(socket->default_value)->value, value.data());
       static_cast<bNodeSocketValueRGBA *>(socket->default_value)->value[3] = 1.0f;
@@ -86,12 +86,13 @@ static std::string replace_all_occurences(StringRef path, StringRef to_remove, S
 }
 
 /**
- * Load image for Image Texture node.
+ * Load image for Image Texture node and set the node properties.
  * Return success if Image can be loaded successfully.
  */
-static bool set_img_filepath(Main *bmain, const tex_map_XX &tex_map, bNode *r_node)
+static bool load_texture_image(Main *bmain, const tex_map_XX &tex_map, bNode *r_node)
 {
-  BLI_assert(r_node);
+  BLI_assert(r_node && r_node->type == SH_NODE_TEX_IMAGE);
+
   std::string tex_file_path{tex_map.mtl_dir_path + tex_map.image_path};
   Image *tex_image = BKE_i

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list