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

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


Commit: 4d0b45fcdcaffd9b59b53109c9bd28481572d321
Author: Michael Kowalski
Date:   Tue Dec 28 15:37:26 2021 -0500
Branches: temp-usd-udim-import
https://developer.blender.org/rB4d0b45fcdcaffd9b59b53109c9bd28481572d321

USD material import improvements.

Per suggestions from Jesse and Sybren in their
reviews.

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

M	source/blender/editors/io/io_usd.c
M	source/blender/io/usd/intern/usd_reader_material.cc
M	source/blender/io/usd/intern/usd_reader_mesh.cc
M	source/blender/io/usd/intern/usd_reader_prim.h
M	source/blender/io/usd/usd.h

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

diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c
index 1c57615969e..e33edfdf08b 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -74,16 +74,16 @@ const EnumPropertyItem rna_enum_usd_export_evaluation_mode_items[] = {
 };
 
 const EnumPropertyItem rna_enum_usd_mtl_name_collision_mode_items[] = {
-    {USD_MTL_NAME_COLLISION_MODIFY,
-     "MODIFY",
+    {USD_MTL_NAME_COLLISION_UNIQUE_NAME,
+     "UNIQUE_NAME",
      0,
-     "Modify",
+     "Unique Name",
      "Create a unique name for the imported material"},
-    {USD_MTL_NAME_COLLISION_SKIP,
-     "SKIP",
+    {USD_MTL_NAME_COLLISION_REFERENCE_EXISTING,
+     "REFERENCE_EXISTING",
      0,
-     "Skip",
-     "Keep the existing material and discard the imported material"},
+     "Reference Existing",
+     "If a material with the same name already exists, reference that instead of importing"},
     {0, NULL, 0, NULL, NULL},
 };
 
@@ -543,7 +543,7 @@ void WM_OT_usd_import(struct wmOperatorType *ot)
       ot->srna,
       "mtl_name_collision_mode",
       rna_enum_usd_mtl_name_collision_mode_items,
-      USD_MTL_NAME_COLLISION_MODIFY,
+      USD_MTL_NAME_COLLISION_UNIQUE_NAME,
       "Material Name Collision",
       "Behavior when the name of an imported material conflicts with an existing material");
 }
