CCCPaste

Nuke - Write Node Panel (Nuke 14)

#   Write Node Switch Panel v11.0
#   Written by EToth / JCollier
#   Updated 10/20/2016

import sys
import nuke # JC: added this so it doesn't just fail to init
from functools import partial
from PySide2.QtGui import *
from PySide2.QtCore import *

#Function to clean up the filepath string,
#should return just the file and extension
def filePath(FilePath):
    FilePath=FilePath.split("/")
    FilePath=FilePath[-1]
    FilePath=FilePath.replace(".%04d","")
    return FilePath

#Function to disable all Write Nodes
def disableAll():
    for node in nuke.allNodes("Write"):
        node['disable'].setValue(True)
    for node in nuke.allNodes("WriteTank"):
        node['disable'].setValue(True)
    for node in nuke.allNodes("WriteGeo"):
        node['disable'].setValue(True)

#Function to enable all Write Nodes
def enableAll():
    for node in nuke.allNodes("Write"):
        node['disable'].setValue(False)
    for node in nuke.allNodes("WriteTank"):
        node['disable'].setValue(False)
    for node in nuke.allNodes("WriteGeo"):
        node['disable'].setValue(False)

#Function to restore Write nodes to what they were when the panel was first opened.
def currentStatus(nodeList,self):
    #print 'nodeList: {}'.format(str(nodeList))
    #checkboxes = []
    for node in range(0,len(nodeList)):
        nuke.toNode(nodeList[node][0])['disable'].setValue(nodeList[node][2])
        temp = nodeList[node][0]
        #print temp, nodeList[node][2]
        if nodeList[node][2] == True:
            exec("self.checkbox_"+temp+".setChecked(False)")
            #print nodeList[node][0], "TRUE"
        else:
            exec("self.checkbox_"+temp+".setChecked(True)")
            #print nodeList[node][0], "FALSE"

#Function to select all write nodes (Shotgun nodes included)
def selectAll():
    for node in nuke.allNodes("Write"):
        node['selected'].setValue(True)
    for node in nuke.allNodes("WriteTank"):
        node['selected'].setValue(True)
    for node in nuke.allNodes("WriteGeo"):
        node['selected'].setValue(True)

#Function to select all Shotgun Write nodes
def selectSG():
    for node in nuke.allNodes():
        node['selected'].setValue(False)
    for node in nuke.allNodes("WriteTank"):
        node['selected'].setValue(True)

#Function to select all non-SG Write nodes
def selectWrite():
    for node in nuke.allNodes():
        node['selected'].setValue(False)
    for node in nuke.allNodes("Write"):
        node['selected'].setValue(True)

#Function to select nothing
def selectNone():
    for node in nuke.allNodes():
        node['selected'].setValue(False)

#Function to return a list of all Write, WriteGeo, SGTK Write Nodes
#as a list of nodes wih their attributes inside
def nodeList():
    NodeList=[]
    #find all Write Nodes and append to NodeList
    for node in nuke.allNodes('Write'):
        nodeInfo=[]
        nodeInfo.append(node['name'].getValue())
        nodeInfo.append(filePath(node['file'].getValue()))
        nodeInfo.append(node['disable'].value())
        nodeInfo.append(node.Class())
        NodeList.append(nodeInfo)
    #find all WriteGeo Nodes and append to NodeList
    for node in nuke.allNodes('WriteGeo'):
        nodeInfo=[]
        nodeInfo.append(node['name'].getValue())
        nodeInfo.append(filePath(node['file'].getValue()))
        nodeInfo.append(node['disable'].value())
        nodeInfo.append(node.Class())
        NodeList.append(nodeInfo)
    #find all WriteGeo Nodes and append to NodeList
    for node in nuke.allNodes('WriteTank'):
        nodeInfo=[]
        nodeInfo.append(node['name'].getValue())
        nodeInfo.append(node['path_filename'].value().replace(".%04d",""))
        nodeInfo.append(node['disable'].value())
        nodeInfo.append(node.Class())
        NodeList.append(nodeInfo)
    NodeList.sort(key=lambda x: x[0])
    #print NodeList
    return NodeList

#Function to open help
def selfHelp():
    print("Opening writeNodeSwitch help...")
    # the Qt for this would be:
    # helpURL = "https://mammal.atlassian.net/wiki/display/MC/Write+Nodes+Switch+Panel"
    # QtGui.QDesktopServices.openUrl(QtCore.QUrl(helpURL))
    nuke.tcl("start https://mammal.atlassian.net/wiki/display/MC/Write+Nodes+Switch+Panel")

