From 5ea31db2ce88a6f9131a362a8dea54ab57d0192d Mon Sep 17 00:00:00 2001 From: Michael Kowalski Date: Wed, 15 Mar 2023 23:33:06 -0400 Subject: [PATCH] USD import fix: set active mesh color. Fixed a bug where the active color wasn't being set on imported meshes, resulting in no colors displaying in the viewport. --- .../blender/io/usd/intern/usd_reader_mesh.cc | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/source/blender/io/usd/intern/usd_reader_mesh.cc b/source/blender/io/usd/intern/usd_reader_mesh.cc index a457c66f1f5..2e79af66544 100644 --- a/source/blender/io/usd/intern/usd_reader_mesh.cc +++ b/source/blender/io/usd/intern/usd_reader_mesh.cc @@ -449,11 +449,17 @@ void USDMeshReader::read_colors(Mesh *mesh, const double motionSampleTime) pxr::UsdGeomPrimvarsAPI primvarsAPI = pxr::UsdGeomPrimvarsAPI(mesh_prim_); - std::vector primvars = primvarsAPI.GetPrimvars(); + std::vector primvars = primvarsAPI.GetPrimvarsWithValues(); + + pxr::TfToken active_color_name; /* Convert all color primvars to custom layer data. */ for (pxr::UsdGeomPrimvar pv : primvars) { + if (!pv.HasValue()) { + continue; + } + pxr::SdfValueTypeName type = pv.GetTypeName(); if (!ELEM(type, @@ -465,6 +471,13 @@ void USDMeshReader::read_colors(Mesh *mesh, const double motionSampleTime) pxr::TfToken name = pv.GetPrimvarName(); + /* Set the active color name to 'displayColor', if a color primvar + * with this name exists. Otherwise, use the name of the first + * color primvar we find for the active color. */ + if (active_color_name.IsEmpty() || name == usdtokens::displayColor) { + active_color_name = name; + } + /* Skip if we read this primvar before and it isn't animated. */ if (primvar_varying_map_.find(name) != primvar_varying_map_.end() && !primvar_varying_map_.at(name)) { @@ -473,6 +486,10 @@ void USDMeshReader::read_colors(Mesh *mesh, const double motionSampleTime) read_colors(mesh, pv, motionSampleTime); } + + if (!active_color_name.IsEmpty()) { + BKE_id_attributes_active_color_set(&mesh->id, active_color_name.GetText()); + } } void USDMeshReader::read_colors(Mesh *mesh,