pythonrgbanuke

Colour Difference in The Foundry NUKE


So I am trying to make a painterly node group with Python code straight so I can use it for future projects but I can't seem to get the power part of the formula in nuke to work from this colour difference formula( I'm also new to Nuke so if there is a better way of writing this please let me know it would be awesome thank you, or if I'm doing this wrong completely also let me know)

The following formula for color difference is used to create the difference image: |(r1,g1,b1) – (r2,g2,b2)| = ((r1 – r2)^2 + (g1 –g2)^2 + (b1 – b2)^2)^1/2.

nRedShuffle = nuke.nodes.Shuffle()
nRedShuffle['red'].setValue('red')
nRedShuffle['green'].setValue('red')
nRedShuffle['blue'].setValue('red')
nRedShuffle['alpha'].setValue('red')

nGreenShuffle = nuke.nodes.Shuffle()
nGreenShuffle['red'].setValue('green')
nGreenShuffle['green'].setValue('green')
nGreenShuffle['blue'].setValue('green')
nGreenShuffle['alpha'].setValue('green')

#...(so on for the rest of rgba1 and rgba2)

nGreenShuffle2 = nuke.nodes.Shuffle()
nGreenShuffle2['red'].setValue('green')
nGreenShuffle2['green'].setValue('green')
nGreenShuffle2['blue'].setValue('green')
nGreenShuffle2['alpha'].setValue('green')

nBlueShuffle2 = nuke.nodes.Shuffle()
nBlueShuffle2['red'].setValue('blue')
nBlueShuffle2['green'].setValue('blue')
nBlueShuffle2['blue'].setValue('blue')
nBlueShuffle2['alpha'].setValue('blue')

#I am having troubles with the powers below

redDiff = nuke.nodes.Merge2(operation='minus', inputs=[nRedShuffle2, nRedShuffle])
redDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[redDiff, redDiff])

greenDiff = nuke.nodes.Merge2(operation='minus', inputs=[nGreenShuffle2, nGreenShuffle])
greenDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[greenDiff, greenDiff])

blueDiff = nuke.nodes.Merge2(operation='minus', inputs=[nBlueShuffle2, nBlueShuffle])
blueDiffMuli = nuke.nodes.Merge2(operation='multiply', inputs=[blueDiff, blueDiff])

redGreenAdd = nuke.nodes.Merge2(operation='plus', inputs=[redDiffMuli, greenDiffMuli]) 
redGreenBlueAdd =  nuke.nodes.Merge2(operation='plus', inputs=[redGreenAdd, blueDiffMuli]) 

Solution

  • Here are at least two ways to implement Color Difference formula for two images. You can use difference op in Merge node or you can write a formula in field for each channel inside MergeExpression node:

    enter image description here

    Expression for each channel is as simple as this:

    abs(Ar-Br)
    abs(Ag-Bg)
    abs(Ab-Bb)
    

    enter image description here

    Python commands

    You can use .nodes.MergeExpression methodology:

    import nuke
    
    merge = nuke.nodes.MergeExpression(expr0='abs(Ar-Br)', 
                                       expr1='abs(Ag-Bg)', 
                                       expr2='abs(Ab-Bb)')
    

    or regular .createNode syntax:

    merge = nuke.createNode('MergeExpression')
    
    merge['expr0'].setValue('abs(Ar-Br)')
    merge['expr1'].setValue('abs(Ag-Bg)')
    merge['expr2'].setValue('abs(Ab-Bb)')
    

    Full code version

    import nuke
    import nukescripts
    
    red = nuke.createNode("Constant")
    red['color'].setValue([1,0,0,1])
    
    merge = nuke.createNode('MergeExpression')
    merge['expr0'].setValue('abs(Ar-Br)')
    merge['expr1'].setValue('abs(Ag-Bg)')
    merge['expr2'].setValue('abs(Ab-Bb)')
    
    yellow = nuke.createNode("Constant")
    yellow['color'].setValue([1,1,0,1])
    
    merge.connectInput(0, yellow)
    nuke.toNode('MergeExpression1').setSelected(True)
    
    nukescripts.connect_selected_to_viewer(0)
    
    # Auto-alignment in Node Graph
    for everyNode in nuke.allNodes():
        everyNode.autoplace()
    

    Consider! MergeExpression node is much slower that regular Merge(difference) node.