Skip to content

Commit

Permalink
MAYA-129059 - Add a markAsFailed() method to UsdUndoCreateStageWithNe…
Browse files Browse the repository at this point in the history
…wLayerCommand.

Replace `#pragma once` with header guard.
Use `getProxyShape()` instead of calling `g_StageMap.proxyShape()` directly.
  • Loading branch information
frohnej-adsk committed May 10, 2023
1 parent 60e93f7 commit ab589dc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
46 changes: 32 additions & 14 deletions lib/mayaUsd/ufe/UsdUndoCreateStageWithNewLayerCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//
#include "UsdUndoCreateStageWithNewLayerCommand.h"

#include <mayaUsd/ufe/UsdStageMap.h>
#include <mayaUsd/ufe/UsdUndoRenameCommand.h>
#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/undo/OpUndoItems.h>
Expand All @@ -27,8 +26,6 @@ PXR_NAMESPACE_USING_DIRECTIVE
namespace MAYAUSD_NS_DEF {
namespace ufe {

extern UsdStageMap g_StageMap;

UsdUndoCreateStageWithNewLayerCommand::UsdUndoCreateStageWithNewLayerCommand(
const Ufe::SceneItem::Ptr& parentItem)
: _parentItem(nullptr)
Expand Down Expand Up @@ -63,6 +60,7 @@ Ufe::SceneItem::Ptr UsdUndoCreateStageWithNewLayerCommand::sceneItem() const
void UsdUndoCreateStageWithNewLayerCommand::execute()
{
if (!_parentItem) {
markAsFailed();
return;
}

Expand All @@ -74,6 +72,7 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
MDagPath parentDagPath = MayaUsd::ufe::ufeToDagPath(_parentItem->path());
MObject parentObject = parentDagPath.transform(&status);
if (status != MStatus::kInvalidParameter && MFAIL(status)) {
markAsFailed();
return;
}

Expand All @@ -86,11 +85,13 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
MObject transformObj;
transformObj = _createTransformDagMod.createNode("transform", parentObject, &status);
if (MFAIL(status)) {
markAsFailed();
return;
}
TF_VERIFY(!transformObj.isNull());
status = _createTransformDagMod.doIt();
if (MFAIL(status)) {
markAsFailed();
return;
}
_createTransformSuccess = true;
Expand All @@ -99,6 +100,7 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
MObject proxyShape;
proxyShape = _createProxyShapeDagMod.createNode("mayaUsdProxyShape", transformObj, &status);
if (MFAIL(status)) {
markAsFailed();
return;
}
TF_VERIFY(!proxyShape.isNull());
Expand All @@ -111,14 +113,17 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
// according to the suffix of the transform, because they now share the common prefix "stage".
status = _createProxyShapeDagMod.renameNode(proxyShape, "stageShape1");
if (MFAIL(status)) {
markAsFailed();
return;
}
status = _createProxyShapeDagMod.renameNode(transformObj, "stage1");
if (MFAIL(status)) {
markAsFailed();
return;
}
status = _createProxyShapeDagMod.renameNode(transformObj, "stage1");
if (MFAIL(status)) {
markAsFailed();
return;
}

Expand All @@ -128,41 +133,49 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
MObject time1;
status = selection.getDependNode(0, time1);
if (MFAIL(status)) {
markAsFailed();
return;
}
MFnDependencyNode time1DepNodeFn(time1, &status);
if (MFAIL(status)) {
markAsFailed();
return;
}
MObject time1OutTimeAttr = time1DepNodeFn.attribute("outTime", &status);
if (MFAIL(status)) {
markAsFailed();
return;
}

// Get the `time` attribute of the newly created mayaUsdProxyShape.
MDagPath proxyShapeDagPath = MDagPath::getAPathTo(proxyShape, &status);
if (MFAIL(status)) {
markAsFailed();
return;
}
MFnDependencyNode proxyShapeDepNodeFn(proxyShapeDagPath.node(), &status);
if (MFAIL(status)) {
markAsFailed();
return;
}
MObject proxyShapeTimeAttr = proxyShapeDepNodeFn.attribute("time", &status);
if (MFAIL(status)) {
markAsFailed();
return;
}

// Connect `time1.outTime` to `proxyShapde.time`.
status
= _createProxyShapeDagMod.connect(time1, time1OutTimeAttr, proxyShape, proxyShapeTimeAttr);
if (MFAIL(status)) {
markAsFailed();
return;
}

// Execute the operations.
status = _createProxyShapeDagMod.doIt();
if (MFAIL(status)) {
markAsFailed();
return;
}
_createProxyShapeSuccess = true;
Expand All @@ -175,35 +188,40 @@ void UsdUndoCreateStageWithNewLayerCommand::execute()
// When creating the proxy shape, the stage map gets dirtied and cleaned. Afterwards, the proxy
// shape is renamed. The stage map does not observe the Maya data model, so renaming does not
// dirty the stage map again. Thus, the cache is in an invalid state, where it contains the
// path of the proxy shape before it was renamed. Calling UsdStageMap::proxyShape() refreshes
// the cache. See comments within UsdStageMap::proxyShape() for more details.
g_StageMap.proxyShape(proxyShapeUfePath);
// path of the proxy shape before it was renamed. Calling getProxyShape() refreshes the cache.
// See comments within UsdStageMap::proxyShape() for more details.
getProxyShape(proxyShapeUfePath);
}

void UsdUndoCreateStageWithNewLayerCommand::undo()
{
if (_createProxyShapeSuccess) {
_createProxyShapeDagMod.undoIt();
}

if (_createTransformSuccess) {
_createTransformDagMod.undoIt();
}
}

void UsdUndoCreateStageWithNewLayerCommand::redo()
{
if (_createTransformSuccess) {
if (_createProxyShapeSuccess) {
_createTransformDagMod.doIt();
_createProxyShapeDagMod.doIt();

// Refresh the cache of the stage map.
if (_insertedChild) {
getProxyShape(_insertedChild->path());
}
}
}

void UsdUndoCreateStageWithNewLayerCommand::markAsFailed()
{
if (_createProxyShapeSuccess) {
_createProxyShapeDagMod.doIt();
_createProxyShapeDagMod.undoIt();
}

// Refresh the cache of the stage map.
if (_insertedChild) {
g_StageMap.proxyShape(_insertedChild->path());
if (_createTransformSuccess) {
_createTransformDagMod.undoIt();
}
}

Expand Down
7 changes: 6 additions & 1 deletion lib/mayaUsd/ufe/UsdUndoCreateStageWithNewLayerCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#pragma once
#ifndef MAYAUSD_UFE_CREATESTAGEWITHNEWLAYERCOMMAND_H
#define MAYAUSD_UFE_CREATESTAGEWITHNEWLAYERCOMMAND_H

#include <mayaUsd/base/api.h>

Expand Down Expand Up @@ -59,6 +60,8 @@ class MAYAUSD_CORE_PUBLIC UsdUndoCreateStageWithNewLayerCommand
void redo() override;

private:
void markAsFailed();

Ufe::SceneItem::Ptr _parentItem;
Ufe::SceneItem::Ptr _insertedChild;

Expand All @@ -70,3 +73,5 @@ class MAYAUSD_CORE_PUBLIC UsdUndoCreateStageWithNewLayerCommand

} // namespace ufe
} // namespace MAYAUSD_NS_DEF

#endif // MAYAUSD_UFE_CREATESTAGEWITHNEWLAYERCOMMAND_H

0 comments on commit ab589dc

Please sign in to comment.