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

Autodesk/tremblp/maya 103495/contextual operations #303

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,12 @@ if(UFE_FOUND)
ufe/UsdObject3dHandler.cpp
ufe/UsdUndoCreateGroupCommand.cpp
)
if(UFE_PREVIEW_VERSION_NUM GREATER_EQUAL 2009)
list(APPEND mayaUsd_src
ufe/UsdContextOps.cpp
ufe/UsdContextOpsHandler.cpp
)
endif()
endif()
endif()

Expand Down Expand Up @@ -388,6 +394,8 @@ if(UFE_FOUND)
ufe/UsdAttribute.h
ufe/UsdAttributes.h
ufe/UsdAttributesHandler.h
ufe/UsdContextOps.h
ufe/UsdContextOpsHandler.h
ufe/UsdObject3d.h
ufe/UsdObject3dHandler.h
ufe/UsdUndoCreateGroupCommand.h
Expand Down
12 changes: 11 additions & 1 deletion lib/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
// Note: must come after include of ufe files so we have the define.
#include "UsdAttributesHandler.h"
#include "UsdObject3dHandler.h"
#if UFE_PREVIEW_VERSION_NUM >= 2009
#include "UsdContextOpsHandler.h"
#endif
#else
#include "UfeVersionCompat.h"
#endif
Expand Down Expand Up @@ -97,10 +100,17 @@ MStatus initialize()
auto usdSceneItemOpsHandler = UsdSceneItemOpsHandler::create();
UFE_V2(auto usdAttributesHandler = UsdAttributesHandler::create();)
UFE_V2(auto usdObject3dHandler = UsdObject3dHandler::create();)
#if UFE_PREVIEW_VERSION_NUM >= 2009
UFE_V2(auto usdContextOpsHandler = UsdContextOpsHandler::create();)
#endif
g_USDRtid = Ufe::RunTimeMgr::instance().register_(
kUSDRunTimeName, usdHierHandler, usdTrans3dHandler,
usdSceneItemOpsHandler
UFE_V2(, usdAttributesHandler, usdObject3dHandler));
UFE_V2(, usdAttributesHandler, usdObject3dHandler)
#if UFE_PREVIEW_VERSION_NUM >= 2009
UFE_V2(, usdContextOpsHandler)
#endif
);

#if !defined(NDEBUG)
assert(g_USDRtid != 0);
Expand Down
187 changes: 187 additions & 0 deletions lib/ufe/UsdContextOps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
//
// 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 "UsdContextOps.h"
#include "Utils.h"

#include <ufe/attributes.h>
#include <ufe/attribute.h>

#include <pxr/usd/usd/variantSets.h>

#include <maya/MGlobal.h>

#include <cassert>

namespace {

class SetVariantSelectionUndoableCommand : public Ufe::UndoableCommand
{
public:

SetVariantSelectionUndoableCommand(
const UsdPrim& prim,
const Ufe::ContextOps::ItemPath& itemPath
) : fVarSet(prim.GetVariantSets().GetVariantSet(itemPath[1])),
fOldSelection(fVarSet.GetVariantSelection()),
fNewSelection(itemPath[2])
{}

void undo() override { fVarSet.SetVariantSelection(fOldSelection); }

void redo() override { fVarSet.SetVariantSelection(fNewSelection); }

private:

UsdVariantSet fVarSet;
const std::string fOldSelection;
const std::string fNewSelection;
};

}

