Support updating references when renaming a node or nodegraph
Issue
This was raised by @dgovil in Slack that there is no such API in existence. Element::setName() will just the name but not update references resulting in breaking connections.
Proposal
- The suggestion as to add a optional argument to
setName(name, updateReferences=false)which defaults to not updating references. This might work but onlyNodeandNodeGraphclasses support the require call to find downstream pots. - Another suggestion would be to add this on the derived classes of
NodeandNodeGraph. - Another possibility is to add in a
rename()method.
Implementation
This is what I cam up with as the logic required.
def renameNode(node, newName : str, updateReferences : bool = True):
if not node or not newName:
return
if not (node.isA(mx.Node) or node.isA(mx.NodeGraph)):
print('A non-node or non-nodegraph was passed to renameNode()')
return
if node.getName() == newName:
return
parent = node.getParent()
if not parent:
return
newName = parent.createValidChildName(newName)
if updateReferences:
downStreamPorts = node.getDownstreamPorts()
if downStreamPorts:
for port in downStreamPorts:
#if (port.getNodeName() == node.getName()): This is assumed from getDownstreamPorts()
oldName = port.getNodeName()
if (port.getAttribute('nodename')):
port.setNodeName(newName)
print(' > Update downstream port: "' + port.getNamePath() + '" from:"' + oldName + '" to "' + port.getAttribute('nodename') + '"')
elif (port.getAttribute('nodegraph')):
port.setAttribute('nodegraph', newName)
print(' > Update downstream port: "' + port.getNamePath() + '" from:"' + oldName + '" to "' + port.getAttribute('nodegraph') + '"')
elif (port.getAttribute('interfacename')):
port.setAttribute('interfacename', newName)
print(' > Update downstream port: "' + port.getNamePath() + '" from:"' + oldName + '" to "' + port.getAttribute('interfacename') + '"')
node.setName(newName)
It might be "nice" to add in a renameConnection() which would check the usage of interfacename or nodename or nodegraph. Long term it would better to just use 1 connection string identifier.
Thanks, @kwokcb!
Hello! Better late than never (hopefully!). (I've signed up and finished the CLA but missed the step to have a task assigned to myself) This is my first year as a contributor to ASWF Dev Days and I think this task is a good fit for me! Is it too late to have this task assigned to me?
Not too late at all - glad to have you onboard for dev-days :)
Thanks to @rasmustilljander for addressing this in #2386, as well as to @kwokcb and @dgovil for the original request!