Skip to content

Instantly share code, notes, and snippets.

@jedypod
Created November 6, 2019 15:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedypod/64ad6e54a90304571062771679ae3f67 to your computer and use it in GitHub Desktop.
Save jedypod/64ad6e54a90304571062771679ae3f67 to your computer and use it in GitHub Desktop.
Nuke python script to connect each selected node's 0th input to the nearest node.
"""
# Add to menu.py
import connect_to_closest
nuke.toolbar('Nuke').addCommand('Edit/Node/Connect Multiple', 'connect_to_closest.run()', 'meta+shift+y', shortcutContext=2)
"""
import nuke
import math
def get_closest_node(node):
# Return the closest node to node
distances = {}
for n in nuke.allNodes():
if n.name() == node.name():
continue
distance = math.sqrt(
math.pow( (node.xpos() - n.xpos()), 2 ) + math.pow( (node.ypos() - n.ypos()), 2 )
)
distances[n.name()] = distance
return nuke.toNode(min(distances, key=distances.get))
def run():
for node in nuke.selectedNodes():
closest = get_closest_node(node)
print "{0} : {1}".format(node.name(), closest.name())
node.connectInput(0, closest)
if __name__=="__main__":
run()
@ilambharathi36
Copy link

how to disconnect the selected nodes ! -PYTHON

@jedypod
Copy link
Author

jedypod commented Nov 21, 2022

Hello!
There are actually some built-in functions for this.

  • If you select a bunch of nodes and press Ctrl+D that will disconnect the inputs of each node.
  • If you instead press Ctrl+Shift+X that will extract each node from the pipe it is connected to.

Also this gist got merged into my dag module, I think I made some updates and improvements since.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment