[Bf-blender-cvs] [b474c5da30f] temp-usd-udim-import: USD material import improvements.

Michael Kowalski noreply at git.blender.org
Wed Dec 29 03:35:32 CET 2021


Commit: b474c5da30f1d6ac5b0ab36ccd3f3415474bea9a
Author: Michael Kowalski
Date:   Tue Dec 28 19:03:39 2021 -0500
Branches: temp-usd-udim-import
https://developer.blender.org/rBb474c5da30f1d6ac5b0ab36ccd3f3415474bea9a

USD material import improvements.

Per review by Sybren, added find_existing_material()
utility function and comments.

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

M	source/blender/io/usd/intern/usd_reader_mesh.cc
M	source/blender/io/usd/usd.h

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

diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc
index 99c6a8894c3..98d21c6138e 100644
--- a/source/blender/io/usd/intern/usd_reader_mesh.cc
+++ b/source/blender/io/usd/intern/usd_reader_mesh.cc
@@ -89,6 +89,44 @@ static pxr::UsdShadeMaterial compute_bound_material(const pxr::UsdPrim &prim)
   return mtl;
 }
 
+/* Returns an existing Blender material that corresponds to the USD
+ * material with with the given path.  Returns null if no such material
+ * exists. */
+static Material *find_existing_material(const pxr::SdfPath &usd_mat_path,
+                                        const USDImportParams &params,
+                                        const std::map<std::string, Material *> &mat_map,
+                                        const std::map<std::string, std::string> &usd_path_to_mat_name)
+{
+  if (params.mtl_name_collision_mode == USD_MTL_NAME_COLLISION_UNIQUE_NAME) {
+    /* Check if we've already created the Blender material with a modified name. */
+    std::map<std::string, std::string>::const_iterator path_to_name_iter =
+      usd_path_to_mat_name.find(usd_mat_path.GetAsString());
+
+    if (path_to_name_iter != usd_path_to_mat_name.end()) {
+      std::string mat_name = path_to_name_iter->second;
+      std::map<std::string, Material *>::const_iterator mat_iter = mat_map.find(mat_name);
+      if (mat_iter != mat_map.end()) {
+        return mat_iter->second;
+      }
+      else {
+        std::cout
+          << "WARNING: Couldn't find previously assigned Blender material for USD material "
+          << usd_mat_path << std::endl;
+      }
+    }
+  }
+  else {
+    std::string mat_name = usd_mat_path.GetName();
+    std::map<std::string, Material *>::const_iterator mat_iter = mat_map.find(mat_name);
+
+    if (mat_iter != mat_map.end()) {
+      return mat_iter->second;
+    }
+  }
+
+  return nullptr;
+}
+
 static void assign_materials(Main *bmain,
                              Object *ob,
                              const std::map<pxr::SdfPath, int> &mat_index_map,
@@ -118,35 +156,10 @@ static void assign_materials(Main *bmain,
 
   for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) {
 
-    Material *assigned_mat = nullptr;
-
-    if (params.mtl_name_collision_mode == USD_MTL_NAME_COLLISION_UNIQUE_NAME) {
-      /* Check if we've already created the Blender material with a modified name. */
-      std::map<std::string, std::string>::const_iterator path_to_name_iter =
-          usd_path_to_mat_name.find(it->first.GetAsString());
-
-      if (path_to_name_iter != usd_path_to_mat_name.end()) {
-        std::string mat_name = path_to_name_iter->second;
-        std::map<std::string, Material *>::iterator mat_iter = mat_map.find(mat_name);
-        if (mat_iter != mat_map.end()) {
-          assigned_mat = mat_iter->second;
-        }
-        else {
-          std::cout
-              << "WARNING: Couldn't find previously assigned Blender material for USD material "
-              << it->first << std::endl;
-        }
-      }
-    }
-    else {
-      std::string mat_name = it->first.GetName();
-      std::map<std::string, Material *>::iterator mat_iter = mat_map.find(mat_name);
-
-      if (mat_iter != mat_map.end()) {
-        assigned_mat = mat_iter->second;
-      }
-    }
-
+    Material *assigned_mat = find_existing_material(it->first,
+                                                    params,
+                                                    mat_map,
+                                                    usd_path_to_mat_name);
     if (!assigned_mat) {
       /* Blender material doesn't exist, so create it now. */
 
diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h
index 1d696424569..ebdcb099a32 100644
--- a/source/blender/io/usd/usd.h
+++ b/source/blender/io/usd/usd.h
@@ -31,6 +31,8 @@ struct CacheReader;
 struct Object;
 struct bContext;
 
+/* Behavior when the name of an imported material
+ * conflicts with an existing material. */
 typedef enum eUSDMtlNameCollisionMode {
   USD_MTL_NAME_COLLISION_UNIQUE_NAME = 0,
   USD_MTL_NAME_COLLISION_REFERENCE_EXISTING = 1,



More information about the Bf-blender-cvs mailing list