Jump to content

Trigger python script when releasing a node?


lionlion44

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:
            break
            
        # create array of input connections and output connections for each of the two nodes
            
        inputInfo.append(node.inputConnectors())
        outputInfo.append(node.outputConnectors())

    # 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:
                inputBuffer.append([nodes[1-idx],inputs.inputIndex(),inputs.inputNode(),inputs.outputIndex(),inputs.outputNode()])
    
    for idx, outputs in enumerate(outputInfo):
        for outputs in outputs:
            for outputs in outputs:
                outputBuffer.append([outputs.outputNode(),outputs.inputIndex(),nodes[1-idx],outputs.outputIndex()])
    
    # delete all effected connections
                
    for inputs in inputInfo:
        for inputs in inputs:
            for inputs in inputs:
                inputs.outputNode().setInput(inputs.inputIndex(),None)
    
    for outputs in outputInfo:
        for outputs in outputs:
            for outputs in outputs:
                outputs.outputNode().setInput(outputs.inputIndex(),None)
    
    # 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())
                else:
                    length = len(inputs[4].outputConnectors())
                if inputs[3]<length:
                    inputs[0].setInput(inputs[1],inputs[4],inputs[3])
            else:
                inputs[0].setInput(inputs[1],inputs[2],inputs[3])
    
    for outputs in outputBuffer:
        if outputs[0].name()!=outputs[2].name():
            if outputs[2].isSubNetwork()==1:
                length = len(outputs[2].subnetOutputs())
            else:
                length = len(outputs[2].outputConnectors())
            if outputs[3]<length:
                    outputs[0].setInput(outputs[1],outputs[2],outputs[3])
    
    # swap positions of nodes
                    
    pos0 = nodes[0].position()
    pos1 = nodes[1].position()
    nodes[0].setPosition(pos1)
    nodes[1].setPosition(pos0)

 

Edited by lionlion44
Added comments to script
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...