-
Notifications
You must be signed in to change notification settings - Fork 202
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
Changes from 25 commits
c7a5db0
465b089
2dbce29
78ec066
3cae843
542290c
5b3df2e
0788a0b
cf792ac
ff741b9
14ea1fd
781c8e6
3702743
1f643bb
2066d6a
8bf3540
8951dae
37a108b
1cba9b8
25a2f4d
69563dd
9fc697d
dfb9353
cfac06c
226b5c5
92e5955
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 typedefThere was a problem hiding this comment.
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