[Bf-blender-cvs] [c9622636fad] usd-importer-T81257-merge: USD Import: import display colors.

makowalski noreply at git.blender.org
Wed Mar 10 03:02:24 CET 2021


Commit: c9622636fad312c8635675c32051fa1fdd34de7b
Author: makowalski
Date:   Tue Mar 9 20:55:48 2021 -0500
Branches: usd-importer-T81257-merge
https://developer.blender.org/rBc9622636fad312c8635675c32051fa1fdd34de7b

USD Import: import display colors.

Added support for reading the display color primvars.  This
option is off by default in the mesh read flags.

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

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

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

diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c
index 60bbfda1cc0..582e14e04a9 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -587,7 +587,8 @@ void WM_OT_usd_import(struct wmOperatorType *ot)
                       "Set read flag for all usd import mesh sequence cache modifiers");
 
   RNA_def_property_flag(prop, PROP_ENUM_FLAG);
-  RNA_def_property_enum_default(prop, MOD_MESHSEQ_READ_ALL);
+  RNA_def_property_enum_default(
+      prop, (MOD_MESHSEQ_READ_VERT | MOD_MESHSEQ_READ_POLY | MOD_MESHSEQ_READ_UV));
 
   RNA_def_string(ot->srna,
                  "prim_path_mask",
diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc
index 3daaa1c07a0..e591ed859e5 100644
--- a/source/blender/io/usd/intern/usd_reader_mesh.cc
+++ b/source/blender/io/usd/intern/usd_reader_mesh.cc
@@ -546,6 +546,90 @@ void USDMeshReader::read_vels(Mesh *mesh,
   }
 }
 
+void USDMeshReader::read_colors(Mesh *mesh,
+                                const pxr::UsdGeomMesh &mesh_prim,
+                                double motionSampleTime)
+{
+  if (!(mesh && mesh_prim && mesh->totloop > 0)) {
+    return;
+  }
+
+  pxr::UsdGeomPrimvar color_primvar = mesh_prim.GetDisplayColorPrimvar();
+
+  if (!color_primvar.HasValue()) {
+    return;
+  }
+
+  pxr::TfToken interp = color_primvar.GetInterpolation();
+
+  if (interp == pxr::UsdGeomTokens->varying) {
+    std::cerr << "WARNING: Unsupported varying interpolation for display colors\n" << std::endl;
+    return;
+  }
+
+  pxr::VtArray<pxr::GfVec3f> display_colors;
+
+  if (!color_primvar.ComputeFlattened(&display_colors)) {
+    std::cerr << "WARNING: Couldn't compute display colors\n" << std::endl;
+    return;
+  }
+
+  if ((interp == pxr::UsdGeomTokens->faceVarying && display_colors.size() != mesh->totloop) ||
+      (interp == pxr::UsdGeomTokens->vertex && display_colors.size() != mesh->totvert) ||
+      (interp == pxr::UsdGeomTokens->constant && display_colors.size() != 1) ||
+      (interp == pxr::UsdGeomTokens->uniform && display_colors.size() != mesh->totpoly)) {
+    std::cerr << "WARNING: display colors count mismatch\n" << std::endl;
+    return;
+  }
+
+  void *cd_ptr = add_customdata_cb(mesh, "displayColors", CD_MLOOPCOL);
+
+  if (!cd_ptr) {
+    std::cerr << "WARNING: Couldn't add displayColors custom data.\n";
+    return;
+  }
+
+  MLoopCol *colors = static_cast<MLoopCol *>(cd_ptr);
+
+  mesh->mloopcol = colors;
+
+  MPoly *poly = mesh->mpoly;
+
+  for (int i = 0, e = mesh->totpoly; i < e; ++i, ++poly) {
+    for (int j = 0; j < poly->totloop; ++j) {
+      int loop_index = poly->loopstart + j;
+
+      int usd_index = 0;  // Default for constant varying interpolation.
+
+      if (interp == pxr::UsdGeomTokens->vertex) {
+        usd_index = mesh->mloop[loop_index].v;
+      }
+      else if (interp == pxr::UsdGeomTokens->faceVarying) {
+        usd_index = poly->loopstart;
+        if (m_isLeftHanded) {
+          usd_index += poly->totloop - 1 - j;
+        }
+        else {
+          usd_index += j;
+        }
+      }
+      else if (interp == pxr::UsdGeomTokens->uniform) {
+        // Uniform varying uses the poly index.
+        usd_index = i;
+      }
+
+      if (usd_index >= display_colors.size()) {
+        continue;
+      }
+
+      colors[loop_index].r = unit_float_to_uchar_clamp(display_colors[usd_index][0]);
+      colors[loop_index].g = unit_float_to_uchar_clamp(display_colors[usd_index][1]);
+      colors[loop_index].b = unit_float_to_uchar_clamp(display_colors[usd_index][2]);
+      colors[loop_index].a = unit_float_to_uchar_clamp(1.0);
+    }
+  }
+}
+
 void USDMeshReader::process_normals_vertex_varying(Mesh *mesh)
 {
   if (m_normals.empty()) {
@@ -722,6 +806,10 @@ void USDMeshReader::read_mesh_sample(const std::string &iobject_full_name,
   if ((settings->read_flag & MOD_MESHSEQ_READ_VELS) != 0) {
     read_vels(mesh, mesh_prim, settings->vel_scale, motionSampleTime);
   }
+
+  if ((settings->read_flag & MOD_MESHSEQ_READ_COLOR) != 0) {
+    read_colors(mesh, mesh_prim, motionSampleTime);
+  }
 }
 
 void USDMeshReader::assign_facesets_to_mpoly(double motionSampleTime,
diff --git a/source/blender/io/usd/intern/usd_reader_mesh.h b/source/blender/io/usd/intern/usd_reader_mesh.h
index 50ee6fa4563..683be456aad 100644
--- a/source/blender/io/usd/intern/usd_reader_mesh.h
+++ b/source/blender/io/usd/intern/usd_reader_mesh.h
@@ -64,6 +64,7 @@ class USDMeshReader : public USDGeomReader {
                 bool load_uvs = false);
   void read_attributes(Mesh *mesh, pxr::UsdGeomMesh mesh_prim, double motionSampleTime);
   void read_vels(Mesh *mesh, pxr::UsdGeomMesh mesh_prim, float vel_scale, double motionSampleTime);
+  void read_colors(Mesh *mesh, const pxr::UsdGeomMesh &mesh_prim, double motionSampleTime);
 
   void read_mesh_sample(const std::string &iobject_full_name,
                         ImportSettings *settings,



More information about the Bf-blender-cvs mailing list