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

LOOKDEVX-862- Rename ports #2644

Merged
merged 26 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c7a5db0
Add first version for renameAttribute function
alicedegirolamo Sep 26, 2022
465b089
Add RenameAttributeCommand
alicedegirolamo Sep 26, 2022
2dbce29
Add canRenameAttribute
alicedegirolamo Sep 26, 2022
78ec066
Add missing check for material outputs
alicedegirolamo Oct 6, 2022
3cae843
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Oct 6, 2022
542290c
Add override renameAttributeCmd function
alicedegirolamo Oct 7, 2022
5b3df2e
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Oct 11, 2022
0788a0b
Add missing braces
alicedegirolamo Oct 11, 2022
cf792ac
Start recreating connections
alicedegirolamo Oct 12, 2022
ff741b9
Use alias for AddAttributeCommand
alicedegirolamo Oct 13, 2022
14ea1fd
Recreate connections for renamed attribute
alicedegirolamo Oct 13, 2022
781c8e6
Rename bool
alicedegirolamo Oct 13, 2022
3702743
Move connectedApi check
alicedegirolamo Oct 17, 2022
1f643bb
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Oct 17, 2022
2066d6a
Handle name uniqueness
alicedegirolamo Oct 17, 2022
8bf3540
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Oct 21, 2022
8951dae
PR corrections
alicedegirolamo Oct 26, 2022
37a108b
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Oct 26, 2022
1cba9b8
Add missing #ifdef for new attribute cmd typedef
alicedegirolamo Nov 2, 2022
25a2f4d
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Nov 2, 2022
69563dd
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Nov 4, 2022
9fc697d
Update to ufe 0.4.34
alicedegirolamo Nov 4, 2022
dfb9353
Fix compound renaming connections
alicedegirolamo Nov 8, 2022
cfac06c
Update test for renaming attribute and testing connections
alicedegirolamo Nov 8, 2022
226b5c5
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Nov 8, 2022
92e5955
Merge branch 'dev' into degiroa/LOOKDEVX-862/rename-and-delete-ports
alicedegirolamo Nov 9, 2022
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
132 changes: 130 additions & 2 deletions lib/mayaUsd/ufe/UsdAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,32 @@ bool UsdAttributes::hasAttribute(const std::string& name) const

#ifdef UFE_V4_FEATURES_AVAILABLE
#if (UFE_PREVIEW_VERSION_NUM >= 4024)
Ufe::AddAttributeCommand::Ptr
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
Ufe::AddAttributeUndoableCommand::Ptr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it was mentioned in other comments. You'll need an extra ifdef here for >= 4033 check to use "AddAttributeUndoableCommand" (keeping AddAttibuteCommand for >= 4024).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I should have added the necessary ifdef for the new typedef

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its okay, but personally I would have just wrapped the return value in the if/else

UsdAttributes::addAttributeCmd(const std::string& name, const Ufe::Attribute::Type& type)
{
return UsdAddAttributeCommand::create(fItem, name, type);
}
#else

Ufe::AddAttributeCommand::Ptr
UsdAttributes::addAttributeCmd(const std::string& name, const Ufe::Attribute::Type& type)
{
return UsdAddAttributeCommand::create(fItem, name, type);
}
#endif
Ufe::UndoableCommand::Ptr UsdAttributes::removeAttributeCmd(const std::string& name)
{
return UsdRemoveAttributeCommand::create(fItem, name);
}
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
Ufe::RenameAttributeUndoableCommand::Ptr
UsdAttributes::renameAttributeCmd(const std::string& originalName, const std::string& newName)
{
return UsdRenameAttributeCommand::create(fItem, originalName, newName);
}
#endif
#endif

#ifdef UFE_V4_FEATURES_AVAILABLE
Expand Down Expand Up @@ -473,7 +488,120 @@ bool UsdAttributes::doRemoveAttribute(const UsdSceneItem::Ptr& item, const std::
return false;
}
#endif
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
bool UsdAttributes::canRenameAttribute(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName)
{
// No need to rename the attribute.
if (originalName == newName) {
return false;
}
// Renaming meets the same conditions as attribute removal.
return canRemoveAttribute(sceneItem, originalName);
}

static void setConnections(
const PXR_NS::UsdPrim& prim,
const SdfPath& OldPropertyPath,
const SdfPath& newPropertyPath)
{
// Update the connections with the new attribute name.
for (const auto& node : prim.GetChildren()) {
for (auto& attribute : node.GetAttributes()) {
PXR_NS::UsdAttribute attr = attribute.As<PXR_NS::UsdAttribute>();
PXR_NS::SdfPathVector sources;
attr.GetConnections(&sources);
bool hasChanged = false;
// Check if the node attribute is connected to the original property path.
for (size_t i = 0; i < sources.size(); ++i) {
if (sources[i] == OldPropertyPath) {
sources[i] = newPropertyPath;
hasChanged = true;
}
}
// Update the connections with the new property path.
if (hasChanged) {
attr.SetConnections(sources);
}
}
}
}

