Jump to content

Trigger python script when releasing a node?

Recommended Posts

I'm new to python and trying to recreate a feature of Nuke's node view. It allows you to swap the positions and connections of two nodes by dropping one on top of another with [ctrl+shift] held.

I have already created a shelf tool that swaps the nodes (any feedback is appreciated, like I said I'm relatively new to python). I'm just wondering if I can trigger this script the same way nuke does, when one node is dropped onto another with a modifier key held down.

nodes = hou.selectedNodes()

inputInfo = []
inputBuffer = []
outputInfo = []
outputBuffer = []

# check if two or more nodes are selected

if len(nodes)>=2:
    for idx, node in enumerate(nodes):
        # if more than two nodes are selected break loop after two iterations
        if idx>=2:
        # create array of input connections and output connections for each of the two nodes

    # the connection array will update each time the nodes are altered so I've saved the connection info to a buffer (node to change, input index, node to connect to, output index, self)
    for idx, inputs in enumerate(inputInfo):
        for inputs in inputs:
            for inputs in inputs:
    for idx, outputs in enumerate(outputInfo):
        for outputs in outputs:
            for outputs in outputs:
    # delete all effected connections
    for inputs in inputInfo:
        for inputs in inputs:
            for inputs in inputs:
    for outputs in outputInfo:
        for outputs in outputs:
            for outputs in outputs:
    # rebuild connections taking into account if the input/output index exists on the target node, whether it is a subnet and whether the selected nodes are connected to each other
    for inputs in inputBuffer:
        if inputs[1]<len(inputs[0].inputConnectors()):
            if inputs[0].name()==inputs[2].name():
                if inputs[4].isSubNetwork()==1:
                    length = len(inputs[4].subnetOutputs())
                    length = len(inputs[4].outputConnectors())
                if inputs[3]<length:
    for outputs in outputBuffer:
        if outputs[0].name()!=outputs[2].name():
            if outputs[2].isSubNetwork()==1:
                length = len(outputs[2].subnetOutputs())
                length = len(outputs[2].outputConnectors())
            if outputs[3]<length:
    # swap positions of nodes
    pos0 = nodes[0].position()
    pos1 = nodes[1].position()


Edited by lionlion44
Added comments to script

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now