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

Feldstj/usd UI node graph node #2520

Merged
merged 7 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions lib/mayaUsd/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ if(CMAKE_UFE_V4_FEATURES_AVAILABLE)
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4023)
target_sources(${PROJECT_NAME}
PRIVATE
UsdUINodeGraphNode.cpp
UsdUINodeGraphNodeHandler.cpp
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4024)
target_sources(${PROJECT_NAME}
PRIVATE
Expand Down Expand Up @@ -274,6 +282,13 @@ if(CMAKE_UFE_V4_FEATURES_AVAILABLE)
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4023)
list(APPEND HEADERS
UsdUINodeGraphNode.h
UsdUINodeGraphNodeHandler.h
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4024)
list(APPEND HEADERS
UsdUndoAttributesCommands.h
Expand Down
6 changes: 6 additions & 0 deletions lib/mayaUsd/ufe/Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
#if (UFE_PREVIEW_VERSION_NUM >= 4020)
#include <mayaUsd/ufe/UsdConnectionHandler.h>
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4023)
#include <mayaUsd/ufe/UsdUINodeGraphNodeHandler.h>
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4001)
#include <mayaUsd/ufe/UsdShaderNodeDefHandler.h>
#endif
Expand Down Expand Up @@ -190,6 +193,9 @@ MStatus initialize()
#if (UFE_PREVIEW_VERSION_NUM >= 4020)
handlers.connectionHandler = UsdConnectionHandler::create();
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4023)
handlers.uiNodeGraphNodeHandler = UsdUINodeGraphNodeHandler::create();
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4001)
handlers.nodeDefHandler = UsdShaderNodeDefHandler::create();
#endif
Expand Down
102 changes: 102 additions & 0 deletions lib/mayaUsd/ufe/UsdUINodeGraphNode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// =======================================================================
// Copyright 2022 Autodesk, Inc. All rights reserved.
//
// This computer source code and related instructions and comments are the
// unpublished confidential and proprietary information of Autodesk, Inc.
// and are protected under applicable copyright and trade secret law. They
// may not be disclosed to, copied or used by any third party without the
// prior written consent of Autodesk, Inc.
// =======================================================================

#include "UsdUINodeGraphNode.h"

#include <pxr/usd/usdUI/nodeGraphNodeAPI.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

class SetPositionCommand : public Ufe::UndoableCommand
{
public:
SetPositionCommand(const PXR_NS::UsdPrim& prim, const Ufe::Vector2f& newPos)
: Ufe::UndoableCommand()
, _stage(prim.GetStage())
, _primPath(prim.GetPath())
, _newPos(PXR_NS::GfVec2f(newPos.x(), newPos.y()))
{
}

void execute() override
{
PXR_NAMESPACE_USING_DIRECTIVE
if (_stage) {
const UsdPrim prim = _stage->GetPrimAtPath(_primPath);
if (!prim.HasAPI<UsdUINodeGraphNodeAPI>()) {
UsdUINodeGraphNodeAPI::Apply(prim);
}
if (prim.HasAPI<UsdUINodeGraphNodeAPI>()) {
UsdUINodeGraphNodeAPI posApi(prim);
TF_VERIFY(posApi);
UsdAttribute attr = posApi.GetPosAttr();
if (!attr) {
attr = posApi.CreatePosAttr();
}
attr.Set(_newPos);
}
}
}

void undo() override { }
void redo() override { }

private:
const PXR_NS::UsdStageWeakPtr _stage;
const PXR_NS::SdfPath _primPath;
const PXR_NS::VtValue _newPos;
};

UsdUINodeGraphNode::UsdUINodeGraphNode(const UsdSceneItem::Ptr& item)
: Ufe::UINodeGraphNode()
, fItem(item)
{
}

UsdUINodeGraphNode::Ptr UsdUINodeGraphNode::create(const UsdSceneItem::Ptr& item)
{
return std::make_shared<UsdUINodeGraphNode>(item);
}

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

bool UsdUINodeGraphNode::hasPosition() const
{
PXR_NAMESPACE_USING_DIRECTIVE
const UsdPrim prim = fItem->prim();
UsdUINodeGraphNodeAPI posApi(prim);
TF_VERIFY(posApi);
UsdAttribute attr = posApi.GetPosAttr();
return attr.IsValid();
}