Ufe::Attribute::Ptr UsdAttributes::doRenameAttribute(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName)
{
// Avoid checks since we have already did them.
PXR_NS::TfToken nameAsToken(originalName);
auto prim = sceneItem->prim();
auto attribute = prim.GetAttribute(nameAsToken);
PXR_NS::UsdShadeConnectableAPI connectApi(prim);
UsdPrim primParent = prim.GetParent();

// Ensure the newName is unique.
const std::string uniqueNewName = UsdAttributes::getUniqueAttrName(sceneItem, newName);

PXR_NS::UsdEditTarget editTarget = prim.GetStage()->GetEditTarget();
const PXR_NS::TfToken kOldAttrName = attribute.GetName();
const SdfPath kPrimPath = attribute.GetPrim().GetPath();
const SdfPath kPropertyPath = kPrimPath.AppendProperty(attribute.GetName());
auto propertyHandle = editTarget.GetPropertySpecForScenePath(kPropertyPath);
auto baseNameAndType = PXR_NS::UsdShadeUtils::GetBaseNameAndType(nameAsToken);

prim.GetAttributes();
PXR_NS::UsdShadeNodeGraph ngPrim(prim);

if (!propertyHandle) {
return {};
}

UsdShadeSourceInfoVector sourcesInfo;

// Save the connected sources since after the renaming we will lose them.
if (connectApi) {
sourcesInfo = connectApi.GetConnectedSources(attribute);
}

if (!propertyHandle->SetName(uniqueNewName)) {
return {};
}

// Get the renamed attribute.
auto renamedAttr = UsdAttributes(sceneItem).attribute(uniqueNewName);

if (connectApi && ngPrim) {

const PXR_NS::TfToken kNewNameAsToken = PXR_NS::TfToken(uniqueNewName);
PXR_NS::UsdAttribute usdRenamedAttribute = prim.GetAttribute(kNewNameAsToken);
const SdfPath kOldPropertyPath = kPrimPath.AppendProperty(kOldAttrName);
const SdfPath kNewPropertyPath = kPrimPath.AppendProperty(kNewNameAsToken);

if (!sourcesInfo.empty()) {
std::vector<UsdShadeConnectionSourceInfo> connectionsInfo;

for (const auto& connectionInfo : sourcesInfo) {
connectionsInfo.push_back(connectionInfo);
}

UsdShadeConnectableAPI::SetConnectedSources(usdRenamedAttribute, connectionsInfo);
}

// Given the unidirectional nature of connections, we discriminate whether the source is
// input or output
if (baseNameAndType.second == PXR_NS::UsdShadeAttributeType::Input) {
setConnections(prim, kOldPropertyPath, kNewPropertyPath);
}
if (baseNameAndType.second == PXR_NS::UsdShadeAttributeType::Output) {
setConnections(prim.GetParent(), kOldPropertyPath, kNewPropertyPath);
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there, but the hardest part goes here. If the attribute was connected, then it must stay connected. This means finding and updating all connection sources that used the old name and changing that to the new name. This is where ngPrim will come handy as you only have to do that if the attribute is a NodeGraph boundary. You do not have to traverse the whole stage, only the nodes in the enclosing NodeGraph, if any, and the nodes in ngPrim.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should also have completed this part.

return renamedAttr;
}
#endif
#endif
} // namespace ufe
} // namespace MAYAUSD_NS_DEF
22 changes: 21 additions & 1 deletion lib/mayaUsd/ufe/UsdAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ class UsdAttributes : public Ufe::Attributes
bool hasAttribute(const std::string& name) const override;
#ifdef UFE_V4_FEATURES_AVAILABLE
#if (UFE_PREVIEW_VERSION_NUM >= 4024)
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
Ufe::AddAttributeUndoableCommand::Ptr
addAttributeCmd(const std::string& name, const Ufe::Attribute::Type& type) override;
#else
Ufe::AddAttributeCommand::Ptr
addAttributeCmd(const std::string& name, const Ufe::Attribute::Type& type) override;
addAttributeCmd(const std::string& name, const Ufe::Attribute::Type& type) override;
#endif
Ufe::UndoableCommand::Ptr removeAttributeCmd(const std::string& name) override;
#endif

#if (UFE_PREVIEW_VERSION_NUM >= 4034)
Ufe::RenameAttributeUndoableCommand::Ptr
renameAttributeCmd(const std::string& originalName, const std::string& newName) override;
#endif

#if (UFE_PREVIEW_VERSION_NUM >= 4010)
inline Ufe::NodeDef::Ptr nodeDef() const;
#endif
Expand All @@ -79,6 +89,16 @@ class UsdAttributes : public Ufe::Attributes
static bool canRemoveAttribute(const UsdSceneItem::Ptr& item, const std::string& name);
static bool doRemoveAttribute(const UsdSceneItem::Ptr& item, const std::string& name);
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
static bool canRenameAttribute(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName);
static Ufe::Attribute::Ptr doRenameAttribute(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName);
#endif
#endif