MAYAUSD_NS_DEF {
namespace ufe {

UsdContextOps::UsdContextOps()
: Ufe::ContextOps()
{
}

UsdContextOps::~UsdContextOps()
{
}

/*static*/
UsdContextOps::Ptr UsdContextOps::create()
{
return std::make_shared<UsdContextOps>();
}

void UsdContextOps::setItem(const UsdSceneItem::Ptr& item)
{
fPrim = item->prim();
fItem = item;
}

const Ufe::Path& UsdContextOps::path() const
{
return fItem->path();
}

//------------------------------------------------------------------------------
// Ufe::ContextOps overrides
//------------------------------------------------------------------------------

Ufe::SceneItem::Ptr UsdContextOps::sceneItem() const
{
return fItem;
}

Ufe::ContextOps::Items UsdContextOps::getItems(
const Ufe::ContextOps::ItemPath& itemPath
) const
{
Ufe::ContextOps::Items items;
if (itemPath.empty()) {
// Top-level items. Variant sets and visibility.
if (fPrim.HasVariantSets()) {
items.emplace_back(
"Variant Sets", "Variant Sets", Ufe::ContextItem::kHasChildren);
}
auto attributes = Ufe::Attributes::attributes(sceneItem());
if (attributes) {
auto visibility =
std::dynamic_pointer_cast<Ufe::AttributeEnumString>(
attributes->attribute("visibility"));
if (visibility) {
auto current = visibility->get();
const std::string l = (current == "invisible") ?
std::string("Make Visible") : std::string("Make Invisible");
items.emplace_back("Toggle Visibility", l);
}
}
}
else {
if (itemPath[0] == "Variant Sets") {
UsdVariantSets varSets = fPrim.GetVariantSets();
std::vector<std::string> varSetsNames;
varSets.GetNames(&varSetsNames);

if (itemPath.size() == 1u) {
// Variant sets list.
for (auto i = varSetsNames.crbegin(); i != varSetsNames.crend(); ++i) {

items.emplace_back(*i, *i, Ufe::ContextItem::kHasChildren);
}
}
else {
// Variants of a given variant set. Second item in the path is
// the variant set name.
assert(itemPath.size() == 2u);

UsdVariantSet varSet = varSets.GetVariantSet(itemPath[1]);
auto selected = varSet.GetVariantSelection();

const auto varNames = varSet.GetVariantNames();
for (const auto& vn : varNames) {
const bool checked(vn == selected);
items.emplace_back(vn, vn, Ufe::ContextItem::kNoChildren,
Ufe::ContextItem::kCheckable, checked,
Ufe::ContextItem::kExclusive);
}
} // Variants of a variant set
} // Variant sets
} // Top-level items

return items;
}

Ufe::UndoableCommand::Ptr UsdContextOps::doOpCmd(const ItemPath& itemPath)
{
// Empty argument means no operation was specified, error.
if (itemPath.empty()) {
return nullptr;
}

if (itemPath[0] == "Variant Sets") {
// Operation is to set a variant in a variant set. Need both the
// variant set and the variant as arguments to the operation.
if (itemPath.size() != 3u) {
return nullptr;
}

// At this point we know we have enough arguments to execute the
// operation.
return std::make_shared<SetVariantSelectionUndoableCommand>(
fPrim, itemPath);
} // Variant sets
else if (itemPath[0] == "Toggle Visibility") {
auto attributes = Ufe::Attributes::attributes(sceneItem());
assert(attributes);
auto visibility = std::dynamic_pointer_cast<Ufe::AttributeEnumString>(
attributes->attribute("visibility"));
assert(visibility);
auto current = visibility->get();
return visibility->setCmd(
current == "invisible" ? "inherited" : "invisible");
} // Visibility

return nullptr;
}

} // namespace ufe
} // namespace MayaUsd
65 changes: 65 additions & 0 deletions lib/ufe/UsdContextOps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// 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.
//
#pragma once

#include "../base/api.h"

#include "UsdSceneItem.h"

#include <ufe/path.h>
#include <ufe/contextOps.h>

#include <pxr/usd/usd/prim.h>

PXR_NAMESPACE_USING_DIRECTIVE

MAYAUSD_NS_DEF {
namespace ufe {

//! \brief Interface for scene item operations.
class MAYAUSD_CORE_PUBLIC UsdContextOps : public Ufe::ContextOps
{
public:
typedef std::shared_ptr<UsdContextOps> Ptr;

UsdContextOps();
~UsdContextOps() override;

// Delete the copy/move constructors assignment operators.
UsdContextOps(const UsdContextOps&) = delete;
UsdContextOps& operator=(const UsdContextOps&) = delete;
UsdContextOps(UsdContextOps&&) = delete;
UsdContextOps& operator=(UsdContextOps&&) = delete;

//! Create a UsdContextOps.
static UsdContextOps::Ptr create();

void setItem(const UsdSceneItem::Ptr& item);
const Ufe::Path& path() const;

// Ufe::ContextOps overrides
Ufe::SceneItem::Ptr sceneItem() const override;
Items getItems(const ItemPath& itemPath) const override;
Ufe::UndoableCommand::Ptr doOpCmd(const ItemPath& itemPath) override;

private:
UsdSceneItem::Ptr fItem;
UsdPrim fPrim;

}; // UsdContextOps

} // namespace ufe
} // namespace MayaUsd
53 changes: 53 additions & 0 deletions lib/ufe/UsdContextOpsHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// 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 "UsdContextOpsHandler.h"

MAYAUSD_NS_DEF {
namespace ufe {

UsdContextOpsHandler::UsdContextOpsHandler()
: Ufe::ContextOpsHandler()
{
fUsdContextOps = UsdContextOps::create();
}

UsdContextOpsHandler::~UsdContextOpsHandler()
{
}

/*static*/
UsdContextOpsHandler::Ptr UsdContextOpsHandler::create()
{
return std::make_shared<UsdContextOpsHandler>();
}

//------------------------------------------------------------------------------
// Ufe::ContextOpsHandler overrides
//------------------------------------------------------------------------------

Ufe::ContextOps::Ptr UsdContextOpsHandler::contextOps(const Ufe::SceneItem::Ptr& item) const
{
UsdSceneItem::Ptr usdItem = std::dynamic_pointer_cast<UsdSceneItem>(item);
#if !defined(NDEBUG)
assert(usdItem);
#endif
fUsdContextOps->setItem(usdItem);
return fUsdContextOps;
}

} // namespace ufe
} // namespace MayaUsd
Loading