Ufe::Vector2f UsdUINodeGraphNode::getPosition() const
{
if (hasPosition()) {
const PXR_NS::UsdPrim prim = fItem->prim();
const PXR_NS::UsdUINodeGraphNodeAPI posApi(prim);
const PXR_NS::UsdAttribute attr = posApi.GetPosAttr();
PXR_NS::VtValue v;
attr.Get(&v);
const PXR_NS::GfVec2f pos = v.Get<PXR_NS::GfVec2f>();
return Ufe::Vector2f(pos[0], pos[1]);
} else {
return Ufe::Vector2f(0.0f, 0.0f);
}
}

Ufe::UndoableCommand::Ptr UsdUINodeGraphNode::setPositionCmd(const Ufe::Vector2f& pos)
{
return std::make_shared<SetPositionCommand>(fItem ? fItem->prim() : PXR_NS::UsdPrim(), pos);
}

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

// =======================================================================
// Copyright 2022 Autodesk, Inc. All rights reserved.
//
// This computer source code and related instructions and comments are the
// unpublished confidential and proprietary information of Autodesk, Inc.
// and are protected under applicable copyright and trade secret law. They
// may not be disclosed to, copied or used by any third party without the
// prior written consent of Autodesk, Inc.
// =======================================================================

#include "UsdSceneItem.h"

#include <mayaUsd/base/api.h>

#include <ufe/uiNodeGraphNode.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

//! \brief Implementation of Ufe::UINodeGraphNode interface for USD objects.
class MAYAUSD_CORE_PUBLIC UsdUINodeGraphNode : public Ufe::UINodeGraphNode
{
public:
typedef std::shared_ptr<UsdUINodeGraphNode> Ptr;

UsdUINodeGraphNode(const UsdSceneItem::Ptr& item);
~UsdUINodeGraphNode() override = default;

UsdUINodeGraphNode(const UsdUINodeGraphNode&) = delete;
UsdUINodeGraphNode& operator=(const UsdUINodeGraphNode&) = delete;
UsdUINodeGraphNode(UsdUINodeGraphNode&&) = delete;
UsdUINodeGraphNode& operator=(UsdUINodeGraphNode&&) = delete;

//! Create a UsdUINodeGraphNode.
static UsdUINodeGraphNode::Ptr create(const UsdSceneItem::Ptr& item);

// Ufe::UsdUINodeGraphNode overrides
Ufe::SceneItem::Ptr sceneItem() const override;
bool hasPosition() const override;
Ufe::Vector2f getPosition() const override;
Ufe::UndoableCommand::Ptr setPositionCmd(const Ufe::Vector2f& pos) override;

private:
UsdSceneItem::Ptr fItem;
};

} // namespace ufe
} // namespace MAYAUSD_NS_DEF

#endif // USDUINODEGRAPHNODE_H
38 changes: 38 additions & 0 deletions lib/mayaUsd/ufe/UsdUINodeGraphNodeHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ===========================================================================
// Copyright 2022 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================

#include "UsdUINodeGraphNodeHandler.h"

#include "UsdUINodeGraphNode.h"

#include <pxr/usd/usdUI/nodeGraphNodeAPI.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

UsdUINodeGraphNodeHandler::Ptr UsdUINodeGraphNodeHandler::create()
{
return std::make_shared<UsdUINodeGraphNodeHandler>();
}

Ufe::UINodeGraphNode::Ptr
UsdUINodeGraphNodeHandler::uiNodeGraphNode(const Ufe::SceneItem::Ptr& item) const
{
PXR_NAMESPACE_USING_DIRECTIVE
UsdSceneItem::Ptr usdItem = std::dynamic_pointer_cast<UsdSceneItem>(item);
TF_VERIFY(usdItem);

const UsdPrim prim = usdItem->prim();
if (prim.IsValid() && UsdUINodeGraphNodeAPI::CanApply(prim)) {
return UsdUINodeGraphNode::create(usdItem);
}
return nullptr;
}

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

// ===========================================================================
// Copyright 2022 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
// ===========================================================================

