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-128599 fixing pivot command #3110

Merged
merged 1 commit into from
May 26, 2023
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
74 changes: 74 additions & 0 deletions doc/USD Transforms Stack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# USD Transforms Stack

## Take Aways

- Understand the USD transform stack (xformOp)
- Know the available USD transform ops
- Know the structure of the USD transform stack
- Know how it maps to UFE
- Know the various implementations of xformOp in MayaUSD

## What is a USD Transform stack

- Control the 3D position of a USD prim
- It is an ordered list of transform operations (xformOp):
translation, rotation, scale, 4x4 matrix
- USD supports any number of xformOp, in any order
- An individual xformOp is kept in a USD attribute
- All transform-related attributes begin with the "xformOp:" prefix
- An xformOp attribute name has two or three parts:
- "xformOp:"
- the transform type
- optional suffix
- For example: "xformOp:translate:pivot"
- The xformOp order is kept in a special attribute: "xformOpOrder"

## Quick Recap on Pivot

Pivots...

- ... are generally neutral: they don't move the prim
- ... come as a pair of opposite translations, sandwhiching other transforms
- ... are used in DCC to position the center of rotation and center of scaling

## Quick Recap on Transform Maths

- A single matrix is equivalent to any number of chained transforms
- The inverse is not true
- Some matrices cannot be expressed with translation, rotation and scaling...
- ... but they are generally considered degenerate and are rarely seen
- On the other hand, matrix cannot express pivot pairs, since they are neutral
- In general, except for pivots, all transform representations are equivalent

## USD Common Transform Stack

- USD provides a recommended, simple transform stack it calls the "common API"
- The goal is to have a baseline transform stack that all DCC should support
- The USD common stack structure is: translate, pivot, rotate, scale
- In particular, the pivot wraps both the rotation and scaling, unlike Maya

## UFE Transforms

UFE Transform3d ...

- ... allows modifying a UFE scene item transforms
- ... is created by the registered UFE Transform3dHandler
- ... creates commands that when executed changes the transform
- ... does *not* prescribe how the various transforms interact
- ... is implicitly tied to Maya's view of how transforms are ordered

## MayaUSD XformOp Implementations

- MayaUSD provides multiple UFE Transform3d implementations
- The implementations are: point instance, Maya stack, USD common API, 4x4 matrix and no comprendo + Maya stack
- For the rest of the presentation, we will ignore point instance and no comprendo

## MayaUSD XformOp Details

- Only the Maya stack supports all the UFE commands. In particular, pivot commands
- Which one is used is decided at the time of the creation of the Transform3d
- Once decided, there is no turning back, we cannot switch dynamically
- The decision is based on what is already authored on the prim
- In case multiple implementations could be used, the priority order is: Maya stack, common API, matrix
- The main goal was that we would privilege the Maya stack, but still support the others representations
- One design decision is to *not* convert between representations
Binary file added doc/USD Transforms Stack.pdf
Binary file not shown.
81 changes: 51 additions & 30 deletions lib/mayaUsd/fileio/utils/xformStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,11 @@ UsdMayaXformStack::MatchingSubstack(const std::vector<UsdGeomXformOp>& xformops)

if (GetNameMatters()) {
// First try the fast attrName lookup...
const auto foundTokenIdxPairIter
= _sharedData->m_attrNamesToIdxs.find(xformOp.GetName());
const TfToken& opName = xformOp.GetName();
const auto foundTokenIdxPairIter = _sharedData->m_attrNamesToIdxs.find(opName);
if (foundTokenIdxPairIter == _sharedData->m_attrNamesToIdxs.end()) {
// Couldn't find the xformop in our stack, abort
// TF_WARN("Cannot find xform op %s", opName.GetText());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I left these debug print in comments behind because I found them useful to understand what was going on, so They will be there for teh next dev that need to investigate problem with this complex code.

return _NO_MATCH;
}

Expand All @@ -455,13 +456,21 @@ UsdMayaXformStack::MatchingSubstack(const std::vector<UsdGeomXformOp>& xformops)
} else {
// The result we found is before an earlier-found op,
// so it doesn't match our stack... abort.
// TF_WARN(
// "Cannot find index of xform op %s at %ld between [%ld, %ld]",
// opName.GetText(),
// long(nextOpIndex),
// long(foundIdxPair.first),
// long(foundIdxPair.second));
return _NO_MATCH;
}

assert(foundOpIdx != NO_INDEX);
// TF_STATUS("Found xform op %s at %ld", opName.GetText(), long(foundOpIdx));

