Skip to content

Commit

Permalink
Merge pull request #3769 from jufrantz/issue3766/file_writers_support…
Browse files Browse the repository at this point in the history
…_envvar_expansion

Add support for env var expansions in file node writers
  • Loading branch information
seando-adsk authored May 21, 2024
2 parents f59dd15 + 24b5dc0 commit 60fd003
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
21 changes: 20 additions & 1 deletion lib/usd/translators/shading/mtlxFileTextureWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <maya/MGlobal.h>
#include <maya/MObject.h>
#include <maya/MPlug.h>
#include <maya/MRenderUtil.h>
#include <maya/MStatus.h>
#include <maya/MString.h>

Expand Down Expand Up @@ -292,11 +293,29 @@ void MtlxUsd_FileWriter::Write(const UsdTimeCode& usdTime)
return;
}

std::string fileTextureName(fileTextureNamePlug.asString(&status).asChar());
const MString rawTextureName = fileTextureNamePlug.asString(&status);
if (status != MS::kSuccess) {
return;
}

// Resolve texture path like a file node
const MString exactTextureName = MRenderUtil::exactFileTextureName(
rawTextureName,
/* useFrameExt = */ false,
/* currentFrameExt = */ MString(),
depNodeFn.absoluteName(),
&status);
if (status != MS::kSuccess) {
return;
}

std::string fileTextureName(exactTextureName.asChar(), exactTextureName.length());

// Fallback to raw name if maya resolution failed, eg file was not found
if (fileTextureName.empty()) {
fileTextureName.assign(rawTextureName.asChar(), rawTextureName.length());
}

const MPlug tilingAttr
= depNodeFn.findPlug(TrMayaTokens->uvTilingMode.GetText(), true, &status);
const bool isUDIM = (status == MS::kSuccess && tilingAttr.asInt() == 3);
Expand Down
21 changes: 20 additions & 1 deletion lib/usd/translators/shading/usdFileTextureWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include <maya/MGlobal.h>
#include <maya/MObject.h>
#include <maya/MPlug.h>
#include <maya/MRenderUtil.h>
#include <maya/MStatus.h>
#include <maya/MString.h>

Expand Down Expand Up @@ -268,11 +269,29 @@ void PxrUsdTranslators_FileTextureWriter::Write(const UsdTimeCode& usdTime)
return;
}

std::string fileTextureName(fileTextureNamePlug.asString(&status).asChar());
const MString rawTextureName = fileTextureNamePlug.asString(&status);
if (status != MS::kSuccess) {
return;
}

// Resolve texture path like a file node
const MString exactTextureName = MRenderUtil::exactFileTextureName(
rawTextureName,
/* useFrameExt = */ false,
/* currentFrameExt = */ MString(),
depNodeFn.absoluteName(),
&status);
if (status != MS::kSuccess) {
return;
}

std::string fileTextureName(exactTextureName.asChar(), exactTextureName.length());

// Fallback to raw name if maya resolution failed, eg file was not found
if (fileTextureName.empty()) {
fileTextureName.assign(rawTextureName.asChar(), rawTextureName.length());
}

const MPlug tilingAttr
= depNodeFn.findPlug(TrMayaTokens->uvTilingMode.GetText(), true, &status);
const bool isUDIM = (status == MS::kSuccess && tilingAttr.asInt() == 3);
Expand Down
44 changes: 37 additions & 7 deletions test/lib/usd/translators/testUsdExportTexture.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import unittest

import fixturesUtils
import testUtils

PROJ_ENV_VAR_NAME = "_MAYA_USD_EXPORT_TEXTURE_TEST_PATH"

class testUsdExportTexture(unittest.TestCase):

Expand Down Expand Up @@ -56,7 +58,7 @@ def tearDown(self):
# We're removing the output file only to be nice, it is not mandatory.
pass

def runTextureTest(self, relativeMode):
def runTextureTest(self, relativeMode, withEnvVar):
projectFolder = os.path.join(self.inputPath, 'UsdExportTextureTest', 'rel_project')
mayaScenePath = os.path.join(projectFolder, 'scenes', 'TextureTest.ma')
cmds.file(mayaScenePath, force=True, open=True)
Expand All @@ -65,12 +67,16 @@ def runTextureTest(self, relativeMode):
cmds.workspace(projectFolder, openWorkspace=True)

# Prepare texture path to be part of the project folder.
texturePath = os.path.join(projectFolder, 'sourceimages', 'grid.png')
# build the absolute texturePath, with an env var if requested
textureRoot = ('$' + PROJ_ENV_VAR_NAME) if withEnvVar else projectFolder
texturePath = os.path.join(textureRoot, 'sourceimages', 'grid.png')
cmds.setAttr('file1.ftn', texturePath, edit=True, type='string')

# Export to USD.
# Export to USD, ensuring envvar is set if it is used by texture path
usdFilePath = self.getUsdFilePath()
cmds.mayaUSDExport(mergeTransformAndShape=True, file=usdFilePath, exportRelativeTextures=relativeMode)

with testUtils.TemporaryEnvironmentVariable(PROJ_ENV_VAR_NAME, projectFolder):
cmds.mayaUSDExport(mergeTransformAndShape=True, file=usdFilePath, exportRelativeTextures=relativeMode)

stage = Usd.Stage.Open(usdFilePath)
self.assertTrue(stage, usdFilePath)
Expand All @@ -88,24 +94,48 @@ def runTextureTest(self, relativeMode):
self.assertEqual(os.path.isabs(exportedTexturePath.path), shouldBeAbsolute,
'The exported texture %s does not have the right relative mode' % exportedTexturePath)

self.assertTrue(os.path.exists(exportedTexturePath.resolvedPath),
'The exported texture %s is not resolved by USD' % exportedTexturePath)

def testExportRelativeTexture(self):
'''
Test that texture can be exported with relative paths for textures.
'''
self.runTextureTest('relative')
self.runTextureTest('relative', withEnvVar=False)

def testExportAbsoluteTexture(self):
'''
Test that texture can be exported with absolute paths for textures.
'''
self.runTextureTest('absolute')
self.runTextureTest('absolute', withEnvVar=False)

def testExportAutomaticTexture(self):
'''
Test that texture can be exported with automatic reltive or absolute
paths for textures.
'''
self.runTextureTest('automatic')
self.runTextureTest('automatic', withEnvVar=False)

def testExportRelativeTextureWithEnvVar(self):
'''
Test that texture path with an environment variable expansion can be
exported with relative paths for textures.
'''
self.runTextureTest('relative', withEnvVar=True)

def testExportAbsoluteTextureWithEnvVar(self):
'''
Test that texture path with an environment variable expansion can be
exported with absolute paths for textures.
'''
self.runTextureTest('absolute', withEnvVar=True)

def testExportAutomaticTextureWithEnvVar(self):
'''
Test that texture path with an environment variable expansion can be
exported with automatic relative or absolute paths for textures.
'''
self.runTextureTest('automatic', withEnvVar=True)


if __name__ == '__main__':
Expand Down

0 comments on commit 60fd003

Please sign in to comment.