diff --git a/source/blender/io/usd/intern/usd_reader_material.cc b/source/blender/io/usd/intern/usd_reader_material.cc
index 375b36875ef..950c6a5cefe 100644
--- a/source/blender/io/usd/intern/usd_reader_material.cc
+++ b/source/blender/io/usd/intern/usd_reader_material.cc
@@ -113,9 +113,9 @@ static void link_nodes(
 
 /* Returns a layer handle retrieved from the given attribute's property specs.
  * Note that the returned handle may be invalid if no layer could be found. */
-static pxr::SdfLayerHandle get_layer_handle(const pxr::UsdAttribute &Attribute)
+static pxr::SdfLayerHandle get_layer_handle(const pxr::UsdAttribute &attribute)
 {
-  for (auto PropertySpec : Attribute.GetPropertyStack(pxr::UsdTimeCode::EarliestTime())) {
+  for (auto PropertySpec : attribute.GetPropertyStack(pxr::UsdTimeCode::EarliestTime())) {
     if (PropertySpec->HasDefaultValue() ||
         PropertySpec->GetLayer()->GetNumTimeSamplesForPath(PropertySpec->GetPath()) > 0) {
       return PropertySpec->GetLayer();
@@ -136,14 +136,6 @@ static bool get_udim_tiles(const std::string &file_path,
                            std::string *r_first_tile_path,
                            std::vector<int> *r_tile_indices)
 {
-  if (file_path.empty()) {
-    return false;
-  }
-
-  if (!(r_first_tile_path && r_tile_indices)) {
-    return false;
-  }
-
   /* Check if we have a UDIM path. */
   std::size_t udim_token_offset = file_path.find("<UDIM>");
 
@@ -160,21 +152,14 @@ static bool get_udim_tiles(const std::string &file_path,
   std::string base_udim_path(file_path);
   base_udim_path.replace(udim_token_offset, 6, "1001");
 
-  /* Exctract the file and directory names from the path. */
+  /* Extract the file and directory names from the path. */
   char filename[FILE_MAX], dirname[FILE_MAXDIR];
   BLI_split_dirfile(base_udim_path.c_str(), dirname, filename, sizeof(dirname), sizeof(filename));
 
   /* Split the base and head portions of the file name. */
   ushort digits = 0;
   char base_head[FILE_MAX], base_tail[FILE_MAX];
-  base_head[0] = '\0';
-  base_tail[0] = '\0';
-  int id = BLI_path_sequence_decode(filename, base_head, base_tail, &digits);
-
-  /* As a sanity check, confirm that we got our original index. */
-  if (id != 1001) {
-    return false;
-  }
+  BLI_path_sequence_decode(filename, base_head, base_tail, &digits);
 
   /* Iterate over the directory contents to find files
    * with matching names and with the expected index format
@@ -192,8 +177,6 @@ static bool get_udim_tiles(const std::string &file_path,
     }
 
     char head[FILE_MAX], tail[FILE_MAX];
-    head[0] = '\0';
-    tail[0] = '\0';
     int id = BLI_path_sequence_decode(dir[i].relname, head, tail, &digits);
 
     if (digits == 0 || digits > 4 || !(STREQLEN(base_head, head, FILE_MAX)) ||
@@ -226,14 +209,10 @@ static bool get_udim_tiles(const std::string &file_path,
 /* Add tiles with the given indices to the given image. */
 static void add_udim_tiles(Image *image, const std::vector<int> &indices)
 {
-  if (!image || indices.empty()) {
-    return;
-  }
-
   image->source = IMA_SRC_TILED;
 
-  for (int i = 0; i < indices.size(); ++i) {
-    BKE_image_add_tile(image, indices[i], nullptr);
+  for (int tile_number : indices) {
+    BKE_image_add_tile(image, tile_number, nullptr);
   }
 }
 
diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc
index a7cd0d07d72..99c6a8894c3 100644
--- a/source/blender/io/usd/intern/usd_reader_mesh.cc
+++ b/source/blender/io/usd/intern/usd_reader_mesh.cc
@@ -76,6 +76,19 @@ static void build_mat_map(const Main *bmain, std::map<std::string, Material *> *
   }
 }
 
+static pxr::UsdShadeMaterial compute_bound_material(const pxr::UsdPrim &prim)
+{
+  pxr::UsdShadeMaterialBindingAPI api = pxr::UsdShadeMaterialBindingAPI(prim);
+  pxr::UsdShadeMaterial mtl = api.ComputeBoundMaterial();
+
+  if (!mtl) {
+    /* Check for a preview material as fallback. */
+    mtl = api.ComputeBoundMaterial(pxr::UsdShadeTokens->preview);
+  }
+
+  return mtl;
+}
+
 static void assign_materials(Main *bmain,
                              Object *ob,
                              const std::map<pxr::SdfPath, int> &mat_index_map,
@@ -107,7 +120,7 @@ static void assign_materials(Main *bmain,
 
     Material *assigned_mat = nullptr;
 
-    if (params.mtl_name_collision_mode == USD_MTL_NAME_COLLISION_MODIFY) {
+    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());
@@ -159,7 +172,7 @@ static void assign_materials(Main *bmain,
       std::string mat_name = pxr::TfMakeValidIdentifier(assigned_mat->id.name + 2);
       mat_map[mat_name] = assigned_mat;
 
-      if (params.mtl_name_collision_mode == USD_MTL_NAME_COLLISION_MODIFY) {
+      if (params.mtl_name_collision_mode == USD_MTL_NAME_COLLISION_UNIQUE_NAME) {
         /* Record the name of the Blender material we created for the USD material
          * with the given path. */
         usd_path_to_mat_name[it->first.GetAsString()] = mat_name;
@@ -719,16 +732,8 @@ void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime,
   int current_mat = 0;
   if (!subsets.empty()) {
     for (const pxr::UsdGeomSubset &subset : subsets) {
-      pxr::UsdShadeMaterialBindingAPI subset_api = pxr::UsdShadeMaterialBindingAPI(
-          subset.GetPrim());
-
-      pxr::UsdShadeMaterial subset_mtl = subset_api.ComputeBoundMaterial();
-
-      if (!subset_mtl) {
-        /* Check for a preview material as fallback. */
-        subset_mtl = subset_api.ComputeBoundMaterial(pxr::UsdShadeTokens->preview);
-      }
 
+      pxr::UsdShadeMaterial subset_mtl = utils::compute_bound_material(subset.GetPrim());
       if (!subset_mtl) {
         continue;
       }
@@ -757,17 +762,9 @@ void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime,
   }
 
   if (r_mat_map->empty()) {
-    pxr::UsdShadeMaterialBindingAPI api = pxr::UsdShadeMaterialBindingAPI(prim_);
-
-    pxr::UsdShadeMaterial mtl = api.ComputeBoundMaterial();
-
-    if (!mtl) {
-      /* Check for a preview material as fallback. */
-      mtl = api.ComputeBoundMaterial(pxr::UsdShadeTokens->preview);
-    }
 
+    pxr::UsdShadeMaterial mtl = utils::compute_bound_material(prim_);
     if (mtl) {
-
       pxr::SdfPath mtl_path = mtl.GetPath();
 
       if (!mtl_path.IsEmpty()) {
diff --git a/source/blender/io/usd/intern/usd_reader_prim.h b/source/blender/io/usd/intern/usd_reader_prim.h
index b5d795ac3bd..c4457867849 100644
--- a/source/blender/io/usd/intern/usd_reader_prim.h
+++ b/source/blender/io/usd/intern/usd_reader_prim.h
@@ -54,7 +54,7 @@ struct ImportSettings {
   CacheFile *cache_file;
 
   /* Map a USD matrial prim path to a Blender material name.
-   * This map might be updated by readers during stage traversal. */
+   * This map is updated by readers during stage traversal. */
   mutable std::map<std::string, std::string> usd_path_to_mat_name;
 
   ImportSettings()
diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h
index 275b7fdc3b6..1d696424569 100644
--- a/source/blender/io/usd/usd.h
+++ b/source/blender/io/usd/usd.h
@@ -32,8 +32,8 @@ struct Object;
 struct bContext;
 
 typedef enum eUSDMtlNameCollisionMode {
-  USD_MTL_NAME_COLLISION_MODIFY = 0,
-  USD_MTL_NAME_COLLISION_SKIP = 1,
+  USD_MTL_NAME_COLLISION_UNIQUE_NAME = 0,
+  USD_MTL_NAME_COLLISION_REFERENCE_EXISTING = 1,
 } eUSDMtlNameCollisionMode;
 
 struct USDExportParams {



More information about the Bf-blender-cvs mailing list