# GUIFarm by Andreas Gebauer

# Lets you disable nodes in the gui and globally turn them on and off for previewing without using expression links. Tested in Nuke 9 and 10.

# 1. Select node and press SHIFT + D.
# 2. Set values in the 'gui' and 'farm' fields. By default node is True in gui and False on farm.
# 3. Globally turn them on and off in the top GUIFarm menu with "Toggle preview"

# GUIFarm Menu:
# Disable all: Removes expressions from all GUIFarm affected nodes and sets disable knob to False.
# Enable all: Enables all GUIFarm affected nodes and sets $gui expression.

# You can use this on other boolean knobs as well but I haven' really tested it.




import nuke


guiFarmPreview = False



def build():



	disableThis = 'nuke.thisNode()[nuke.thisNode()["knobName2"].value()].clearAnimated()\nnuke.thisNode()["GUIFarmEnabled"].setValue(False)\nnuke.thisNode()[nuke.thisNode()["knobName2"].value()].setValue(False)'

	enableThis = 'nuke.thisNode()[nuke.thisNode()["knobName2"].value()].setExpression("$gui?gui:farm")\nnuke.thisNode()["GUIFarmEnabled"].setValue(True)'


	
	global guiFarmPreview
	
	if nuke.thisNode().knob('GUIFarm'):

		nuke.message('GUI/Farm already exists')

	else:

		knobName = nuke.thisKnob().name()
		
		nuke.selectedNode().addKnob(nuke.Tab_Knob('GUIFarm', 'GUI/Farm'))
		nuke.selectedNode().addKnob(nuke.String_Knob('knobName2', 'Knob name'))
		nuke.selectedNode().addKnob(nuke.Text_Knob('guiInfo', 'Disable on:'))
		nuke.selectedNode().addKnob(nuke.Boolean_Knob('gui',))
		nuke.selectedNode().addKnob(nuke.Boolean_Knob('farm'))
		nuke.selectedNode().addKnob(nuke.PyScript_Knob('enableThis', 'Enable'))
		nuke.selectedNode().addKnob(nuke.PyScript_Knob('disableThis', 'Disable'))
		
		nuke.selectedNode().addKnob(nuke.Boolean_Knob('GUIFarmEnabled'))
		
		nuke.selectedNode().knob('knobName2').setValue(knobName)
		nuke.selectedNode().knob('knobName2').setEnabled(False)

		nuke.selectedNode().knob('enableThis').setValue(enableThis)
		nuke.selectedNode().knob('disableThis').setValue(disableThis)
		
		nuke.selectedNode().knob('GUIFarmEnabled').setValue(True)
		nuke.selectedNode().knob('GUIFarmEnabled').setVisible(False)
		nuke.selectedNode().knob('gui').setValue(True)
		
		if guiFarmPreview == True:
			nuke.selectedNode().knob(knobName).setExpression('farm')
			
			
		if guiFarmPreview == False:
			nuke.selectedNode().knob(knobName).setExpression('$gui?gui:farm')
		





def buildDisable():

    


    disableThis = 'nuke.thisNode()[nuke.thisNode()["knobName2"].value()].clearAnimated()\nnuke.thisNode()["GUIFarmEnabled"].setValue(False)\nnuke.thisNode()[nuke.thisNode()["knobName2"].value()].setValue(False)'

    enableThis = 'nuke.thisNode()[nuke.thisNode()["knobName2"].value()].setExpression("$gui?gui:farm")\nnuke.thisNode()["GUIFarmEnabled"].setValue(True)'

    if nuke.selectedNode().knob('GUIFarm'):

        nuke.message('GUI/Farm already exists')

    else:

        knobName = 'disable'
        
        nuke.selectedNode().addKnob(nuke.Tab_Knob('GUIFarm', 'GUI/Farm'))
        nuke.selectedNode().addKnob(nuke.String_Knob('knobName2', 'Knob name'))
        nuke.selectedNode().addKnob(nuke.Text_Knob('guiInfo', 'Disable on:'))
        nuke.selectedNode().addKnob(nuke.Boolean_Knob('gui',))
        nuke.selectedNode().addKnob(nuke.Boolean_Knob('farm'))
        nuke.selectedNode().addKnob(nuke.PyScript_Knob('enableThis', 'Enable'))
        nuke.selectedNode().addKnob(nuke.PyScript_Knob('disableThis', 'Disable'))

        nuke.selectedNode().addKnob(nuke.Boolean_Knob('GUIFarmEnabled'))

        nuke.selectedNode().knob('knobName2').setValue(knobName)
        nuke.selectedNode().knob('knobName2').setEnabled(False)
        nuke.selectedNode().knob(knobName).setExpression('$gui?gui:farm')
        nuke.selectedNode().knob('enableThis').setValue(enableThis)
        nuke.selectedNode().knob('disableThis').setValue(disableThis)
	nuke.selectedNode().knob('GUIFarmEnabled').setValue(True)
	nuke.selectedNode().knob('GUIFarmEnabled').setVisible(False)
	nuke.selectedNode().knob('gui').setValue(True)




def togglePreview():

     global guiFarmPreview

     guiFarmPreview = not guiFarmPreview
    
     if guiFarmPreview == True:

        for i in nuke.allNodes():
            for k in i.knobs():
                if i.knob(k).name() == "knobName2" and i.knob("GUIFarmEnabled").value() == True:
                    knobName = i.knob("knobName2").value()
                    i.knob(knobName).setExpression("farm")

     elif guiFarmPreview == False:

        for i in nuke.allNodes():
            for k in i.knobs():
                if i.knob(k).name() == "knobName2" and i.knob("GUIFarmEnabled").value() == True:
                    knobName = i.knob("knobName2").value()
                    i.knob(knobName).setExpression("$gui?gui:farm")

def enableAll():

    for i in nuke.allNodes():
        for k in i.knobs():
            if i.knob(k).name() == "knobName2":
                knobName = i.knob("knobName2").value()
                i.knob(knobName).setExpression("$gui?gui:farm")
                i.knob("GUIFarmEnabled").setValue(True)

def disableAll():

    for i in nuke.allNodes():
	for k in i.knobs():
            if i.knob(k).name() == "knobName2":
		knobName = i.knob("knobName2").value()
		i.knob(knobName).clearAnimated()
		i.knob(knobName).setValue(False)
		i.knob("GUIFarmEnabled").setValue(False)



