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-106722 Maya check callback to see if there are any unsaved edits #934

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions plugin/adsk/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ target_sources(${TARGET_NAME}
importTranslator.cpp
exportTranslator.cpp
ProxyShape.cpp
sceneResetCheck.cpp
)

# -----------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions plugin/adsk/plugin/base/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@
#define MAYAUSD_PLUGIN_LOCAL
#endif
#endif

// Convenience symbol versioning include: because api.h is widely
// included, this reduces the need to explicitly include mayaUsd.h.
#include <mayaUsd/mayaUsd.h>
5 changes: 5 additions & 0 deletions plugin/adsk/plugin/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "base/api.h"
#include "exportTranslator.h"
#include "importTranslator.h"
#include "sceneResetCheck.h"

#include <mayaUsd/base/api.h>
#include <mayaUsd/commands/editTargetCommand.h>
Expand Down Expand Up @@ -201,6 +202,8 @@ MStatus initializePlugin(MObject obj)

UsdMayaSceneResetNotice::InstallListener();

MayaUsd::SceneResetCheck::registerSceneResetCheckCallback();

return status;
}

Expand Down Expand Up @@ -259,5 +262,7 @@ MStatus uninitializePlugin(MObject obj)

UsdMayaSceneResetNotice::RemoveListener();

MayaUsd::SceneResetCheck::deregisterSceneResetCheckCallback();

return status;
}
115 changes: 115 additions & 0 deletions plugin/adsk/plugin/sceneResetCheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//
// Copyright 2020 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "sceneResetCheck.h"

#include "ProxyShape.h"

#include <pxr/pxr.h>
#include <pxr/usd/usd/common.h>
#include <pxr/usd/usd/stage.h>

#include <maya/MFnDependencyNode.h>
#include <maya/MGlobal.h>
#include <maya/MItDependencyNodes.h>
#include <maya/MMessage.h>
#include <maya/MSceneMessage.h>

PXR_NAMESPACE_USING_DIRECTIVE

namespace {
MCallbackId _beforeNewCheckCallbackId = 0;
MCallbackId _beforeOpenCheckCallbackId = 0;

const char* ignoreDirtyLayersConfirmScript = R"(
global proc string MayaUsdIgnoreDirtyLayersConfirm()
{
return `confirmDialog -title "Discard USD Edits"
-message "Are you sure you want to exit this session?\n\nAll edits on your USD layer(s) will be discarded."
-button "Yes" -button "No" -defaultButton "No" -icon "warning"
-cancelButton "No" -dismissString "No"`;

}
MayaUsdIgnoreDirtyLayersConfirm();
)";

static void _OnMayaNewOrOpenSceneCheckCallback(bool* retCode, void*)
{
*retCode = true;

bool atLeastOneDirty = false;

MFnDependencyNode fn;
MItDependencyNodes iter(MFn::kPluginDependNode);

for (; !iter.isDone() && !atLeastOneDirty; iter.next()) {
MObject mobj = iter.item();
fn.setObject(mobj);
if (MayaUsd::ProxyShape::typeId == fn.typeId()) {
MayaUsdProxyShapeBase* pShape = static_cast<MayaUsdProxyShapeBase*>(fn.userNode());
UsdStageRefPtr stage = pShape ? pShape->getUsdStage() : nullptr;
if (!stage) {
continue;
}

std::string temp;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This variable is unused.

SdfLayerHandleVector allLayers = stage->GetLayerStack(true);
for (auto layer : allLayers) {
if (layer->IsDirty()) {
atLeastOneDirty = true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not break here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The for loop will exit immediately, it checks atLeastOneDirty. I think I had originally been printing out some information while debugging something and just left it like this (but completely missed the temp variable you pointed out). I could remove the check in the for loop and add an extra line to "break" here if you prefer that style.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually it won't because you are in a for-loop (allLayers) inside of that other for-loop (that checks atLeastOneDirty).

}
}
}
}

if (atLeastOneDirty) {
MString allowExit = MGlobal::executeCommandStringResult(ignoreDirtyLayersConfirmScript);
if (MString("No") == allowExit) {
*retCode = false;
}
}
}

} // namespace

namespace MAYAUSD_NS_DEF {

void SceneResetCheck::registerSceneResetCheckCallback()
{
if (_beforeNewCheckCallbackId == 0) {
_beforeNewCheckCallbackId = MSceneMessage::addCheckCallback(
MSceneMessage::kBeforeNewCheck, _OnMayaNewOrOpenSceneCheckCallback);
}

if (_beforeOpenCheckCallbackId == 0) {
_beforeOpenCheckCallbackId = MSceneMessage::addCheckCallback(
MSceneMessage::kBeforeOpenCheck, _OnMayaNewOrOpenSceneCheckCallback);
}
}

void SceneResetCheck::deregisterSceneResetCheckCallback()
{
if (_beforeNewCheckCallbackId != 0) {
MMessage::removeCallback(_beforeNewCheckCallbackId);
_beforeNewCheckCallbackId = 0;
}

if (_beforeOpenCheckCallbackId != 0) {
MMessage::removeCallback(_beforeOpenCheckCallbackId);
_beforeOpenCheckCallbackId = 0;
}
}

} // namespace MAYAUSD_NS_DEF
35 changes: 35 additions & 0 deletions plugin/adsk/plugin/sceneResetCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright 2020 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef ADSK_MAYA_SCENE_RESET_CHECK_H
#define ADSK_MAYA_SCENE_RESET_CHECK_H

#include "base/api.h"

namespace MAYAUSD_NS_DEF {

class SceneResetCheck
{
public:
MAYAUSD_PLUGIN_PUBLIC
static void registerSceneResetCheckCallback();

MAYAUSD_PLUGIN_PUBLIC
static void deregisterSceneResetCheckCallback();
};

} // namespace MAYAUSD_NS_DEF

#endif