Skip to content

Commit

Permalink
Merge pull request #1715 from Autodesk/donnels/MAYA-112768/channel_bo…
Browse files Browse the repository at this point in the history
…x_for_usd_prims

MAYA-112768 - Channel box for USD prims
  • Loading branch information
Krystian Ligenza authored Sep 21, 2021
2 parents 94697d8 + e726f0c commit f072bcb
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/mayaUsd/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ if(CMAKE_UFE_V2_FEATURES_AVAILABLE)
UsdTransform3dSetObjectMatrix.cpp
UsdTransform3dUndoableCommands.cpp
UsdUIInfoHandler.cpp
UsdUIUfeObserver.cpp
UsdUndoAddNewPrimCommand.cpp
UsdUndoCreateGroupCommand.cpp
UsdUndoInsertChildCommand.cpp
Expand Down Expand Up @@ -129,6 +130,7 @@ if(CMAKE_UFE_V2_FEATURES_AVAILABLE)
UsdTransform3dSetObjectMatrix.h
UsdTransform3dUndoableCommands.h
UsdUIInfoHandler.h
UsdUIUfeObserver.h
UsdUndoableCommand.h
UsdUndoAddNewPrimCommand.h
UsdUndoCreateGroupCommand.h
Expand Down
4 changes: 4 additions & 0 deletions lib/mayaUsd/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <mayaUsd/ufe/UsdTransform3dMayaXformStack.h>
#include <mayaUsd/ufe/UsdTransform3dPointInstance.h>
#include <mayaUsd/ufe/UsdUIInfoHandler.h>
#include <mayaUsd/ufe/UsdUIUfeObserver.h>
#endif

#include <ufe/hierarchyHandler.h>
Expand Down Expand Up @@ -150,6 +151,7 @@ MStatus initialize()
handlers.transform3dHandler = pointInstanceHandler;

g_USDRtid = Ufe::RunTimeMgr::instance().register_(kUSDRunTimeName, handlers);
MayaUsd::ufe::UsdUIUfeObserver::create();
#else
auto usdHierHandler = UsdHierarchyHandler::create();
auto usdTrans3dHandler = UsdTransform3dHandler::create();
Expand Down Expand Up @@ -185,6 +187,8 @@ MStatus finalize()
if (g_MayaContextOpsHandler)
Ufe::RunTimeMgr::instance().setContextOpsHandler(g_MayaRtid, g_MayaContextOpsHandler);
g_MayaContextOpsHandler.reset();

MayaUsd::ufe::UsdUIUfeObserver::destroy();
#endif
Ufe::RunTimeMgr::instance().unregister(g_USDRtid);
g_MayaHierarchyHandler.reset();
Expand Down
91 changes: 91 additions & 0 deletions lib/mayaUsd/ufe/UsdUIUfeObserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
//
// Copyright 2021 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 "UsdUIUfeObserver.h"

#include <pxr/base/tf/diagnostic.h>
#include <pxr/usd/usdGeom/tokens.h>

#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MStringArray.h>
#include <ufe/attributes.h>
#include <ufe/pathString.h>

// Needed because of TF_VERIFY
PXR_NAMESPACE_USING_DIRECTIVE

namespace MAYAUSD_NS_DEF {
namespace ufe {

//------------------------------------------------------------------------------
// Global variables
//------------------------------------------------------------------------------
Ufe::Observer::Ptr UsdUIUfeObserver::ufeObserver;

//------------------------------------------------------------------------------
// UsdUIUfeObserver
//------------------------------------------------------------------------------

UsdUIUfeObserver::UsdUIUfeObserver()
: Ufe::Observer()
{
}

/*static*/
void UsdUIUfeObserver::create()
{
TF_VERIFY(!ufeObserver);
if (!ufeObserver) {
ufeObserver = std::make_shared<UsdUIUfeObserver>();
Ufe::Attributes::addObserver(ufeObserver);
}
}

/*static*/
void UsdUIUfeObserver::destroy()
{
TF_VERIFY(ufeObserver);
if (ufeObserver) {
Ufe::Attributes::removeObserver(ufeObserver);
ufeObserver.reset();
}
}

//------------------------------------------------------------------------------
// Ufe::Observer overrides
//------------------------------------------------------------------------------

void UsdUIUfeObserver::operator()(const Ufe::Notification& notification)
{
if (auto ac = dynamic_cast<const Ufe::AttributeValueChanged*>(&notification)) {
if (ac->name() == UsdGeomTokens->xformOpOrder) {
static const MString mainObjListCmd(
"if (`channelBox -exists mainChannelBox`) channelBox -q -mainObjectList "
"mainChannelBox;");
MStringArray paths;
if (MGlobal::executeCommand(mainObjListCmd, paths) && (paths.length() > 0)) {
auto ufePath = Ufe::PathString::path(paths[0].asChar());
if (ufePath.startsWith(ac->path())) {
static const MString updateCBCmd("channelBox -e -update mainChannelBox;");
MGlobal::executeCommand(updateCBCmd);
}
}
}
}
}

} // namespace ufe
} // namespace MAYAUSD_NS_DEF
52 changes: 52 additions & 0 deletions lib/mayaUsd/ufe/UsdUIUfeObserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef USDUIUFEOBSERVER_H
#define USDUIUFEOBSERVER_H

