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

Mute layer and variant switch preserve selection. #1141

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 35 additions & 1 deletion lib/mayaUsd/commands/layerEditorCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "layerEditorCommand.h"

#include <mayaUsd/ufe/Utils.h>
#include <mayaUsd/utils/query.h>

#include <pxr/base/tf/diagnostic.h>
Expand All @@ -24,6 +25,10 @@
#include <maya/MArgParser.h>
#include <maya/MStringArray.h>
#include <maya/MSyntax.h>
#include <ufe/globalSelection.h>
#include <ufe/hierarchy.h>
#include <ufe/observableSelection.h>
#include <ufe/pathString.h>

#include <cstddef>
#include <string>
Expand Down Expand Up @@ -368,9 +373,13 @@ class MuteLayer : public BaseCmd
if (!stage)
return false;
if (_muteIt) {
// Muting a layer will cause all scene items under the proxy shape
// to be stale.
saveSelection();
stage->MuteLayer(layer->GetIdentifier());
} else {
stage->UnmuteLayer(layer->GetIdentifier());
restoreSelection();
}

// we perfer not holding to pointers needlessly, but we need to hold on to the layer if we
Expand All @@ -387,7 +396,11 @@ class MuteLayer : public BaseCmd
return false;
if (_muteIt) {
stage->UnmuteLayer(layer->GetIdentifier());
restoreSelection();
} else {
// Muting a layer will cause all scene items under the proxy shape
// to be stale.
saveSelection();
stage->MuteLayer(layer->GetIdentifier());
}
// we can release the pointer
Expand All @@ -398,14 +411,35 @@ class MuteLayer : public BaseCmd
std::string _proxyShapePath;
bool _muteIt = true;

protected:
private:
UsdStageWeakPtr getStage()
{
auto prim = UsdMayaQuery::GetPrim(_proxyShapePath.c_str());
auto stage = prim.GetStage();
return stage;
}

void saveSelection()
{
// Make a copy of the global selection, to restore it on unmute.
auto globalSn = Ufe::GlobalSelection::get();
_savedSn.replaceWith(*globalSn);
// Filter the global selection, removing items below our proxy shape.
globalSn->replaceWith(
MayaUsd::ufe::removeDescendants(_savedSn, Ufe::PathString::path(_proxyShapePath)));
}

void restoreSelection()
{
// Restore the saved selection to the global selection. If a saved
// selection item started with the proxy shape path, re-create it.
auto globalSn = Ufe::GlobalSelection::get();
globalSn->replaceWith(
MayaUsd::ufe::recreateDescendants(_savedSn, Ufe::PathString::path(_proxyShapePath)));
}

PXR_NS::SdfLayerRefPtr _mutedLayer;
Ufe::Selection _savedSn;
};

} // namespace Impl
Expand Down
39 changes: 30 additions & 9 deletions lib/mayaUsd/ufe/UsdContextOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#if UFE_PREVIEW_VERSION_NUM >= 2034
#include <ufe/object3d.h>
#endif
#include <ufe/globalSelection.h>
#include <ufe/observableSelection.h>
#include <ufe/path.h>

#include <algorithm>
Expand Down Expand Up @@ -179,22 +181,41 @@ class SetVariantSelectionUndoableCommand : public Ufe::UndoableCommand
{
public:
SetVariantSelectionUndoableCommand(
const Ufe::Path& path,
const UsdPrim& prim,
const Ufe::ContextOps::ItemPath& itemPath)
: fVarSet(prim.GetVariantSets().GetVariantSet(itemPath[1]))
, fOldSelection(fVarSet.GetVariantSelection())
, fNewSelection(itemPath[2])
: _path(path)
, _varSet(prim.GetVariantSets().GetVariantSet(itemPath[1]))
, _oldSelection(_varSet.GetVariantSelection())
, _newSelection(itemPath[2])
{
}

void undo() override { fVarSet.SetVariantSelection(fOldSelection); }
void undo() override
{
_varSet.SetVariantSelection(_oldSelection);
// Restore the saved selection to the global selection. If a saved
// selection item started with the prim's path, re-create it.
auto globalSn = Ufe::GlobalSelection::get();
globalSn->replaceWith(MayaUsd::ufe::recreateDescendants(_savedSn, _path));
}

void redo() override { fVarSet.SetVariantSelection(fNewSelection); }
void redo() override
{
// Make a copy of the global selection, to restore it on unmute.
auto globalSn = Ufe::GlobalSelection::get();
_savedSn.replaceWith(*globalSn);
// Filter the global selection, removing items below our prim.
globalSn->replaceWith(MayaUsd::ufe::removeDescendants(_savedSn, _path));
_varSet.SetVariantSelection(_newSelection);
}

private:
UsdVariantSet fVarSet;
const std::string fOldSelection;
const std::string fNewSelection;
const Ufe::Path _path;
UsdVariantSet _varSet;
const std::string _oldSelection;
const std::string _newSelection;
Ufe::Selection _savedSn; // For global selection save and restore.
};

