Skip to content

Commit

Permalink
Merge pull request #2644 from Autodesk/degiroa/LOOKDEVX-862/rename-an…
Browse files Browse the repository at this point in the history
…d-delete-ports

LOOKDEVX-862- Rename ports
  • Loading branch information
seando-adsk authored Nov 9, 2022
2 parents cfcace1 + 92e5955 commit ed25078
Show file tree
Hide file tree
Showing 5 changed files with 335 additions and 5 deletions.
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
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);
}
}

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

0 comments on commit ed25078

Please sign in to comment.