private:
Expand Down
53 changes: 53 additions & 0 deletions lib/mayaUsd/ufe/UsdUndoAttributesCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ UsdAddAttributeCommand::UsdAddAttributeCommand(
const UsdSceneItem::Ptr& sceneItem,
const std::string& name,
const Ufe::Attribute::Type& type)
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
: UsdUndoableCommand<Ufe::AddAttributeUndoableCommand>()
#else
: UsdUndoableCommand<Ufe::AddAttributeCommand>()
#endif
, _sceneItemPath(sceneItem->path())
, _name(name)
, _type(type)
Expand Down Expand Up @@ -112,6 +116,55 @@ std::string UsdRemoveAttributeCommand::commandString() const
{
return std::string("RemoveAttribute ") + _name + " " + Ufe::PathString::string(_sceneItemPath);
}
#endif
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
UsdRenameAttributeCommand::UsdRenameAttributeCommand(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName)
: UsdUndoableCommand<Ufe::RenameAttributeUndoableCommand>()
, _sceneItemPath(sceneItem->path())
, _originalName(originalName)
, _newName(newName)
{
}

UsdRenameAttributeCommand::~UsdRenameAttributeCommand() { }

UsdRenameAttributeCommand::Ptr UsdRenameAttributeCommand::create(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName)
{
if (UsdAttributes::canRenameAttribute(sceneItem, originalName, newName)) {
return std::make_shared<UsdRenameAttributeCommand>(sceneItem, originalName, newName);
}

return nullptr;
}

void UsdRenameAttributeCommand::executeUndoBlock()
{
// Validation has already been done. Just rename the attribute.
auto sceneItem
= std::dynamic_pointer_cast<UsdSceneItem>(Ufe::Hierarchy::createItem(_sceneItemPath));
auto renamedAttr = UsdAttributes::doRenameAttribute(sceneItem, _originalName, _newName);

// Set the new name, since it could have been changed in order to be unique.
if (renamedAttr) {
setNewName(renamedAttr->name());
}
}

Ufe::Attribute::Ptr UsdRenameAttributeCommand::attribute() const
{
auto sceneItem
= std::dynamic_pointer_cast<UsdSceneItem>(Ufe::Hierarchy::createItem(_sceneItemPath));
return UsdAttributes(sceneItem).attribute(_newName);
}

void UsdRenameAttributeCommand::setNewName(const std::string& newName) { _newName = newName; };

#endif
#endif

Expand Down
51 changes: 49 additions & 2 deletions lib/mayaUsd/ufe/UsdUndoAttributesCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ namespace MAYAUSD_NS_DEF {
namespace ufe {

//! \brief Implementation of AddAttributeCommand
class UsdAddAttributeCommand : public UsdUndoableCommand<Ufe::AddAttributeCommand>
class UsdAddAttributeCommand
:
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
public UsdUndoableCommand<Ufe::AddAttributeUndoableCommand>
#else
public UsdUndoableCommand<Ufe::AddAttributeCommand>
#endif
{
public:
typedef std::shared_ptr<UsdAddAttributeCommand> Ptr;
Expand Down Expand Up @@ -78,7 +84,7 @@ class UsdAddAttributeCommand : public UsdUndoableCommand<Ufe::AddAttributeComman

}; // UsdAddAttributeCommand

//! \brief Implementation of AddAttributeCommand
//! \brief Implementation of RemoveAttributeCommand
class UsdRemoveAttributeCommand : public UsdUndoableCommand<Ufe::UndoableCommand>
{
public:
Expand Down Expand Up @@ -110,6 +116,47 @@ class UsdRemoveAttributeCommand : public UsdUndoableCommand<Ufe::UndoableCommand
const std::string _name;
}; // UsdRemoveAttributeCommand

#ifdef UFE_V4_FEATURES_AVAILABLE
#if (UFE_PREVIEW_VERSION_NUM >= 4034)
//! \brief Implementation of RenameAttributeCommand
class UsdRenameAttributeCommand : public UsdUndoableCommand<Ufe::RenameAttributeUndoableCommand>
{
public:
typedef std::shared_ptr<UsdRenameAttributeCommand> Ptr;

UsdRenameAttributeCommand(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName);
~UsdRenameAttributeCommand() override;

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

//! Create a UsdRenameAttributeCommand
static UsdRenameAttributeCommand::Ptr create(
const UsdSceneItem::Ptr& sceneItem,
const std::string& originalName,
const std::string& newName);

void executeUndoBlock() override;

Ufe::Attribute::Ptr attribute() const override;

private:
const Ufe::Path _sceneItemPath;
const std::string _originalName;
std::string _newName;

void setNewName(const std::string& newName);

}; // UsdRenameAttributeCommand

#endif
#endif
} // namespace ufe
} // namespace MAYAUSD_NS_DEF

Expand Down
Loading