Create a ticket

Q100372: Renaming input and output ports with Python


When setting up nodes with multiple inputs (such as Merge, Switch or Global State Variables nodes), it can be quite tedious to manually rename each connection port to clarify the connection purpose. To automate the process you can create a custom Shelf Item or SuperTool with Python that handled the renaming. This article explains how this can be achieved.




An example script which loops through each selected node’s input ports and renames them to a specific value, is included and explained below.

This can be tested within Katana using for example a Merge or VariableSwitch node.



# Grab all currently selected nodes
selectedNodes = NodegraphAPI.GetAllSelectedNodes()

# Loop through each selected node for node in selectedNodes:
# Get all the import ports within the node     inputPorts = node.getInputPorts()
# Loop through each port within the node     for inputs in inputPorts:         index= inputs.getIndex()         name = node.getInputPortByIndex(index).getName()
# Name each port 'Shot' plus their indexed location         node.renameInputPort(name,('Shot'+repr(index+1)))



Within Katana, the script can be tested by simply creating a Merge or VariableSwitch node and adding multiple input ports to it (middle arrow on the node):

Pasting/executing the above script into the Python tab while the node is selected should rename the default input ports to something like: 


You can also test the above script as a custom Shelf Item by downloading the attached '' file and placing it in the user's home area '.katana/Shelves/' directory, under a Shelf folder name.

For example:

Windows: C:\Users\usename\.katana\Shelves\RenameShelf\
Linux: /mnt/nethome/users/usename/.katana/Shelves/RenameShelf/

This setup is the basis for a custom Shelf or SuperTool.  From here, you could additionally add a popup widget that will allow you to rename each port, or you can include the code as part of the SuperTool node itself.

For more information on setting up connections, creating Shelf Items and SuperTools, please check the Katana Online Help and Katana Developer Guide using the links below:

Python connecting nodes: Katana Developer Guide - Working with Nodes - Connecting Nodes
Shelf scripts: Katana Online Help - Technical Information - Shelf Item Scripts 
SuperTools: Katana Online Help - Technical Information - SuperTools



selectedNodes = NodegraphAPI.GetAllSelectedNodes()

Using the NodegraphAPI you first find the target nodes to apply the script logic to. This can be done in two ways, either by specifying the nodes by their name or by querying for currently selected nodes. The above line finds and stores the list of currently selected node objects as selectedNodes

for node in selectedNodes:
inputPorts = node.getInputPorts()

Once you have the list of node objects, you can iterate through each via a for loop and call the getInputPorts() function, which obtains all the input ports of the nodes.

for inputs in inputPorts:         
  index= inputs.getIndex()

Now that you have the ports of each node, you loop through the individual ports either by their name or via their indexed location. The above line finds their index location using the getIndex() function. 

name = node.getInputPortByIndex(index).getName()

The above lines retrieve the current name of the node port and rename it to Shot followed by the index of that port. The final name looks like Shot1, Shot2, Shot3 depending on how many ports a node has. 

Was this article helpful?

We're sorry to hear that!

Please tell us why.
0 out of 0 found this helpful