[Bf-blender-cvs] [16cb6ea7c46] usd-importer-T81257: Cleaned up and commented USD material import code.
Michael A. Kowalski
noreply at git.blender.org
Fri Oct 23 17:49:44 CEST 2020
Commit: 16cb6ea7c46e352d4a60170d6aed594692f9ef03
Author: Michael A. Kowalski
Date: Fri Oct 23 10:44:32 2020 -0400
Branches: usd-importer-T81257
https://developer.blender.org/rB16cb6ea7c46e352d4a60170d6aed594692f9ef03
Cleaned up and commented USD material import code.
===================================================================
M source/blender/io/usd/import/usd_reader_mesh.cc
===================================================================
diff --git a/source/blender/io/usd/import/usd_reader_mesh.cc b/source/blender/io/usd/import/usd_reader_mesh.cc
index 368699f5b28..6c961e50727 100644
--- a/source/blender/io/usd/import/usd_reader_mesh.cc
+++ b/source/blender/io/usd/import/usd_reader_mesh.cc
@@ -497,7 +497,9 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
return;
}
- std::string mtl_name = bound_mtl.GetPrim().GetName().GetString();
+ /* We have a material bound to the mesh prim. */
+
+ /* Add a material slot to the object .*/
if (!BKE_object_material_slot_add(bmain, object_)) {
std::cerr << "WARNING: Couldn't add material slot for mesh prim " << this->prim_path_
@@ -505,6 +507,10 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
return;
}
+ /* Check if a material with the same name already exists. */
+
+ std::string mtl_name = bound_mtl.GetPrim().GetName().GetString();
+
Material *mtl = static_cast<Material *>(bmain->materials.first);
Material *blen_mtl = nullptr;
@@ -517,6 +523,7 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
}
if (!blen_mtl) {
+ /* No existing material, so add it now. */
blen_mtl = BKE_material_add(bmain, mtl_name.c_str());
}
@@ -525,6 +532,7 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
<< this->prim_path_ << std::endl;
}
+ /* Set the material IDs on the polys. */
for (int p = 0; p < mesh->totpoly; ++p) {
mesh->mpoly[p].mat_nr = 0;
}
@@ -554,8 +562,9 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
std::map<std::string, Material *> blen_mtl_map;
build_mtl_map(bmain, blen_mtl_map);
- std::map<std::string, Material *>::iterator blen_mtl_iter = blen_mtl_map.begin();
-
+ /* Iterate over the USD materials and add corresponding
+ * Blender materials of the same name, if they don't
+ * already exist. */
usd_mtl_iter = usd_mtl_map.begin();
int idx = 0;
@@ -564,7 +573,8 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
std::string mtl_name = usd_mtl_iter->first.c_str();
- blen_mtl_iter = blen_mtl_map.find(mtl_name);
+ std::map<std::string, Material *>::const_iterator blen_mtl_iter = blen_mtl_map.find(
+ mtl_name);
if (blen_mtl_iter != blen_mtl_map.end()) {
blen_mtl = blen_mtl_iter->second;
@@ -584,6 +594,8 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
}
BKE_object_material_assign(bmain, object_, blen_mtl, idx + 1, BKE_MAT_ASSIGN_OBDATA);
+
+ /* Record this material's index. */
mtl_index_map.insert(std::make_pair(usd_mtl_iter->first, idx));
}
@@ -591,6 +603,7 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
for (const std::pair<pxr::UsdGeomSubset, std::string> &sub_mtl : subset_mtls) {
+ /* Find the index of the current material. */
std::map<std::string, int>::const_iterator mtl_index_iter = mtl_index_map.find(
sub_mtl.second);
@@ -600,11 +613,12 @@ void UsdMeshReader::assign_materials(Main *bmain, Mesh *mesh, double time)
}
int mtl_idx = mtl_index_iter->second;
- const pxr::UsdGeomSubset &sub = sub_mtl.first;
+ /* Query the subset membership. */
pxr::VtIntArray indices;
- sub.GetIndicesAttr().Get(&indices, time);
+ sub_mtl.first.GetIndicesAttr().Get(&indices, time);
+ /* Assign the poly material indices. */
for (int face_idx : indices) {
if (mtl_idx > mesh->totpoly) {
std::cerr << "WARNING: Out of bounds material index." << std::endl;
More information about the Bf-blender-cvs
mailing list