diff --git a/lib/mayaUsd/python/wrapUtil.cpp b/lib/mayaUsd/python/wrapUtil.cpp index fe613cd937..bc5a284b3f 100644 --- a/lib/mayaUsd/python/wrapUtil.cpp +++ b/lib/mayaUsd/python/wrapUtil.cpp @@ -55,5 +55,6 @@ void wrapUtil() .def("getDictionaryFromEncodedOptions", getDictionaryFromEncodedOptions) .def( "getPathRelativeToMayaSceneFile", - &UsdMayaUtilFileSystem::getPathRelativeToMayaSceneFile); + UsdMayaUtilFileSystem::getPathRelativeToMayaSceneFile) + .staticmethod("getPathRelativeToMayaSceneFile"); } diff --git a/test/lib/testMayaUsdCreateStageCommands.py b/test/lib/testMayaUsdCreateStageCommands.py index 901a207aa5..ae6df68303 100644 --- a/test/lib/testMayaUsdCreateStageCommands.py +++ b/test/lib/testMayaUsdCreateStageCommands.py @@ -53,6 +53,11 @@ def testCreateStageWithNewLayer(self): self.assertTrue(cmds.isConnected('time1.outTime', shapeNode+'.time')) def testCreateStageFromFile(self): + # Open top_layer.ma and make sure to have USD stage paths as absolute + mayaSceneFilePath = testUtils.getTestScene("ballset", "StandaloneScene", "top_layer.ma") + cmds.file(mayaSceneFilePath, force=True, open=True) + cmds.optionVar(iv=('mayaUsd_MakePathRelativeToSceneFile', 0)) + # We cannot directly call the 'mayaUsdCreateStageFromFile' # as it opens a file dialog to choose the scene. So instead # we can call what it does once the file is choose. @@ -72,3 +77,14 @@ def testCreateStageFromFile(self): # Verify that the shape node is connected to time. self.assertTrue(cmds.isConnected('time1.outTime', shapeNode+'.time')) + + # Now switch to having USD stage paths as relative to Maya scene file + cmds.optionVar(iv=('mayaUsd_MakePathRelativeToSceneFile', 1)) + + # Create the same stage and verify that now it's open as relative + shapeNodeRel = mel.eval('mayaUsd_createStageFromFilePath(\"'+ballFilePath+'\")') + filePathAttrRel = cmds.getAttr(shapeNodeRel+'.filePath') + self.assertEqual(filePathAttrRel, 'top_layer.usda') + + # Restore mayaUsd_MakePathRelativeToSceneFile + cmds.optionVar(iv=('mayaUsd_MakePathRelativeToSceneFile', 0)) \ No newline at end of file diff --git a/test/lib/testMayaUsdLayerEditorCommands.py b/test/lib/testMayaUsdLayerEditorCommands.py index b1317d1847..8b6c8b67f7 100644 --- a/test/lib/testMayaUsdLayerEditorCommands.py +++ b/test/lib/testMayaUsdLayerEditorCommands.py @@ -18,6 +18,7 @@ import unittest import tempfile +import testUtils from os import path from maya import cmds, mel import mayaUsd_createStageWithNewLayer @@ -557,3 +558,22 @@ def addFileBakedLayerByPath(parentLayer): testMuteLayerImpl(addAnonymousLayer) testMuteLayerImpl(addFileBakedLayer) testMuteLayerImpl(addFileBakedLayerByPath) + + def testPathRelativeToMayaSceneFile(self): + # Function mayaUsdLib.Util.getPathRelativeToMayaSceneFile is used during + # USD root layer saving/loading, and since we cannot test the full workflow + # as it opens a file dialog and requires user input, we make sure to test + # the function itself here. + + ballFilePath = path.normpath(testUtils.getTestScene('ballset', 'StandaloneScene', 'top_layer.usda')).replace('\\', '/') + + # Without Maya scene file, the absolute path is returned + cmds.file(newFile=True, force=True) + filePathAbs = mayaUsd.lib.Util.getPathRelativeToMayaSceneFile(ballFilePath) + self.assertEqual(filePathAbs, ballFilePath) + + # With Maya scene file, the relative path is returned + mayaSceneFilePath = testUtils.getTestScene("ballset", "StandaloneScene", "top_layer.ma") + cmds.file(mayaSceneFilePath, force=True, open=True) + filePathRel = mayaUsd.lib.Util.getPathRelativeToMayaSceneFile(ballFilePath) + self.assertEqual(filePathRel, 'top_layer.usda')