Skip to content

Commit

Permalink
Merge pull request #2068 from Autodesk/tremblp/MAYA-121230/testParent…
Browse files Browse the repository at this point in the history
…Cmd_edit_router

Fix edit router parent test.
  • Loading branch information
Krystian Ligenza authored Feb 4, 2022
2 parents 1f134bc + 0be33d7 commit e02e9ce
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 34 deletions.
2 changes: 2 additions & 0 deletions lib/mayaUsd/python/wrapEditRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ void wrapEditRouter()
return MayaUsd::registerEditRouter(
operation, std::make_shared<PyEditRouter>(editRouter));
});

def("restoreDefaultEditRouter", &MayaUsd::restoreDefaultEditRouter);
}
1 change: 0 additions & 1 deletion lib/mayaUsd/resources/scripts/mayaUsdCacheMayaReference.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

from pxr import Sdf, Tf

from functools import partial
import re

# These names should not be localized as Usd only accepts [a-z,A-Z] as valid characters.
Expand Down
2 changes: 1 addition & 1 deletion lib/mayaUsd/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ MStatus initialize()
UFE_V2(Ufe::PathString::registerPathComponentSeparator(g_USDRtid, '/');)

// Initialize edit router registry with default routers.
auto defaults = editRouterDefaults();
auto defaults = defaultEditRouters();
for (const auto& entry : defaults) {
registerEditRouter(entry.first, entry.second);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/mayaUsd/ufe/UsdTransform3dUndoableCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ void UsdSetMatrix4dUndoableCommand::executeUndoBlock()
// transform stack, but not for a fallback Maya transform stack, and
// both can be edited by this command.
auto t3d = Ufe::Transform3d::editTransform3d(sceneItem());
if (!TF_VERIFY(t3d)) {
return;
}
t3d->translate(_newT.x(), _newT.y(), _newT.z());
t3d->rotate(_newR.x(), _newR.y(), _newR.z());
t3d->scale(_newS.x(), _newS.y(), _newS.z());
Expand Down
13 changes: 12 additions & 1 deletion lib/mayaUsd/utils/editRouter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void CxxEditRouter::operator()(
_cb(context, routingData);
}

EditRouters editRouterDefaults()
EditRouters defaultEditRouters()
{
PXR_NAMESPACE_USING_DIRECTIVE

Expand All @@ -186,6 +186,17 @@ void registerEditRouter(const PXR_NS::TfToken& operation, const EditRouter::Ptr&
editRouters[operation] = editRouter;
}

bool restoreDefaultEditRouter(const PXR_NS::TfToken& operation)
{
auto defaults = defaultEditRouters();
auto found = defaults.find(operation);
if (found == defaults.end()) {
return false;
}
registerEditRouter(operation, found->second);
return true;
}

EditRouter::Ptr getEditRouter(const PXR_NS::TfToken& operation)
{
auto foundRouter = editRouters.find(operation);
Expand Down
8 changes: 7 additions & 1 deletion lib/mayaUsd/utils/editRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ EditRouter::Ptr getEditRouter(const PXR_NS::TfToken& operation);
MAYAUSD_CORE_PUBLIC
void registerEditRouter(const PXR_NS::TfToken& operation, const EditRouter::Ptr& editRouter);

// Restore the default edit router for the argument operation, overwriting
// the currently-registered edit router. Returns false if no such default
// exists.
MAYAUSD_CORE_PUBLIC
bool restoreDefaultEditRouter(const PXR_NS::TfToken& operation);

// Return built-in default edit routers.
EditRouters editRouterDefaults();
EditRouters defaultEditRouters();

} // namespace MAYAUSD_NS_DEF
91 changes: 61 additions & 30 deletions test/lib/ufe/testParentCmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import mayaUsd.ufe

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

from maya import cmds
from maya import standalone
Expand All @@ -34,20 +34,15 @@
import os
import unittest

def filterUsdStr(usdSceneStr):
'''Remove empty lines and lines starting with pound character.'''
nonBlankLines = filter(None, [l.rstrip() for l in usdSceneStr.splitlines()])
finalLines = [l for l in nonBlankLines if not l.startswith('#')]
return '\n'.join(finalLines)

def childrenNames(children):
return [str(child.path().back()) for child in children]

def firstSubLayer(context, routingData):
prim = context.get('prim')
if prim is None:
print('Prim not in context')
return
if len(prim.GetStage().GetRootLayer().subLayerPaths)==0:
return
print('PaTh: '+prim.GetStage().GetRootLayer().subLayerPaths[0])
routingData['layer'] = prim.GetStage().GetRootLayer().subLayerPaths[0]

def matrixToList(m):
mList = []
for i in m.matrix:
Expand Down Expand Up @@ -1175,48 +1170,84 @@ def checkAfter(a, b, c, e, f):
children = hierarchyAfter()
checkAfter(*children)

# Test is currently crashing and needs to be investigated
def _testEditRouter(self):
def testEditRouter(self):
'''Test edit router functionality.'''

cmds.file(new=True, force=True)
import mayaUsd_createStageWithNewLayer

# Create the following hierarchy:
psPathStr = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
stage = mayaUsd.lib.GetPrim(psPathStr).GetStage()

# Create the following layer hierarchy:
#
# anonymousLayer1
# |_ bSubLayer
# |_ aSubLayer
#
# Sublayer B is thus higher-priority than A.
rootLayerId = stage.GetRootLayer().identifier
aSubLayerId = cmds.mayaUsdLayerEditor(rootLayerId, edit=True, addAnonymous="aSubLayer")[0]
bSubLayerId = cmds.mayaUsdLayerEditor(rootLayerId, edit=True, addAnonymous="bSubLayer")[0]

# Create the following hierarchy in lower-priority layer A.
#
# ps
# |_ A
# |_ B
# |_ C
#
# We select B and C, in order, and parent. This parents B to C.

psPathStr = mayaUsd_createStageWithNewLayer.createStageWithNewLayer()
stage = mayaUsd.lib.GetPrim(psPathStr).GetStage()
cmds.mayaUsdEditTarget(psPathStr, edit=True, editTarget=aSubLayerId)
stage.DefinePrim('/A', 'Xform')
stage.DefinePrim('/A/B', 'Xform')
stage.DefinePrim('/C', 'Xform')

psPath = ufe.PathString.path(psPathStr)
psPathSegment = psPath.segments[0]
aPath = ufe.Path([psPathSegment, usdUtils.createUfePathSegment('/A')])
a = ufe.Hierarchy.createItem(aPath)
bPath = aPath + ufe.PathComponent('B')
b = ufe.Hierarchy.createItem(bPath)
cPath = ufe.Path([psPathSegment, usdUtils.createUfePathSegment('/C')])
c = ufe.Hierarchy.createItem(cPath)

# Add a sub-layer, where the parent edit should write to.
subLayerId = cmds.mayaUsdLayerEditor(stage.GetRootLayer().identifier, edit=True, addAnonymous="aSubLayer")[0]

def firstSubLayer(context, routingData):
# Write edits to the highest-priority child layer of the root.

# Here, prim is the parent prim.
prim = context.get('prim')
self.assertIsNot(prim, None)
self.assertFalse(len(prim.GetStage().GetRootLayer().subLayerPaths)==0)
layerId = prim.GetStage().GetRootLayer().subLayerPaths[0]
layer = Sdf.Layer.Find(layerId)
# Make sure the destination exists in the target layer, otherwise
# SdfCopySpec will error.
Sdf.JustCreatePrimInLayer(layer, prim.GetPath())
routingData['layer'] = layerId

# Register our edit router which directs the parent edit to
# higher-priority layer B, which is not the edit target.
mayaUsd.lib.registerEditRouter('parent', firstSubLayer)

# Check that layer B is empty.
bSubLayer = Sdf.Layer.Find(bSubLayerId)
self.assertEqual(filterUsdStr(bSubLayer.ExportToString()), '')

# We select B and C, in order, and parent. This parents B to C.
sn = ufe.GlobalSelection.get()
sn.clear()
b = ufe.Hierarchy.createItem(ufe.PathString.path(psPathStr+',/A/B'))
c = ufe.Hierarchy.createItem(ufe.PathString.path(psPathStr+',/C'))
sn.append(b)
sn.append(c)

a = ufe.Hierarchy.createItem(ufe.PathString.path(psPathStr+',/A'))
self.assertEqual(ufe.Hierarchy.hierarchy(b).parent(), a)

cmds.parent()

# Check that prim B is now a child of prim C. Re-create its scene
# item, as its path has changed.
b = ufe.Hierarchy.createItem(ufe.PathString.path(psPathStr+',/C/B'))
self.assertEqual(ufe.Hierarchy.hierarchy(b).parent(), c)

# Check that layer B now has the parent overs.
self.assertEqual(filterUsdStr(bSubLayer.ExportToString()),
'over "C"\n{\n def Xform "B"\n {\n }\n}')

# Restore default edit router.
mayaUsd.lib.restoreDefaultEditRouter('parent')

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

0 comments on commit e02e9ce

Please sign in to comment.