[Bf-blender-cvs] [7c6561e1c6c] usd-importer-T81257: USD importer: material assignment bug fix.

makowalski noreply at git.blender.org
Sun Dec 27 22:18:13 CET 2020


Commit: 7c6561e1c6c8627ee5d717d26ecea2bcad6dee0d
Author: makowalski
Date:   Tue Dec 15 15:56:04 2020 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rB7c6561e1c6c8627ee5d717d26ecea2bcad6dee0d

USD importer: material assignment bug fix.

Added logic to avoid assigning material indices
to instanced meshes more than once.

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

M	source/blender/io/usd/import/usd_prim_iterator.cc
M	source/blender/io/usd/import/usd_reader_mesh_base.cc
M	source/blender/io/usd/import/usd_reader_mesh_base.h

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

diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
index 92b71dafe32..398b330e91d 100644
--- a/source/blender/io/usd/import/usd_prim_iterator.cc
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -214,6 +214,8 @@ void USDPrimIterator::cache_prototype_data(USDDataCache &r_cache) const
 
   std::vector<pxr::UsdPrim> protos = stage_->GetMasters();
 
+  double time = 0.0;
+
   for (const pxr::UsdPrim &proto_prim : protos) {
     std::vector<USDXformableReader *> proto_readers;
     std::vector<USDXformableReader *> child_readers;
@@ -226,8 +228,13 @@ void USDPrimIterator::cache_prototype_data(USDDataCache &r_cache) const
         if (reader_prim) {
 
           if (USDMeshReaderBase *mesh_reader = dynamic_cast<USDMeshReaderBase *>(reader)) {
-            Mesh *proto_mesh = mesh_reader->create_mesh(bmain_, 0.0);
+            Mesh *proto_mesh = mesh_reader->create_mesh(bmain_, time);
             if (proto_mesh) {
+
+              if (this->context_.import_params.import_materials) {
+                mesh_reader->assign_materials(this->bmain_, proto_mesh, time, false);
+              }
+
               /* TODO(makowalski): Do we want to decrement the mesh's use count to 0?
                * Might have a small memory leak otherwise. Also, check if mesh is
                * already in cache before adding? */
diff --git a/source/blender/io/usd/import/usd_reader_mesh_base.cc b/source/blender/io/usd/import/usd_reader_mesh_base.cc
index 7d5eb165028..314081481a1 100644
--- a/source/blender/io/usd/import/usd_reader_mesh_base.cc
+++ b/source/blender/io/usd/import/usd_reader_mesh_base.cc
@@ -75,15 +75,20 @@ void USDMeshReaderBase::create_object(Main *bmain, double time, USDDataCache *da
   }
 
   object_ = BKE_object_add_only_object(bmain, OB_MESH, obj_name.c_str());
-  Mesh *mesh = this->read_mesh(bmain, time, data_cache);
+
+  bool is_mesh_instance = false;
+  Mesh *mesh = this->read_mesh(bmain, time, data_cache, is_mesh_instance);
   object_->data = mesh;
 
   if (this->context_.import_params.import_materials) {
-    assign_materials(bmain, mesh, time, true);
+    assign_materials(bmain, (is_mesh_instance ? nullptr : mesh), time, true);
   }
 }
 
-Mesh *USDMeshReaderBase::read_mesh(Main *bmain, double time, USDDataCache *data_cache)
+Mesh *USDMeshReaderBase::read_mesh(Main *bmain,
+                                   double time,
+                                   USDDataCache *data_cache,
+                                   bool &r_is_instance)
 {
   /* If this prim is an instance proxy and instancing is enabled,
    * return the shared mesh created by the instance prototype. */
@@ -101,6 +106,7 @@ Mesh *USDMeshReaderBase::read_mesh(Main *bmain, double time, USDDataCache *data_
       if (proto_mesh) {
         /* Increment the user count before returning. */
         id_us_plus(&proto_mesh->id);
+        r_is_instance = true;
         return proto_mesh;
       }
 
@@ -110,6 +116,7 @@ Mesh *USDMeshReaderBase::read_mesh(Main *bmain, double time, USDDataCache *data_
   }
 
   /* Not sharing the prototype mesh, so create unique mesh data. */
+  r_is_instance = false;
   return create_mesh(bmain, time);
 }
 
diff --git a/source/blender/io/usd/import/usd_reader_mesh_base.h b/source/blender/io/usd/import/usd_reader_mesh_base.h
index d56117e89fd..e0e8bba5528 100644
--- a/source/blender/io/usd/import/usd_reader_mesh_base.h
+++ b/source/blender/io/usd/import/usd_reader_mesh_base.h
@@ -39,7 +39,7 @@ class USDMeshReaderBase : public USDXformableReader {
 
   void create_object(Main *bmain, double time, USDDataCache *data_cache) override;
 
-  struct Mesh *read_mesh(Main *bmain, double time, USDDataCache *data_cache);
+  struct Mesh *read_mesh(Main *bmain, double time, USDDataCache *data_cache, bool &r_is_instance);
 
   virtual struct Mesh *create_mesh(Main *bmain, double time) = 0;



More information about the Bf-blender-cvs mailing list