From 245a537888d06dc7bafe220eba8fcfc9f0649ec0 Mon Sep 17 00:00:00 2001 From: Pierre Baillargeon Date: Tue, 17 May 2022 10:27:36 -0400 Subject: [PATCH] MAYA-122926 turn off auto-edit when caching - At the end of caching, turn off auto-edit. - Refactor turning off auto-edit in a common function. - Add a auto-edit caching unit test that failed before the fix and passes now. --- lib/usd/translators/mayaReferenceUpdater.cpp | 22 ++++-- test/lib/mayaUsd/fileio/testCacheToUsd.py | 82 +++++++++++++++++++- 2 files changed, 95 insertions(+), 9 deletions(-) diff --git a/lib/usd/translators/mayaReferenceUpdater.cpp b/lib/usd/translators/mayaReferenceUpdater.cpp index cde61e4c71..ef2f24de43 100644 --- a/lib/usd/translators/mayaReferenceUpdater.cpp +++ b/lib/usd/translators/mayaReferenceUpdater.cpp @@ -47,6 +47,17 @@ #include namespace { + +void clearAutoEdit(const UsdPrim& prim) +{ + if (prim.IsValid()) { + UsdAttribute mayaAutoEditAttr = prim.GetAttribute(MayaUsd_SchemasTokens->mayaAutoEdit); + if (mayaAutoEditAttr.IsValid()) { + mayaAutoEditAttr.Set(false); + } + } +} + std::string findValue(const PXR_NS::VtDictionary& routingData, const PXR_NS::TfToken& key) { auto found = routingData.find(key); @@ -233,13 +244,7 @@ bool PxrUsdTranslators_MayaReferenceUpdater::discardEdits() if (PrimUpdaterManager::readPullInformation(dagPath, pulledPath)) { // Reset the auto-edit when discarding the edit. UsdPrim prim = MayaUsd::ufe::ufePathToPrim(pulledPath); - if (prim.IsValid()) { - UsdAttribute mayaAutoEditAttr - = prim.GetAttribute(MayaUsd_SchemasTokens->mayaAutoEdit); - if (mayaAutoEditAttr.IsValid()) { - mayaAutoEditAttr.Set(false); - } - } + clearAutoEdit(prim); } } @@ -266,6 +271,9 @@ bool PxrUsdTranslators_MayaReferenceUpdater::pushEnd() MayaUsd::LockNodesUndoItem::lock( "Maya reference pulled transform unlocking", transformPath, false); + // Clear the auto-edit flag. + clearAutoEdit(getUsdPrim()); + return true; } diff --git a/test/lib/mayaUsd/fileio/testCacheToUsd.py b/test/lib/mayaUsd/fileio/testCacheToUsd.py index 0d6a9dd3e2..6361012447 100644 --- a/test/lib/mayaUsd/fileio/testCacheToUsd.py +++ b/test/lib/mayaUsd/fileio/testCacheToUsd.py @@ -101,12 +101,26 @@ def setUpClass(cls): def tearDownClass(cls): standalone.uninitialize() + def getCacheFileName(self): + return 'testCacheToUsd.usda' + + def removeCacheFile(self): + ''' + Remove the cache file if it exists. Ignore error if it does not exists. + ''' + try: + os.remove(self.getCacheFileName()) + except: + pass + def setUp(self): # Start each test with a new scene with empty stage. cmds.file(new=True, force=True) import mayaUsd_createStageWithNewLayer self.proxyShapePathStr = mayaUsd_createStageWithNewLayer.createStageWithNewLayer() self.stage = mayaUsd.lib.GetPrim(self.proxyShapePathStr).GetStage() + # Make sure the cache file is removed in case a previous test failed mid-way. + self.removeCacheFile() def runTestCacheToUsd(self, createMayaRefPrimFn, checkCacheParentFn): '''Cache edits on a pulled Maya reference prim test engine.''' @@ -176,7 +190,7 @@ def runTestCacheToUsd(self, createMayaRefPrimFn, checkCacheParentFn): # Cache to USD, using a payload composition arc. defaultExportOptions = cacheToUsd.getDefaultExportOptions() - cacheFile = 'testCacheToUsd.usda' + cacheFile = self.getCacheFileName() cachePrimName = 'cachePrimName' payloadOrReference = 'Payload' listEditType = 'Prepend' @@ -237,7 +251,8 @@ def runTestCacheToUsd(self, createMayaRefPrimFn, checkCacheParentFn): self.assertTrue(foundPayload) # Clean up - os.remove(cacheFile) + self.removeCacheFile() + def testCacheToUsdSibling(self): self.runTestCacheToUsd(createMayaRefPrimSiblingCache, checkSiblingCacheParent) @@ -245,5 +260,68 @@ def testCacheToUsdSibling(self): def testCacheToUsdVariant(self): self.runTestCacheToUsd(createMayaRefPrimVariantCache, checkVariantCacheParent) + + def testAutoEditAndCache(self): + '''Test editing then caching a Maya Reference. + + Add a Maya Reference using auto-edit, then cache the edits. + ''' + kDefaultPrimName = mayaRefUtils.defaultMayaReferencePrimName() + + # Since this is a brand new prim, it should not have variant sets. + primTestDefault = self.stage.DefinePrim('/Test_Default', 'Xform') + primPathStr = self.proxyShapePathStr + ',/Test_Default' + self.assertFalse(primTestDefault.HasVariantSets()) + + variantSetName = 'new_variant_set' + variantName = 'new_variant' + + mayaRefPrim = mayaUsdAddMayaReference.createMayaReferencePrim( + primPathStr, + self.mayaSceneStr, + self.kDefaultNamespace, + variantSet=(variantSetName, variantName), + mayaAutoEdit=True) + + # The prim should have variant set. + self.assertTrue(primTestDefault.HasVariantSets()) + + # Verify that a Maya Reference prim was created. + self.assertTrue(mayaRefPrim.IsValid()) + self.assertEqual(str(mayaRefPrim.GetName()), kDefaultPrimName) + self.assertEqual(mayaRefPrim, primTestDefault.GetChild(kDefaultPrimName)) + self.assertTrue(mayaRefPrim.GetPrimTypeInfo().GetTypeName(), 'MayaReference') + + attr = mayaRefPrim.GetAttribute('mayaAutoEdit') + self.assertTrue(attr.IsValid()) + self.assertEqual(attr.Get(), True) + + # Cache to USD, using a payload composition arc. + defaultExportOptions = cacheToUsd.getDefaultExportOptions() + cacheFile = self.getCacheFileName() + cachePrimName = 'cachePrimName' + payloadOrReference = 'Payload' + listEditType = 'Prepend' + + userArgs = cacheToUsd.createCacheCreationOptions( + defaultExportOptions, cacheFile, cachePrimName, payloadOrReference, + listEditType, variantSetName, variantName) + + # Merge Maya edits. + aMayaItem = ufe.GlobalSelection.get().front() + aMayaPath = aMayaItem.path() + aMayaPathStr = ufe.PathString.string(aMayaPath) + + with mayaUsd.lib.OpUndoItemList(): + self.assertTrue(mayaUsd.lib.PrimUpdaterManager.mergeToUsd(aMayaPathStr, userArgs)) + + # Verify that the auto-edit has been turned off + attr = mayaRefPrim.GetAttribute('mayaAutoEdit') + self.assertTrue(attr.IsValid()) + self.assertEqual(attr.Get(), False) + + self.removeCacheFile() + + if __name__ == '__main__': unittest.main(verbosity=2)