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-125039 handle rename and reparent in the orphaned nodes manager #2690

Merged
merged 7 commits into from
Nov 23, 2022
107 changes: 96 additions & 11 deletions lib/mayaUsd/fileio/orphanedNodesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,87 @@ Ufe::Trie<OrphanedNodesManager::PullVariantInfo> OrphanedNodesManager::Memento::
// Class OrphanedNodesManager
//------------------------------------------------------------------------------

namespace {

using PullVariantInfo = OrphanedNodesManager::PullVariantInfo;
using VariantSetDescriptor = OrphanedNodesManager::VariantSetDescriptor;
using VariantSelection = OrphanedNodesManager::VariantSelection;

Ufe::Path trieNodeToPulledPrimUfePath(Ufe::TrieNode<PullVariantInfo>::Ptr trieNode);

void renameVariantDescriptors(
std::list<VariantSetDescriptor>& descriptors,
const Ufe::Path& oldPath,
const Ufe::Path& newPath)
{
std::list<VariantSetDescriptor> newDescriptors;
for (VariantSetDescriptor& desc : descriptors) {
if (desc.path.startsWith(oldPath)) {
desc.path = desc.path.reparent(oldPath, newPath);
}
}
}

void renameVariantInfo(
const Ufe::TrieNode<PullVariantInfo>::Ptr& trieNode,
const Ufe::Path& oldPath,
const Ufe::Path& newPath)
{
// Note: TrieNode has no non-const data() function, so to modify the
// data we must make a copy, modify the copy and call setData().
PullVariantInfo newVariantInfo = trieNode->data();

// Note: the change to USD data must be done *after* changes to Maya data because
// the outliner reacts to UFE notifications received following the USD edits
// to rebuild the node tree and the Maya node we want to hide must have been
// hidden by that point. So the node visibility change must be done *first*.
renameVariantDescriptors(newVariantInfo.variantSetDescriptors, oldPath, newPath);

Ufe::Path pulledPath = trieNodeToPulledPrimUfePath(trieNode);
TF_VERIFY(writePullInformation(pulledPath, newVariantInfo.editedAsMayaRoot));

trieNode->setData(newVariantInfo);
}

void recursiveRename(
const Ufe::TrieNode<PullVariantInfo>::Ptr& trieNode,
const Ufe::Path& oldPath,
const Ufe::Path& newPath)
{
if (trieNode->hasData()) {
renameVariantInfo(trieNode, oldPath, newPath);
} else {
auto childrenComponents = trieNode->childrenComponents();
for (auto& c : childrenComponents) {
recursiveRename((*trieNode)[c], oldPath, newPath);
}
}
}

void handlePathChange(
const Ufe::Path& oldPath,
const Ufe::SceneItem::Ptr& item,
Ufe::Trie<PullVariantInfo>& pulledPrims)
{
if (!item)
return;

auto trieNode = pulledPrims.node(oldPath);
if (trieNode) {
const Ufe::Path& newPath = item->path();
// If the only change is the last part of the UFE path, then
// we are dealing with a rename. Else it is a reparent.
if (newPath.pop() == oldPath.pop()) {
trieNode->rename(newPath.back());
} else {
pulledPrims.move(oldPath, newPath);
}
recursiveRename(trieNode, oldPath, newPath);
}
}

} // namespace

OrphanedNodesManager::OrphanedNodesManager()
: _pulledPrims()
{
Expand Down Expand Up @@ -132,6 +213,10 @@ void OrphanedNodesManager::operator()(const Ufe::Notification& n)
auto subtrInv = dynamic_cast<const Ufe::SubtreeInvalidate*>(&sceneNotification)) {
handleOp(Ufe::SceneCompositeNotification::Op(
Ufe::SceneCompositeNotification::OpType::SubtreeInvalidate, subtrInv->root()));
} else if (auto objRename = dynamic_cast<const Ufe::ObjectRename*>(&sceneNotification)) {
handlePathChange(objRename->previousPath(), objRename->item(), _pulledPrims);
} else if (auto objRep = dynamic_cast<const Ufe::ObjectReparent*>(&sceneNotification)) {
handlePathChange(objRep->previousPath(), objRep->item(), _pulledPrims);
}
#endif
}
Expand Down Expand Up @@ -242,22 +327,23 @@ void OrphanedNodesManager::handleOp(const Ufe::SceneCompositeNotification::Op& o
}
}
} break;
#ifdef UFE_V4_FEATURES_AVAILABLE
case Ufe::SceneCompositeNotification::OpType::ObjectPathChange: {
if (op.subOpType == Ufe::ObjectPathChange::ObjectRename
|| op.subOpType == Ufe::ObjectPathChange::ObjectReparent) {
handlePathChange(op.path, op.item, _pulledPrims);
}
} break;
#endif
default: {
// ObjectPathChange (reparent, rename): to be implemented (MAYA-125039).
// SceneCompositeNotification: already expanded in operator().
}
}
}

Ufe::Trie<OrphanedNodesManager::PullVariantInfo>& OrphanedNodesManager::pulledPrims()
{
return _pulledPrims;
}
Ufe::Trie<PullVariantInfo>& OrphanedNodesManager::pulledPrims() { return _pulledPrims; }

const Ufe::Trie<OrphanedNodesManager::PullVariantInfo>& OrphanedNodesManager::pulledPrims() const
{
return _pulledPrims;
}
const Ufe::Trie<PullVariantInfo>& OrphanedNodesManager::pulledPrims() const { return _pulledPrims; }

void OrphanedNodesManager::clear() { pulledPrims().clear(); }