//
// Copyright 2021 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 <mayaUsd/base/api.h>

#include <ufe/observer.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

//! \brief Helper class used to receive UFE notifications and respond to them by updating UI.
class UsdUIUfeObserver : public Ufe::Observer
{
public:
UsdUIUfeObserver();

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

//! Create/Destroy a UsdUIUfeObserver.
static void create();
static void destroy();

void operator()(const Ufe::Notification& notification) override;

private:
static Ufe::Observer::Ptr ufeObserver;
};

} // namespace ufe
} // namespace MAYAUSD_NS_DEF

#endif // USDUIUFEOBSERVER_H
8 changes: 8 additions & 0 deletions plugin/adsk/scripts/mayaUsd_pluginUICreation.mel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ global proc mayaUsd_pluginUICreation()

source "mayaUsd_fileOptions.mel";

// Set the default USD attributes we want to display
// in the Channel Box.
global string $gChannelBoxName;
if (`channelBox -exists $gChannelBoxName`) {
// This flag only exists in recent versions of Maya.
catchQuiet(`channelBox -e -ufeFixedAttrList "USD" {"xformOp:translate*", "xformOp:rotate*", "xformOp:scale*", "xformOp:*", "visibility"} $gChannelBoxName`);
}

int $mjv = `about -majorVersion`;

if (((2021 <= $mjv)) && (!`pluginInfo -q -loaded "ufeSupport"`)) {
Expand Down
10 changes: 6 additions & 4 deletions test/lib/ufe/testAttribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,9 @@ def testObservation(self):
(ball34Obs, ball35Obs, globalObs) = [TestObserver() for i in range(3)]

# Maya registers a single global observer on startup.
self.assertEqual(ufe.Attributes.nbObservers(), 1)
# Maya-Usd lib registers a single global observer when it is initialized.
kNbGlobalObs = 2
self.assertEqual(ufe.Attributes.nbObservers(), kNbGlobalObs)

# No item-specific observers.
self.assertFalse(ufe.Attributes.hasObservers(ball34.path()))
Expand All @@ -478,7 +480,7 @@ def testObservation(self):
# Add a global observer.
ufe.Attributes.addObserver(globalObs)

self.assertEqual(ufe.Attributes.nbObservers(), 2)
self.assertEqual(ufe.Attributes.nbObservers(), kNbGlobalObs+1)
self.assertFalse(ufe.Attributes.hasObservers(ball34.path()))
self.assertFalse(ufe.Attributes.hasObservers(ball35.path()))
self.assertEqual(ufe.Attributes.nbObservers(ball34), 0)
Expand All @@ -489,7 +491,7 @@ def testObservation(self):
# Add item-specific observers.
ufe.Attributes.addObserver(ball34, ball34Obs)

self.assertEqual(ufe.Attributes.nbObservers(), 2)
self.assertEqual(ufe.Attributes.nbObservers(), kNbGlobalObs+1)
self.assertTrue(ufe.Attributes.hasObservers(ball34.path()))
self.assertFalse(ufe.Attributes.hasObservers(ball35.path()))
self.assertEqual(ufe.Attributes.nbObservers(ball34), 1)
Expand Down Expand Up @@ -594,7 +596,7 @@ def testObservation(self):

ufe.Attributes.removeObserver(globalObs)

self.assertEqual(ufe.Attributes.nbObservers(), 1)
self.assertEqual(ufe.Attributes.nbObservers(), kNbGlobalObs)

ufeCmd.execute(ball34XlateAttr.setCmd(ufe.Vector3d(7, 8, 9)))

Expand Down

0 comments on commit f072bcb

Please sign in to comment.