MaterialX icon indicating copy to clipboard operation
MaterialX copied to clipboard

Support updating references when renaming a node or nodegraph

Open kwokcb opened this issue 1 year ago • 1 comments

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 only Node and NodeGraph classes support the require call to find downstream pots.
  • Another suggestion would be to add this on the derived classes of Node and NodeGraph.
  • 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.

kwokcb avatar May 30 '24 21:05 kwokcb

Thanks, @kwokcb!

dgovil avatar May 31 '24 03:05 dgovil

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?

rasmustilljander avatar May 14 '25 07:05 rasmustilljander

Not too late at all - glad to have you onboard for dev-days :)

ld-kerley avatar May 14 '25 16:05 ld-kerley

Thanks to @rasmustilljander for addressing this in #2386, as well as to @kwokcb and @dgovil for the original request!

jstone-lucasfilm avatar Jun 08 '25 20:06 jstone-lucasfilm