#include "UsdSceneItem.h"

#include <mayaUsd/base/api.h>

#include <ufe/uiNodeGraphNodeHandler.h>

namespace MAYAUSD_NS_DEF {
namespace ufe {

//! \brief Implementation of Ufe::UINodeGraphNodeHandler interface for USD objects.
class MAYAUSD_CORE_PUBLIC UsdUINodeGraphNodeHandler : public Ufe::UINodeGraphNodeHandler
{
public:
typedef std::shared_ptr<UsdUINodeGraphNodeHandler> Ptr;

UsdUINodeGraphNodeHandler() = default;
~UsdUINodeGraphNodeHandler() override = default;

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

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

// Ufe::UINodeGraphNodeHandler overrides
Ufe::UINodeGraphNode::Ptr uiNodeGraphNode(const Ufe::SceneItem::Ptr& item) const override;
};

} // namespace ufe
} // namespace MAYAUSD_NS_DEF

#endif /* USDUINODEGRAPHNODEHANDLER_H */
6 changes: 6 additions & 0 deletions test/lib/ufe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ if(CMAKE_UFE_V4_FEATURES_AVAILABLE)
)
endif()

if (${UFE_PREVIEW_VERSION_NUM} GREATER_EQUAL 4023)
list(APPEND TEST_SCRIPT_FILES
testUINodeGraphNode.py
)
endif()

if(CMAKE_WANT_MATERIALX_BUILD)
list(APPEND TEST_SCRIPT_FILES
testShaderNodeDef.py
Expand Down
89 changes: 89 additions & 0 deletions test/lib/ufe/testUINodeGraphNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env python

#
# Copyright 2022 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.
#

import fixturesUtils
import mayaUtils

from maya import cmds
from maya import standalone

import ufe

import os
import unittest


class UINodeGraphNodeTestCase(unittest.TestCase):
'''Verify the UINodeGraphNode USD implementation.
'''

pluginsLoaded = False

@classmethod
def setUpClass(cls):
fixturesUtils.readOnlySetUpClass(__file__, loadPlugin=False)

if not cls.pluginsLoaded:
cls.pluginsLoaded = mayaUtils.isMayaUsdPluginLoaded()

@classmethod
def tearDownClass(cls):
standalone.uninitialize()

def setUp(self):
''' Called initially to set up the Maya test environment '''
# Load plugins
self.assertTrue(self.pluginsLoaded)

# Open ballset.ma scene in testSamples
mayaUtils.openGroupBallsScene()

# Clear selection to start off
cmds.select(clear=True)

def testUIInfo(self):
ball3Path = ufe.PathString.path('|transform1|proxyShape1,/Ball_set/Props/Ball_3')
ball3SceneItem = ufe.Hierarchy.createItem(ball3Path)

uiNodeGraphNode = ufe.UINodeGraphNode.uiNodeGraphNode(ball3SceneItem)

self.assertFalse(uiNodeGraphNode.hasPosition())
pos0 = uiNodeGraphNode.getPosition()
self.assertEqual(pos0.x(), 0)
self.assertEqual(pos0.y(), 0)
pos1 = ufe.Vector2f(10, 20)
uiNodeGraphNode.setPosition(pos1)
self.assertTrue(uiNodeGraphNode.hasPosition())
pos2 = uiNodeGraphNode.getPosition()
self.assertEqual(pos1.x(), pos2.x())
self.assertEqual(pos1.y(), pos2.y())
uiNodeGraphNode.setPosition(13, 41)
self.assertTrue(uiNodeGraphNode.hasPosition())
pos3 = uiNodeGraphNode.getPosition()
self.assertEqual(pos3.x(), 13)
self.assertEqual(pos3.y(), 41)
pos4 = ufe.Vector2f(21, 20)
cmd = uiNodeGraphNode.setPositionCmd(pos4)
cmd.execute()
self.assertTrue(uiNodeGraphNode.hasPosition())
pos5 = uiNodeGraphNode.getPosition()
self.assertEqual(pos4.x(), pos5.x())
self.assertEqual(pos4.y(), pos5.y())

if __name__ == '__main__':
unittest.main(verbosity=2)