Created
November 6, 2019 15:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello!
There are actually some built-in functions for this.
Also this gist got merged into my dag module, I think I made some updates and improvements since.