Skip to content

Commit

Permalink
allowed edit target layer to be written a new prim spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Wu committed May 27, 2022
1 parent 85740ad commit 3fb772d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/mayaUsd/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bool stringBeginsWithDigit(const std::string& inputString)

// This function calculates the position index for a given layer across all
// the site's local LayerStacks
uint32_t findLayerIndex(const UsdPrim& prim, const SdfLayerHandle& layer)
uint32_t findLayerIndex(const UsdPrim& prim, const SdfLayerHandle& layer, bool& foundLayer)
{
uint32_t position { 0 };

Expand All @@ -96,6 +96,7 @@ uint32_t findLayerIndex(const UsdPrim& prim, const SdfLayerHandle& layer)
// to find the layer
for (SdfLayerRefPtr const& l : layerStack->GetLayers()) {
if (l == layer) {
foundLayer = true;
return position;
}
++position;
Expand Down Expand Up @@ -451,8 +452,15 @@ bool isAttributeEditAllowed(const PXR_NS::UsdAttribute& attr, std::string* errMs
return false;
}

bool foundLayer = false;

// get the index to edit target layer
const auto targetLayerIndex = findLayerIndex(prim, editTarget.GetLayer());
const auto targetLayerIndex = findLayerIndex(prim, editTarget.GetLayer(), foundLayer);

// If the edit target layer not found in the prim's layer stack, we return true to allow
// new future edits go to the edit target layer.
if (!foundLayer)
return true;

// HS March 22th,2021
// TODO: "Value Clips" are UsdStage-level feature, unknown to Pcp.So if the attribute in
Expand All @@ -470,14 +478,15 @@ bool isAttributeEditAllowed(const PXR_NS::UsdAttribute& attr, std::string* errMs
if (!propertyStack.empty()) {
// get the strongest layer that has the attr.
auto strongestLayer = attr.GetPropertyStack().front()->GetLayer();
foundLayer = false;

// compare the calculated index between the "attr" and "edit target" layers.
if (findLayerIndex(prim, strongestLayer) < targetLayerIndex) {
if (findLayerIndex(prim, strongestLayer, foundLayer) < targetLayerIndex) {
if (errMsg) {
*errMsg = TfStringPrintf(
"Cannot edit [%s] attribute because there is a stronger opinion in [%s].",
attr.GetBaseName().GetText(),
strongestLayer->GetDisplayName().c_str());
attr.GetName().GetText(),
strongestLayer->GetIdentifier().c_str());
}

return false;
Expand Down
81 changes: 80 additions & 1 deletion test/lib/ufe/testAttributeBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import ufeUtils
import usdUtils

from pxr import UsdGeom, Vt, Gf
from pxr import UsdGeom, Vt, Gf, Usd, Sdf

from maya import cmds
from maya import standalone
Expand All @@ -32,6 +32,8 @@
import ufe
import os
import unittest
import textwrap


class AttributeBlockTestCase(unittest.TestCase):

Expand Down Expand Up @@ -347,6 +349,83 @@ def testAttributeBlocking3dMatrixOps(self):

# authoring new transformation edit is allowed.
self.assertTrue(mayaUsdUfe.isAttributeEditAllowed(transformAttr))

def testIsAttributeEditAllowedFunction(self):
rootLayerStr = """\
#sdf 1.4.32
()
def "root"
{
}
"""
subLayerAStr = """\
#sdf 1.4.32
over "root"
{
def "a"
{
float foo = 0.0
}
def "b"
{
float foo = 0.0
}
}
"""
subLayerBStr = """\
#sdf 1.4.32
over "root"
{
}
"""
subLayerCStr = """\
#sdf 1.4.32
over "root"
{
over "a"
{
float foo = 10.0
}
}
"""

rootLayer = Sdf.Layer.CreateAnonymous()
rootLayer.ImportFromString(textwrap.dedent(rootLayerStr))

subLayerA = Sdf.Layer.CreateAnonymous()
subLayerA.ImportFromString(textwrap.dedent(subLayerAStr))

subLayerB = Sdf.Layer.CreateAnonymous()
subLayerB.ImportFromString(textwrap.dedent(subLayerBStr))

subLayerC = Sdf.Layer.CreateAnonymous()
subLayerC.ImportFromString(textwrap.dedent(subLayerCStr))

rootLayer.subLayerPaths = [
subLayerC.identifier,
subLayerB.identifier,
subLayerA.identifier,
]
stage = Usd.Stage.Open(rootLayer)

primB = stage.GetPrimAtPath("/root/b")
self.assertTrue(primB.IsValid())

fooattrB = primB.GetAttribute("foo")
self.assertTrue(fooattrB.IsValid())

stage.SetEditTarget(Usd.EditTarget(subLayerB))
self.assertTrue(mayaUsdUfe.isAttributeEditAllowed(fooattrB))

primA = stage.GetPrimAtPath("/root/a")
self.assertTrue(primA.IsValid())

fooattrA = primA.GetAttribute("foo")
self.assertTrue(fooattrA.IsValid())

self.assertFalse(mayaUsdUfe.isAttributeEditAllowed(fooattrA))


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

0 comments on commit 3fb772d

Please sign in to comment.