Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAYA-113642 disallow switching variants in a weaker layer. #2533

Merged
merged 3 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/mayaUsd/ufe/UsdContextOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "UsdContextOps.h"

#include "private/UfeNotifGuard.h"
#include "private/Utils.h"

#ifdef UFE_V3_FEATURES_AVAILABLE
#include <mayaUsd/commands/PullPushCommands.h>
Expand Down Expand Up @@ -395,6 +396,11 @@ class SetVariantSelectionUndoableCommand : public Ufe::UndoableCommand
, _oldSelection(_varSet.GetVariantSelection())
, _newSelection(itemPath[2])
{
const bool allowStronger = true;
MayaUsd::ufe::applyCommandRestriction(
prim,
"set variant set " + _varSet.GetName() + " to variant " + _newSelection + " on",
allowStronger);
}

void undo() override
Expand Down
30 changes: 21 additions & 9 deletions lib/mayaUsd/ufe/private/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ SdfLayerHandle getStrongerLayer(
return SdfLayerHandle();
}

bool allowedInStrongerLayer(
const UsdPrim& prim,
const SdfPrimSpecHandleVector& primStack,
bool allowStronger)
{
// If the flag to allow edits in a stronger layer if off, then it is not allowed.
if (!allowStronger)
return false;

// If allowed, verify if the target layer is stronger than any existing layer with an opinion.
auto stage = prim.GetStage();
auto rootLayer = stage->GetRootLayer();
auto targetLayer = stage->GetEditTarget().GetLayer();
auto topLayer = primStack.front()->GetLayer();
return getStrongerLayer(rootLayer, targetLayer, topLayer) == targetLayer;
}

} // namespace
void applyCommandRestriction(
const UsdPrim& prim,
Expand Down Expand Up @@ -207,6 +224,8 @@ void applyCommandRestriction(
UsdPrimCompositionQuery query(prim);
for (const auto& compQueryArc : query.GetCompositionArcs()) {
if (!primSpec && PcpArcTypeVariant == compQueryArc.GetArcType()) {
if (allowedInStrongerLayer(prim, primStack, allowStronger))
return;
std::string err = TfStringPrintf(
"Cannot %s [%s] because it is defined inside the variant composition arc %s.",
commandName.c_str(),
Expand All @@ -217,15 +236,8 @@ void applyCommandRestriction(
}

if (!layerDisplayName.empty()) {
if (allowStronger) {
auto stage = prim.GetStage();
auto rootLayer = stage->GetRootLayer();
auto targetLayer = stage->GetEditTarget().GetLayer();
auto topLayer = primStack.front()->GetLayer();
if (getStrongerLayer(rootLayer, targetLayer, topLayer) == targetLayer) {
return;
}
}
if (allowedInStrongerLayer(prim, primStack, allowStronger))
return;
std::string err = TfStringPrintf(
"Cannot %s [%s]. %s. Please set %s as the target layer to proceed.",
commandName.c_str(),
Expand Down
56 changes: 56 additions & 0 deletions test/lib/ufe/testContextOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,62 @@ def testGetItems(self):
if c.checked:
self.assertEqual(c.item, 'Ball_8')

def testSwitchVariantInWeakerLayer(self):
"""
Test that switching variant in a weaker layer is restricted.
"""
contextItems = self.contextOps.getItems([])

contextItemStrings = [c.item for c in contextItems]
self.assertIn('Variant Sets', contextItemStrings)

# Initial shadingVariant is "Ball_8"
contextItems = self.contextOps.getItems(
['Variant Sets', 'shadingVariant'])

# Change variant in variant set.
def shadingVariant():
contextItems = self.contextOps.getItems(
['Variant Sets', 'shadingVariant'])

for c in contextItems:
if c.checked:
return c.item

def shadingVariantOnPrim():
variantSet = self.ball35Prim.GetVariantSet('shadingVariant')
self.assertIsNotNone(variantSet)
return variantSet.GetVariantSelection()

self.assertEqual(shadingVariant(), 'Ball_8')

cmd = self.contextOps.doOpCmd(
['Variant Sets', 'shadingVariant', 'Cue'])
self.assertIsNotNone(cmd)

ufeCmd.execute(cmd)

self.assertEqual(shadingVariant(), 'Cue')
self.assertEqual(shadingVariantOnPrim(), 'Cue')

# Add a lower, weaker layer.
stage = self.ball35Prim.GetStage()
rootLayer = stage.GetRootLayer()
newLayerName = 'Layer_1'
usdFormat = Sdf.FileFormat.FindByExtension('usd')
subLayer = Sdf.Layer.New(usdFormat, newLayerName)
rootLayer.subLayerPaths.append(subLayer.identifier)
stage.SetEditTarget(subLayer)

# Verify we cannot switch variant in a weaker layer.
self.assertRaises(RuntimeError, lambda: self.contextOps.doOpCmd(
['Variant Sets', 'shadingVariant', 'Ball_8']))

# Verify the variant has not switched.
self.assertEqual(shadingVariant(), 'Cue')
self.assertEqual(shadingVariantOnPrim(), 'Cue')


def testDoOp(self):
# Change visibility, undo / redo.
cmd = self.contextOps.doOpCmd(['Toggle Visibility'])
Expand Down