From 56dbe81b4628ce8d7fab310e68b2d167172488ac Mon Sep 17 00:00:00 2001 From: JGamache-autodesk Date: Wed, 21 Oct 2020 17:26:12 -0400 Subject: [PATCH 1/2] MAYA-106892 - Export and import color space information on file nodes On export, if the color space of a file node was set (differs from the global rule) then we export it as a colorSpace metadata on the file UsdShadeInput. On import, if there is a colorSpace on the USD file input, we set it in the Maya file node. --- .../shading/usdFileTextureWriter.cpp | 22 ++++++++++++++----- .../shading/usdUVTextureReader.cpp | 10 +++++++++ ...tUsdExportImportRoundtripPreviewSurface.py | 2 ++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/usd/translators/shading/usdFileTextureWriter.cpp b/lib/usd/translators/shading/usdFileTextureWriter.cpp index bd6f7cf518..7dd11a7353 100644 --- a/lib/usd/translators/shading/usdFileTextureWriter.cpp +++ b/lib/usd/translators/shading/usdFileTextureWriter.cpp @@ -37,6 +37,7 @@ #include #include +#include #include #include #include @@ -72,6 +73,7 @@ TF_DEFINE_PRIVATE_TOKENS( (alphaOffset) (colorGain) (colorOffset) + (colorSpace) (defaultColor) (fileTextureName) (outAlpha) @@ -221,6 +223,11 @@ PxrUsdTranslators_FileTextureWriter::Write(const UsdTimeCode& usdTime) return; } + MString colorRuleCmd; + colorRuleCmd.format( + "colorManagementFileRules -evaluate \"^1s\";", fileTextureNamePlug.asString()); + const MString colorSpaceByRule = MGlobal::executeCommandStringResult(colorRuleCmd); + // WARNING: This extremely minimal attempt at making the file path relative // to the USD stage is a stopgap measure intended to provide // minimal interop. It will be replaced by proper use of Maya and @@ -233,11 +240,16 @@ PxrUsdTranslators_FileTextureWriter::Write(const UsdTimeCode& usdTime) fileTextureName = relativePath.generic_string(); } - shaderSchema.CreateInput( - _tokens->file, - SdfValueTypeNames->Asset).Set( - SdfAssetPath(fileTextureName.c_str()), - usdTime); + UsdShadeInput fileInput = shaderSchema.CreateInput(_tokens->file, SdfValueTypeNames->Asset); + fileInput.Set(SdfAssetPath(fileTextureName.c_str()), usdTime); + + MPlug colorSpacePlug = depNodeFn.findPlug(_tokens->colorSpace.GetText(), true, &status); + if (status == MS::kSuccess) { + const MString colorSpace(colorSpacePlug.asString(&status)); + if (status == MS::kSuccess && colorSpace != colorSpaceByRule) { + fileInput.GetAttr().SetColorSpace(TfToken(colorSpace.asChar())); + } + } // The Maya file node's 'colorGain' and 'alphaGain' attributes map to the // UsdUVTexture's scale input. diff --git a/lib/usd/translators/shading/usdUVTextureReader.cpp b/lib/usd/translators/shading/usdUVTextureReader.cpp index a79a022cc5..49fecdcf96 100644 --- a/lib/usd/translators/shading/usdUVTextureReader.cpp +++ b/lib/usd/translators/shading/usdUVTextureReader.cpp @@ -60,6 +60,7 @@ TF_DEFINE_PRIVATE_TOKENS( (alphaOffset) (colorGain) (colorOffset) + (colorSpace) (defaultColor) (fileTextureName) (outAlpha) @@ -190,6 +191,15 @@ bool PxrMayaUsdUVTexture_Reader::Read(UsdMayaPrimReaderContext* context) if (status == MS::kSuccess) { UsdMayaReadUtil::SetMayaAttr(mayaAttr, val); } + + // colorSpace: + if (usdInput.GetAttr().HasColorSpace()) { + MString colorSpace = usdInput.GetAttr().GetColorSpace().GetText(); + mayaAttr = depFn.findPlug(_tokens->colorSpace.GetText(), true, &status); + if (status == MS::kSuccess) { + mayaAttr.setString(colorSpace); + } + } } // The Maya file node's 'colorGain' and 'alphaGain' attributes map to the diff --git a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py index c824cf2d3e..77152da21f 100644 --- a/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py +++ b/test/lib/usd/translators/testUsdExportImportRoundtripPreviewSurface.py @@ -86,6 +86,7 @@ def testUsdPreviewSurfaceRoundtrip(self): txfile = os.path.join("UsdExportImportRoundtripPreviewSurface", "Brazilian_rosewood_pxr128.png") cmds.setAttr(file_node+".fileTextureName", txfile, type="string") + cmds.setAttr(file_node+".colorSpace", "ACEScg", type="string") cmds.setAttr(file_node + ".defaultColor", 0.5, 0.25, 0.125, type="double3") @@ -135,6 +136,7 @@ def testUsdPreviewSurfaceRoundtrip(self): self.assertTrue(cmds.getAttr("usdPreviewSurface2.useSpecularWorkflow")) self.assertEqual(cmds.getAttr("file2.defaultColor"), [(0.5, 0.25, 0.125)]) + self.assertEqual(cmds.getAttr(file_node+".colorSpace"), "ACEScg") original_path = cmds.getAttr(file_node+".fileTextureName") imported_path = cmds.getAttr("file2.fileTextureName") # imported path will be absolute: From c57206c50876afe9f77e24cf6f76f291b1678257 Mon Sep 17 00:00:00 2001 From: JGamache-autodesk Date: Thu, 22 Oct 2020 15:16:18 -0400 Subject: [PATCH 2/2] MAYA-106892 - Moved command closer to colorSpace code --- lib/usd/translators/shading/usdFileTextureWriter.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/usd/translators/shading/usdFileTextureWriter.cpp b/lib/usd/translators/shading/usdFileTextureWriter.cpp index 7dd11a7353..a9f7f7f759 100644 --- a/lib/usd/translators/shading/usdFileTextureWriter.cpp +++ b/lib/usd/translators/shading/usdFileTextureWriter.cpp @@ -223,11 +223,6 @@ PxrUsdTranslators_FileTextureWriter::Write(const UsdTimeCode& usdTime) return; } - MString colorRuleCmd; - colorRuleCmd.format( - "colorManagementFileRules -evaluate \"^1s\";", fileTextureNamePlug.asString()); - const MString colorSpaceByRule = MGlobal::executeCommandStringResult(colorRuleCmd); - // WARNING: This extremely minimal attempt at making the file path relative // to the USD stage is a stopgap measure intended to provide // minimal interop. It will be replaced by proper use of Maya and @@ -245,6 +240,10 @@ PxrUsdTranslators_FileTextureWriter::Write(const UsdTimeCode& usdTime) MPlug colorSpacePlug = depNodeFn.findPlug(_tokens->colorSpace.GetText(), true, &status); if (status == MS::kSuccess) { + MString colorRuleCmd; + colorRuleCmd.format( + "colorManagementFileRules -evaluate \"^1s\";", fileTextureNamePlug.asString()); + const MString colorSpaceByRule(MGlobal::executeCommandStringResult(colorRuleCmd)); const MString colorSpace(colorSpacePlug.asString(&status)); if (status == MS::kSuccess && colorSpace != colorSpaceByRule) { fileInput.GetAttr().SetColorSpace(TfToken(colorSpace.asChar()));