From 476794eedf4e46f756ec74f487041e22024ff25e Mon Sep 17 00:00:00 2001 From: Koen Vroeijenstijn Date: Sat, 13 Mar 2021 20:16:35 -0800 Subject: [PATCH 1/3] Adding support for indexed normals on import #1029 --- .../fileio/translators/translatorMesh.cpp | 16 ++++++++-- .../translators/UsdImportMeshTest/Mesh.usda | 29 +++++++++++++++++++ test/lib/usd/translators/testUsdImportMesh.py | 3 ++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/mayaUsd/fileio/translators/translatorMesh.cpp b/lib/mayaUsd/fileio/translators/translatorMesh.cpp index 7acc2f5ccb..7c5aba3052 100644 --- a/lib/mayaUsd/fileio/translators/translatorMesh.cpp +++ b/lib/mayaUsd/fileio/translators/translatorMesh.cpp @@ -120,6 +120,7 @@ TranslatorMeshRead::TranslatorMeshRead( // timeInterval or default. VtVec3fArray points; VtVec3fArray normals; + TfToken normals_interpolation; UsdTimeCode pointsTimeSample = UsdTimeCode::EarliestTime(); UsdTimeCode normalsTimeSample = UsdTimeCode::EarliestTime(); std::vector pointsTimeSamples; @@ -139,7 +140,18 @@ TranslatorMeshRead::TranslatorMeshRead( } mesh.GetPointsAttr().Get(&points, pointsTimeSample); - mesh.GetNormalsAttr().Get(&normals, normalsTimeSample); + + /* If 'normals' and 'primvars:normals' are both specified, the latter has precedence. */ + UsdGeomPrimvar primvar = mesh.GetPrimvar(UsdGeomTokens->normals); + + if (primvar.HasValue()) { + primvar.ComputeFlattened(&normals, normalsTimeSample); + normals_interpolation = primvar.GetInterpolation(); + } + else { + mesh.GetNormalsAttr().Get(&normals, normalsTimeSample); + normals_interpolation = mesh.GetNormalsInterpolation(); + } if (points.empty()) { TF_RUNTIME_ERROR( @@ -221,7 +233,7 @@ TranslatorMeshRead::TranslatorMeshRead( TfToken subdScheme; if (mesh.GetSubdivisionSchemeAttr().Get(&subdScheme) && subdScheme == UsdGeomTokens->none) { if (normals.size() == static_cast(meshFn.numFaceVertices()) - && mesh.GetNormalsInterpolation() == UsdGeomTokens->faceVarying) { + && normals_interpolation == UsdGeomTokens->faceVarying) { UsdMayaMeshReadUtils::setEmitNormalsTag(meshFn, true); } } else { diff --git a/test/lib/usd/translators/UsdImportMeshTest/Mesh.usda b/test/lib/usd/translators/UsdImportMeshTest/Mesh.usda index fc83b762ad..bfc2bdddb7 100644 --- a/test/lib/usd/translators/UsdImportMeshTest/Mesh.usda +++ b/test/lib/usd/translators/UsdImportMeshTest/Mesh.usda @@ -81,6 +81,35 @@ def Xform "World" uniform token subdivisionScheme = "none" } + def Mesh "IndexedNormalsMesh" ( + kind = "component" + ) + { + float3[] extent = [(1.5, 1.5, 1.5), (2.5, 2.5, 2.5)] + int[] faceVertexCounts = [4, 4, 4, 4, 4, 4] + int[] faceVertexIndices = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 1, 0, 1, 7, 5, 3, 6, 0, 2, 4] + token interpolateBoundary = "none" + + normal3f[] primvars:normals = [(0, 0, 1), (0, 1, 0), (0, 0, -1), (0, -1, 0) , (1, 0, 0), (-1, 0, 0)] ( + interpolation = "faceVarying" + ) + int[] primvars:normals:indices = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5] + + point3f[] points = [(2.5, 2.5, 3.5), (3.5, 2.5, 3.5), (2.5, 3.5, 3.5), (3.5, 3.5, 3.5), (2.5, 3.5, 2.5), (3.5, 3.5, 2.5), (2.5, 2.5, 2.5), (3.5, 2.5, 2.5)] + color3f[] primvars:displayColor = [(0.13320851, 0.13320851, 0.13320851)] ( + customData = { + dictionary Maya = { + bool generated = 1 + } + } + ) + texCoord2f[] primvars:st = [(0.375, 0), (0.625, 0), (0.625, 0.25), (0.375, 0.25), (0.625, 0.5), (0.375, 0.5), (0.625, 0.75), (0.375, 0.75), (0.625, 1), (0.375, 1), (0.875, 0), (0.875, 0.25), (0.125, 0), (0.125, 0.25)] ( + interpolation = "faceVarying" + ) + int[] primvars:st:indices = [0, 1, 2, 3, 3, 2, 4, 5, 5, 4, 6, 7, 7, 6, 8, 9, 1, 10, 11, 2, 12, 0, 3, 13] + uniform token subdivisionScheme = "none" + } + def Mesh "LeftHandedPolyMesh" ( kind = "component" ) diff --git a/test/lib/usd/translators/testUsdImportMesh.py b/test/lib/usd/translators/testUsdImportMesh.py index fc003c7af5..540f729b16 100644 --- a/test/lib/usd/translators/testUsdImportMesh.py +++ b/test/lib/usd/translators/testUsdImportMesh.py @@ -62,6 +62,9 @@ def verifyPolyMeshCommonAttributes(self, mesh): def testImportPoly(self): self.verifyPolyMeshCommonAttributes('PolyMeshShape') + def testImportIndexedNormals(self): + self.verifyPolyMeshCommonAttributes('IndexedNormalsMeshShape') + def testImportLeftHandedPoly(self): self.verifyPolyMeshCommonAttributes('LeftHandedPolyMeshShape') From 66e16b7687103bdf1c2b8aba18e59c12e852a29b Mon Sep 17 00:00:00 2001 From: koen Date: Sun, 14 Mar 2021 09:25:26 -0700 Subject: [PATCH 2/3] Applying clang formatting --- lib/mayaUsd/fileio/translators/translatorMesh.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/mayaUsd/fileio/translators/translatorMesh.cpp b/lib/mayaUsd/fileio/translators/translatorMesh.cpp index 7c5aba3052..06e07b49e6 100644 --- a/lib/mayaUsd/fileio/translators/translatorMesh.cpp +++ b/lib/mayaUsd/fileio/translators/translatorMesh.cpp @@ -140,17 +140,16 @@ TranslatorMeshRead::TranslatorMeshRead( } mesh.GetPointsAttr().Get(&points, pointsTimeSample); - + /* If 'normals' and 'primvars:normals' are both specified, the latter has precedence. */ UsdGeomPrimvar primvar = mesh.GetPrimvar(UsdGeomTokens->normals); - + if (primvar.HasValue()) { - primvar.ComputeFlattened(&normals, normalsTimeSample); - normals_interpolation = primvar.GetInterpolation(); - } - else { - mesh.GetNormalsAttr().Get(&normals, normalsTimeSample); - normals_interpolation = mesh.GetNormalsInterpolation(); + primvar.ComputeFlattened(&normals, normalsTimeSample); + normals_interpolation = primvar.GetInterpolation(); + } else { + mesh.GetNormalsAttr().Get(&normals, normalsTimeSample); + normals_interpolation = mesh.GetNormalsInterpolation(); } if (points.empty()) { From 24cb0119e7e89f8e793ebaa685719ed71a079c2b Mon Sep 17 00:00:00 2001 From: koen Date: Mon, 15 Mar 2021 08:41:41 -0700 Subject: [PATCH 3/3] changing normals_interpolation variable from snakecase to camelCase --- lib/mayaUsd/fileio/translators/translatorMesh.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/mayaUsd/fileio/translators/translatorMesh.cpp b/lib/mayaUsd/fileio/translators/translatorMesh.cpp index 06e07b49e6..c6aa0bd001 100644 --- a/lib/mayaUsd/fileio/translators/translatorMesh.cpp +++ b/lib/mayaUsd/fileio/translators/translatorMesh.cpp @@ -120,7 +120,7 @@ TranslatorMeshRead::TranslatorMeshRead( // timeInterval or default. VtVec3fArray points; VtVec3fArray normals; - TfToken normals_interpolation; + TfToken normalsInterpolation; UsdTimeCode pointsTimeSample = UsdTimeCode::EarliestTime(); UsdTimeCode normalsTimeSample = UsdTimeCode::EarliestTime(); std::vector pointsTimeSamples; @@ -146,10 +146,10 @@ TranslatorMeshRead::TranslatorMeshRead( if (primvar.HasValue()) { primvar.ComputeFlattened(&normals, normalsTimeSample); - normals_interpolation = primvar.GetInterpolation(); + normalsInterpolation = primvar.GetInterpolation(); } else { mesh.GetNormalsAttr().Get(&normals, normalsTimeSample); - normals_interpolation = mesh.GetNormalsInterpolation(); + normalsInterpolation = mesh.GetNormalsInterpolation(); } if (points.empty()) { @@ -232,7 +232,7 @@ TranslatorMeshRead::TranslatorMeshRead( TfToken subdScheme; if (mesh.GetSubdivisionSchemeAttr().Get(&subdScheme) && subdScheme == UsdGeomTokens->none) { if (normals.size() == static_cast(meshFn.numFaceVertices()) - && normals_interpolation == UsdGeomTokens->faceVarying) { + && normalsInterpolation == UsdGeomTokens->faceVarying) { UsdMayaMeshReadUtils::setEmitNormalsTag(meshFn, true); } } else {