Expand Down Expand Up @@ -479,8 +565,7 @@ OrphanedNodesManager::variantSetDescriptors(const Ufe::Path& p)
}

/* static */
Ufe::Trie<OrphanedNodesManager::PullVariantInfo>
OrphanedNodesManager::deepCopy(const Ufe::Trie<PullVariantInfo>& src)
Ufe::Trie<PullVariantInfo> OrphanedNodesManager::deepCopy(const Ufe::Trie<PullVariantInfo>& src)
{
Ufe::Trie<PullVariantInfo> dst;
deepCopy(src.root(), dst.root());
Expand Down
13 changes: 13 additions & 0 deletions lib/mayaUsd/fileio/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ target_sources(${PROJECT_NAME}
xformStack.cpp
)

if(UFE_TRIE_NODE_HAS_CHILDREN_COMPONENTS_ACCESSOR)
target_sources(${PROJECT_NAME}
PRIVATE
orphanedNodesManagerUtil.cpp
)
endif()

set(HEADERS
adaptor.h
jointWriteUtils.h
Expand All @@ -28,6 +35,12 @@ set(HEADERS
xformStack.h
)

if(UFE_TRIE_NODE_HAS_CHILDREN_COMPONENTS_ACCESSOR)
list(APPEND HEADERS
orphanedNodesManagerUtil.h
)
endif()

# -----------------------------------------------------------------------------
# promoted headers
# -----------------------------------------------------------------------------
Expand Down
132 changes: 132 additions & 0 deletions lib/mayaUsd/fileio/utils/orphanedNodesManagerUtil.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//
// 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.
//
#include "orphanedNodesManagerUtil.h"

#include <maya/MGlobal.h>
#include <ufe/trie.imp.h>

namespace MAYAUSD_NS_DEF {

namespace {

using PullVariantInfo = OrphanedNodesManager::PullVariantInfo;
using VariantSetDescriptor = OrphanedNodesManager::VariantSetDescriptor;
using VariantSelection = OrphanedNodesManager::VariantSelection;

void addIndent(std::string& buf, int indent)
{
for (int i = 0; i < indent; ++i)
buf += " ";
}

void toText(std::string& buf, const char* pfix, const char* text, int indent, bool eol)
{
addIndent(buf, indent);
if (pfix && pfix[0]) {
buf += pfix;
buf += ": ";
}
buf += text;
if (eol)
buf += "\n";
}

void toText(std::string& buf, const char* pfix, const std::string& text, int indent, bool eol)
{
toText(buf, pfix, text.c_str(), indent, eol);
}

void toText(std::string& buf, const char* pfix, const MDagPath& dagPath, int indent, bool eol)
{
toText(buf, pfix, dagPath.fullPathName().asChar(), indent, eol);
}

void toText(std::string& buf, const char* pfix, const Ufe::Path& ufePath, int indent, bool eol)
{
toText(buf, pfix, ufePath.string(), indent, eol);
}

void toText(std::string& buf, const VariantSelection& sel, int indent, bool eol)
{
toText(buf, "Variant ", sel.variantSetName, indent, eol);
toText(buf, "Selection", sel.variantSelection, indent, eol);
}

void toText(std::string& buf, const VariantSetDescriptor& descriptor, int indent, bool eol)
{
toText(buf, "Variant selections", descriptor.path, indent, eol);

indent += 1;

for (const auto& variantSel : descriptor.variantSelections) {
toText(buf, variantSel, indent, eol);
}

indent -= 1;
}

void toText(std::string& buf, const PullVariantInfo& variantInfo, int indent, bool eol)
{
toText(buf, "", "{", indent, eol);
indent += 1;

toText(buf, "Edited Maya root", variantInfo.editedAsMayaRoot, indent, eol);

for (const auto& desc : variantInfo.variantSetDescriptors)
toText(buf, desc, indent, eol);

indent -= 1;
toText(buf, "", "}", indent, eol);
}

} // namespace

void orphanedNodesManagerPullInfoToText(
std::string& buffer,
const Ufe::TrieNode<PullVariantInfo>::Ptr& trieNode,
int indent,
bool eol)
{
if (!trieNode)
return;

const Ufe::TrieNode<PullVariantInfo>& node = *trieNode;

toText(buffer, "", node.component().string(), indent, eol);

if (node.hasData()) {
toText(buffer, node.data(), indent, eol);
}

for (const auto& childComp : node.childrenComponents()) {
orphanedNodesManagerPullInfoToText(buffer, node[childComp], indent + 1, eol);
}

if (eol)
buffer += "\n";
}

void printOrphanedNodesManagerPullInfo(
const Ufe::TrieNode<PullVariantInfo>::Ptr& trieNode,
int indent,
bool eol)
{
std::string buffer("Trie ==========================================\n");
orphanedNodesManagerPullInfoToText(buffer, trieNode, indent, eol);
MGlobal::displayInfo(buffer.c_str());
}

} // namespace MAYAUSD_NS_DEF
34 changes: 34 additions & 0 deletions lib/mayaUsd/fileio/utils/orphanedNodesManagerUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// 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.
//

#pragma once

#include <mayaUsd/fileio/orphanedNodesManager.h>

namespace MAYAUSD_NS_DEF {

void orphanedNodesManagerPullInfoToText(
std::string& buffer,
const Ufe::TrieNode<OrphanedNodesManager::PullVariantInfo>::Ptr& trieNode,
int indent = 0,
bool eol = true);

void printOrphanedNodesManagerPullInfo(
const Ufe::TrieNode<OrphanedNodesManager::PullVariantInfo>::Ptr& trieNode,
int indent = 0,
bool eol = true);

} // namespace MAYAUSD_NS_DEF
Loading