// Now check that the op type matches...
if (!GetOps()[foundOpIdx].IsCompatibleType(xformOp.GetOpType())) {
// TF_WARN("Incorrect type for xform op %s", opName.GetText());
return _NO_MATCH;
}
} else {
Expand Down Expand Up @@ -489,7 +498,10 @@ UsdMayaXformStack::MatchingSubstack(const std::vector<UsdGeomXformOp>& xformops)
// check pivot pairs
TF_FOR_ALL(pairIter, GetInversionTwins())
{
if (opNamesFound[pairIter->first] != opNamesFound[pairIter->second]) {
const size_t firstIdx = pairIter->first;
const size_t secondIdx = pairIter->second;
if (opNamesFound[firstIdx] != opNamesFound[secondIdx]) {
// TF_WARN("Unmatched pivot pairs at [%ld, %ld]", firstIdx, secondIdx);
return _NO_MATCH;
}
}
Expand Down Expand Up @@ -519,36 +531,45 @@ const UsdMayaXformStack& UsdMayaXformStack::MayaStack()
{
static UsdMayaXformStack mayaStack(
// ops
{ UsdMayaXformOpClassification(
UsdMayaXformStackTokens->translate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivotTranslate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivot, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotate, UsdGeomXformOp::TypeRotateXYZ),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotateAxis, UsdGeomXformOp::TypeRotateXYZ),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivot,
UsdGeomXformOp::TypeTranslate,
true /* isInvertedTwin */),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivotTranslate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivot, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->shear, UsdGeomXformOp::TypeTransform),
UsdMayaXformOpClassification(UsdMayaXformStackTokens->scale, UsdGeomXformOp::TypeScale),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivot,
UsdGeomXformOp::TypeTranslate,
true /* isInvertedTwin */) },
{
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->translate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->pivot, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivotTranslate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivot, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotate, UsdGeomXformOp::TypeRotateXYZ),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotateAxis, UsdGeomXformOp::TypeRotateXYZ),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->rotatePivot,
UsdGeomXformOp::TypeTranslate,
true /* isInvertedTwin */),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivotTranslate, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivot, UsdGeomXformOp::TypeTranslate),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->shear, UsdGeomXformOp::TypeTransform),
UsdMayaXformOpClassification(UsdMayaXformStackTokens->scale, UsdGeomXformOp::TypeScale),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->scalePivot,
UsdGeomXformOp::TypeTranslate,
true /* isInvertedTwin */),
UsdMayaXformOpClassification(
UsdMayaXformStackTokens->pivot,
UsdGeomXformOp::TypeTranslate,
true /* isInvertedTwin */),
},

// inversionTwins
{
{ 2, 5 },
{ 7, 10 },
{ 1, 12 },
{ 3, 6 },
{ 8, 11 },
});

return mayaStack;
Expand Down
7 changes: 6 additions & 1 deletion lib/mayaUsd/ufe/UsdTransform3dMayaXformStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ PXR_NS::UsdAttribute getUsdPrimAttribute(const UsdPrim& prim, const TfToken& att
const std::unordered_map<TfToken, UsdTransform3dMayaXformStack::OpNdx, TfToken::HashFunctor>
gOpNameToNdx {
{ TfToken("xformOp:translate"), UsdTransform3dMayaXformStack::NdxTranslate },
// Note: this matches the USD common xformOp name.
{ TfToken("xformOp:translate:pivot"), UsdTransform3dMayaXformStack::NdxPivot },
{ TfToken("xformOp:translate:rotatePivotTranslate"),
UsdTransform3dMayaXformStack::NdxRotatePivotTranslate },
{ TfToken("xformOp:translate:rotatePivot"), UsdTransform3dMayaXformStack::NdxRotatePivot },
Expand All @@ -97,7 +99,10 @@ const std::unordered_map<TfToken, UsdTransform3dMayaXformStack::OpNdx, TfToken::
{ TfToken("xformOp:transform:shear"), UsdTransform3dMayaXformStack::NdxShear },
{ TfToken("xformOp:scale"), UsdTransform3dMayaXformStack::NdxScale },
{ TfToken("!invert!xformOp:translate:scalePivot"),
UsdTransform3dMayaXformStack::NdxScalePivotInverse }
UsdTransform3dMayaXformStack::NdxScalePivotInverse },
// Note: this matches the USD common xformOp name.
{ TfToken("!invert!xformOp:translate:pivot"),
UsdTransform3dMayaXformStack::NdxPivotInverse }
};

} // namespace
Expand Down
2 changes: 2 additions & 0 deletions lib/mayaUsd/ufe/UsdTransform3dMayaXformStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MAYAUSD_CORE_PUBLIC UsdTransform3dMayaXformStack : public UsdTransform3dBa
enum OpNdx
{
NdxTranslate = 0,
NdxPivot,
NdxRotatePivotTranslate,
NdxRotatePivot,
NdxRotate,
Expand All @@ -51,6 +52,7 @@ class MAYAUSD_CORE_PUBLIC UsdTransform3dMayaXformStack : public UsdTransform3dBa
NdxShear,
NdxScale,
NdxScalePivotInverse,
NdxPivotInverse,
NbOpNdx
};

Expand Down
8 changes: 5 additions & 3 deletions lib/usd/ui/layerEditor/mayaSessionState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ bool MayaSessionState::getStageEntry(StageEntry* out_stageEntry, const MString&

MObject shapeObj;
MStatus status = UsdMayaUtil::GetMObjectByName(shapePath, shapeObj);
CHECK_MSTATUS_AND_RETURN(status, false);
if (!status)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were printing error message when there were no stage loaded, which was annoying.

return false;
MFnDagNode dagNode(shapeObj, &status);
CHECK_MSTATUS_AND_RETURN(status, false);
if (!status)
return false;

if (const UsdMayaUsdPrimProvider* usdPrimProvider
= dynamic_cast<const UsdMayaUsdPrimProvider*>(dagNode.userNode())) {
Expand Down Expand Up @@ -303,7 +305,7 @@ void MayaSessionState::loadSelectedStage()
#if defined(WANT_UFE_BUILD)
const std::string shapePath = MayaUsd::LayerManager::getSelectedStage();
StageEntry entry;
if (getStageEntry(&entry, shapePath.c_str())) {
if (!shapePath.empty() && getStageEntry(&entry, shapePath.c_str())) {
setStageEntry(entry);
}
#endif
Expand Down
Loading