//! \brief Undoable command for prim active state change
Expand Down Expand Up @@ -559,7 +580,7 @@ Ufe::UndoableCommand::Ptr UsdContextOps::doOpCmd(const ItemPath& itemPath)

// At this point we know we have enough arguments to execute the
// operation.
return std::make_shared<SetVariantSelectionUndoableCommand>(prim(), itemPath);
return std::make_shared<SetVariantSelectionUndoableCommand>(path(), prim(), itemPath);
} // Variant sets
else if (itemPath[0] == kUSDToggleVisibilityItem) {
#if UFE_PREVIEW_VERSION_NUM < 2034
Expand Down
34 changes: 34 additions & 0 deletions lib/mayaUsd/ufe/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
#include <maya/MFnDependencyNode.h>
#include <maya/MGlobal.h>
#include <maya/MObjectHandle.h>
#include <ufe/hierarchy.h>
#include <ufe/pathSegment.h>
#include <ufe/rtid.h>
#include <ufe/selection.h>

#include <cassert>
#include <cctype>
Expand Down Expand Up @@ -331,5 +333,37 @@ UsdTimeCode getTime(const Ufe::Path& path)
return proxyShape->getTime();
}

Ufe::Selection removeDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath)
{
// Filter the src selection, removing items below the filterPath
Ufe::Selection dst;
for (const auto& item : src) {
const auto& itemPath = item->path();
// The filterPath itself is still valid.
if (!itemPath.startsWith(filterPath) || itemPath == filterPath) {
dst.append(item);
}
}
return dst;
}

Ufe::Selection recreateDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath)
{
// If a src selection item starts with the filterPath, re-create it.
Ufe::Selection dst;
for (const auto& item : src) {
const auto& itemPath = item->path();
// The filterPath itself is still valid.
if (!itemPath.startsWith(filterPath) || itemPath == filterPath) {
dst.append(item);
} else {
auto recreatedItem = Ufe::Hierarchy::createItem(item->path());
TF_AXIOM(recreatedItem);
dst.append(recreatedItem);
}
}
return dst;
}

} // namespace ufe
} // namespace MAYAUSD_NS_DEF
14 changes: 13 additions & 1 deletion lib/mayaUsd/ufe/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@

PXR_NAMESPACE_USING_DIRECTIVE

UFE_NS_DEF { class PathSegment; }
UFE_NS_DEF
{
class PathSegment;
class Selection;
}

namespace MAYAUSD_NS_DEF {
namespace ufe {
Expand Down Expand Up @@ -148,5 +152,13 @@ inline GfMatrix4d toUsd(const Ufe::Matrix4d& src)
//! Copy the argument vector into the return vector.
inline Ufe::Vector3d toUfe(const GfVec3d& src) { return Ufe::Vector3d(src[0], src[1], src[2]); }

//! Filter a source selection by removing descendants of filterPath.
Ufe::Selection removeDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath);

//! Re-build a source selection by copying scene items that are not descendants
//! of filterPath to the destination, and re-creating the others into the
//! destination using the source scene item path.
Ufe::Selection recreateDescendants(const Ufe::Selection& src, const Ufe::Path& filterPath);

} // namespace ufe
} // namespace MAYAUSD_NS_DEF
Loading