class Panel(QWidget):
    def __init__(self):
        #Check allWidgets to see if "Panel" already exists, if it does, SHUT IT DOWN!
        for entry in QApplication.allWidgets():
            if type(entry).__name__ == 'Panel':
                entry.close()
                
        super(Panel, self).__init__()

        #name the panel
        self.setWindowTitle("Write Node Switch Panel")

        #maximize
        #self.showMaximized()
        self.setGeometry(375,100,1,1)

        #get the nodeList
        self.NodeList = nodeList()
        layoutMaster = QHBoxLayout()

        layoutGroupWriteNodes = QVBoxLayout()
        groupWriteNodes = QGroupBox("Nuke Write Nodes")
        groupWriteNodes.setStyleSheet("QGroupBox {font-size: 12pt; font-weight:Bold}")
        layoutSGTKWriteNodes = QVBoxLayout()
        groupSGTKNodes = QGroupBox("Shotgun Write Nodes")
        groupSGTKNodes.setStyleSheet("QGroupBox {font-size: 12pt; font-weight:Bold}")
        layoutWriteGeoNodes = QVBoxLayout()
        groupWriteGeoNodes = QGroupBox("Write Geo Nodes")
        groupWriteGeoNodes.setStyleSheet("QGroupBox {font-size: 12pt; font-weight:Bold}")

        for i in range(0,len(self.NodeList)):
            nodeName='self.checkbox_'+str(self.NodeList[i][0])
            exec(nodeName+"=QCheckBox(self.NodeList[i][0])")
            exec(nodeName+".setText(self.NodeList[i][0]+str(' : ')+self.NodeList[i][1])")
            exec(nodeName+".setChecked(1-self.NodeList[i][2])")  # set the checkbox True/False
            exec(nodeName+".setToolTip(self.NodeList[i][0]+str(' : ')+self.NodeList[i][1])")
            exec(nodeName+".clicked.connect(self.who_clicked)")
            nodeName=str("self.checkbox_"+self.NodeList[i][0])
            if self.NodeList[i][3] == "Write":
                layoutGroupWriteNodes.addWidget(eval(nodeName))
            if self.NodeList[i][3] == "WriteTank":
                layoutSGTKWriteNodes.addWidget(eval(nodeName))
            if self.NodeList[i][3] == "WriteGeo":
                layoutWriteGeoNodes.addWidget(eval(nodeName))

        #addStretch to push buttons together
        #(otherwise they stretch to fit the vertical limit)
        layoutGroupWriteNodes.addStretch()
        layoutSGTKWriteNodes.addStretch()
        layoutWriteGeoNodes.addStretch()

        groupWriteNodes.setLayout(layoutGroupWriteNodes)
        groupSGTKNodes.setLayout(layoutSGTKWriteNodes)
        groupWriteGeoNodes.setLayout(layoutWriteGeoNodes)

        layoutMaster.addWidget(groupWriteNodes)
        layoutMaster.addWidget(groupSGTKNodes)
        layoutMaster.addWidget(groupWriteGeoNodes)

        layoutButtons = QVBoxLayout()
        groupButtons = QGroupBox("Buttons")
        #Enable All Write Nodes
        enableAllButton = QPushButton("Enable All")
        enableAllButton.clicked.connect(enableAll)
        enableAllButton.clicked.connect(self.enableDisable)
        enableAllButton.setStyleSheet("background-color: DarkGreen; font-weight: bold;  font-size: 14pt")
        enableAllButton.setMinimumHeight(60)
        #Disable All Write Nodes
        disableAllButton = QPushButton("Disable All")
        disableAllButton.clicked.connect(disableAll)
        disableAllButton.clicked.connect(self.enableDisable)
        disableAllButton.setStyleSheet("background-color:  DarkRed; font-weight: bold;  font-size: 14pt")
        disableAllButton.setMinimumHeight(60)
        disableAllButton.setMinimumWidth(200)
        #Restore Write Nodes To Current Status
        currentStatusButton = QPushButton("Current Status")
        currentStatusButton.clicked.connect(partial(currentStatus, self.NodeList, self))
        currentStatusButton.setStyleSheet("background-color: DarkGoldenRod; font-weight: bold;  font-size: 14pt")
        currentStatusButton.setMinimumHeight(60)
        #Select All Write Nodes (Shotgun nodes included)
        selectAllButton = QPushButton("Select All")
        selectAllButton.clicked.connect(selectAll)
        selectAllButton.setMinimumHeight(40)
        #Select All Shotgun Write Nodes
        selectSGButton = QPushButton("Select Shotgun\nNodes")
        selectSGButton.clicked.connect(selectSG)
        selectSGButton.setMinimumHeight(40)
        #Select Nuke Write Nodes
        selectWriteButton = QPushButton("Select Nuke\nNodes")
        selectWriteButton.clicked.connect(selectWrite)
        selectWriteButton.setMinimumHeight(40)
        #Select Nothing
        selectNoneButton = QPushButton("Select None")
        selectNoneButton.clicked.connect(selectNone)
        selectNoneButton.setMinimumHeight(40)
        #Horizontal Lines
        line1 = QFrame()
        line1.setFrameShape(QFrame.HLine)
        line1.setFrameShadow(QFrame.Sunken)
        line2 = QFrame()
        line2.setFrameShape(QFrame.HLine)
        line2.setFrameShadow(QFrame.Sunken)
        line3 = QFrame()
        line3.setFrameShape(QFrame.HLine)
        line3.setFrameShadow(QFrame.Sunken)

        #Close the panel
        closeButton = QPushButton("Close")
        closeButton.clicked.connect(self.closePanel)
        closeButton.setMinimumHeight(60)
        closeButton.setStyleSheet("font-weight: bold;  font-size: 14pt")

        #Help Button
        selfHelpButton = QPushButton("Help")
        selfHelpButton.clicked.connect(selfHelp)
        selfHelpButton.setMinimumHeight(40)

        #Layout the buttons across the UI
        layoutButtons.addWidget(enableAllButton)
        layoutButtons.addWidget(disableAllButton)
        layoutButtons.addWidget(currentStatusButton)
        layoutButtons.addWidget(line1)
        layoutButtons.addWidget(selectAllButton)
        layoutButtons.addWidget(selectSGButton)
        layoutButtons.addWidget(selectWriteButton)
        layoutButtons.addWidget(selectNoneButton)
        layoutButtons.addStretch()
        layoutButtons.addWidget(line2)
        layoutButtons.addWidget(selfHelpButton)
        layoutButtons.addWidget(line3)
        layoutButtons.addStretch()
        layoutButtons.addWidget(closeButton)
        groupButtons.setLayout(layoutButtons)


        layoutMaster.addWidget(groupButtons)

        self.setLayout(layoutMaster)

    #Function to close the panel
    # JC: I moved this into your widget class
    # because it was incorrectly scoped
    # I also fixed the corresponding connector for the
    # close button to call "self.closePanel"
    def closePanel(self):
        self.close()

    #Function to close if ESC key is pressed
    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

    #Function to check enable/disable in the UI Panel
    def who_clicked(self):
        sender = self.sender()
        senderText = self.sender().text()
        buttons=[]
        for button in range(0,len(self.NodeList)):
            buttons.append(self.NodeList[button][0]+' : '+self.NodeList[button][1])
        x=0
        for i in buttons:
            if i == senderText:
                onOff=nuke.toNode(self.NodeList[x][0])
                if onOff['disable'].value() == True:
                    onOff['disable'].setValue(False)
                    print(self.NodeList[x][0:2], "ENABLED")
                else:
                    onOff['disable'].setValue(True)
                    print(self.NodeList[x][0:2], "DISABLED")
            x+=1

    #Function to check all the checkboxes on/off depending on
    #user input of ENABLE/DISABLE ALL
    def enableDisable(self):
        sender=self.sender()
        senderText=self.sender().text()
        #print senderText
        checkboxes = []
        for checkbox in range(0,len(self.NodeList)):
            checkboxes.append(self.NodeList[checkbox][0])
        if senderText == "Enable All":
            for temp in checkboxes:
                #print temp
                exec("self.checkbox_"+temp+".setChecked(True)")
        elif senderText == "Disable All":
            for temp in checkboxes:
                exec("self.checkbox_"+temp+".setChecked(False)")

'''
def main(): # JC: we call things as .main() in menu.py
    # init the Panel instance
    # then show the panel
    panel = Panel()
    panel.show()
 
if __name__ == "__main__":
    main()
'''
# ET: In the absence of menu.py; Let's just run this in the script editor for now...
panel = Panel()
panel.show()