Skip to content

Commit

Permalink
EMSUSD-1758 author metadata only on anonymous root layers
Browse files Browse the repository at this point in the history
- Only author the up-axis and units metadata when saving a root layer on-disk.
- Update the unit test to verify this.
  • Loading branch information
pierrebai-adsk committed Oct 24, 2024
1 parent a44df39 commit 35a2c3a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/mayaUsd/nodes/layerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,6 @@ MStatus addLayerToBuilder(
auto fileFormatIdToken = layer->GetFileFormat()->GetFormatId();
fileFormatIdHandle.setString(UsdMayaUtil::convert(fileFormatIdToken.GetString()));

MayaUsd::utils::setLayerUpAxisAndUnits(layer);

std::string temp;
if (!stubOnly && ((exportOnlyIfDirty && layer->IsDirty()) || !exportOnlyIfDirty)) {
if (!layer->ExportToString(&temp)) {
Expand Down Expand Up @@ -984,6 +982,10 @@ void LayerDatabase::convertAnonymousLayers(
// to convertAnonymousLayersRecursive
root = stage->GetRootLayer();
if (root->IsAnonymous()) {
// Only set up-axis and units metadata on the root layer
// and only if it is anonymous before being saved.
MayaUsd::utils::setLayerUpAxisAndUnits(root);

const bool wasTargetLayer = (stage->GetEditTarget().GetLayer() == root);
PXR_NS::SdfFileFormat::FileFormatArguments args;
std::string newFileName = MayaUsd::utils::generateUniqueFileName(proxyName);
Expand Down
7 changes: 6 additions & 1 deletion lib/mayaUsd/utils/utilSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ bool saveLayerWithFormat(
= requestedFormatArg.empty() ? usdFormatArgOption() : requestedFormatArg;

UsdMayaUtilFileSystem::updatePostponedRelativePaths(layer, filePath);
setLayerUpAxisAndUnits(layer);

if (isCompatibleWithSave(layer, filePath, formatArg)) {
if (!layer->Save()) {
Expand Down Expand Up @@ -471,6 +470,12 @@ SdfLayerRefPtr saveAnonymousLayer(
return nullptr;
}

// Only set up-axis and units metadata on the root layer
// and only if it is anonymous before being saved.
if (stage->GetRootLayer() == anonLayer) {
setLayerUpAxisAndUnits(anonLayer);
}

ensureUSDFileExtension(filePath);

const bool wasTargetLayer = (stage->GetEditTarget().GetLayer() == anonLayer);
Expand Down
32 changes: 24 additions & 8 deletions test/lib/mayaUsd/fileio/testSaveUpAxisAndUnits.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ def setUp(self):

def testSaveLayerInUSD(self):
# Save the file. Make sure the USD edits will go to a USD file.
self._runTestSaveLayer(1)
self._runTestSaveLayer(True)

def testSaveLayerInMaya(self):
# Save the file. Make sure the USD edits will go to the Maya file.
self._runTestSaveLayer(2)
self._runTestSaveLayer(False)

def _runTestSaveLayer(self, saveLocation):
def _runTestSaveLayer(self, saveOnDisk):
'''
The goal is to create an anonymous sub-layer, verify the up-axis and units
metadata is present when reloaded.
Expand Down Expand Up @@ -88,7 +88,9 @@ def verifyPrims(stage):

verifyPrims(stage)

# Save the file. Make sure the edit will go where requested by saveLocation.
# Save the file. Make sure the edit will go where requested by saveOnDisk.
# saveLocation 1 means on-disk, 2 in the Maya scene.
saveLocation = 1 if saveOnDisk else 2
tempMayaFile = 'saveUpAxisAndUnitsTest.ma'
cmds.optionVar(intValue=('mayaUsd_SerializedUsdEditsLocation', saveLocation))
cmds.file(rename=tempMayaFile)
Expand All @@ -111,13 +113,27 @@ def verifyPrims(stage):
subLayer = Sdf.Layer.FindOrOpen(subLayerPath)
self.assertIsNotNone(subLayer)

self.assertTrue(stage.HasAuthoredMetadata(UsdGeom.Tokens.metersPerUnit))
self.assertEqual(UsdGeom.GetStageMetersPerUnit(stage), 0.01)
self.assertTrue(stage.HasAuthoredMetadata(UsdGeom.Tokens.upAxis))
self.assertEqual(UsdGeom.GetStageUpAxis(stage), UsdGeom.Tokens.y)
# Verify metadata if saved on disk or verify the absence of metadata
# when saved in the Maya scene. We only author metadata when on-disk,
# since when saved in the Maya scene, the layers remain anonymous.
if saveOnDisk:
self.assertTrue(stage.HasAuthoredMetadata(UsdGeom.Tokens.metersPerUnit))
self.assertEqual(UsdGeom.GetStageMetersPerUnit(stage), 0.01)
self.assertTrue(stage.HasAuthoredMetadata(UsdGeom.Tokens.upAxis))
self.assertEqual(UsdGeom.GetStageUpAxis(stage), UsdGeom.Tokens.y)
else:
self.assertFalse(stage.HasAuthoredMetadata(UsdGeom.Tokens.metersPerUnit))
self.assertFalse(stage.HasAuthoredMetadata(UsdGeom.Tokens.upAxis))

# Verify the two objects are still present.
verifyPrims(stage)

# Stage the sub-layer by itself and verify it has no metadata,
# since we only author metadata on the root layer.
subStage = Usd.Stage.Open(subLayer)
self.assertFalse(subStage.HasAuthoredMetadata(UsdGeom.Tokens.metersPerUnit))
self.assertFalse(subStage.HasAuthoredMetadata(UsdGeom.Tokens.upAxis))


if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit 35a2c3a

Please sign in to comment.