# Copyright (c) 2009 The Foundry Visionmongers Ltd. All Rights Reserved.
import sys
import nuke_internal as nuke
import pyui
__panels = dict()
[docs]def registerPanel( id, command ):
__panels[id] = command
[docs]def unregisterPanel( id, command ):
del __panels[id]
[docs]def restorePanel( id ):
try:
return __panels[id]()
except:
print("Can't restore panel '", id, "' because it hasn't been registered.")
return None
[docs]def PythonPanelKnobChanged(widget):
widget.knobChangedCallback(nuke.thisKnob())
[docs]class PythonPanel( pyui.Dialog ):
def __init__( self, title = "", id = "", scrollable = True ):
pyui.Dialog.__init__( self, title, id, scrollable )
self.__node = nuke.PanelNode()
self.__widget = None
self.__openTabGroups = 0
self.okButton = None
self.cancelButton = None
self._isModalDialog = False
def addCallback( self ):
nuke.addKnobChanged(PythonPanelKnobChanged, args = self, nodeClass = 'PanelNode', node = self.__node)
def removeCallback( self ):
nuke.removeKnobChanged(PythonPanelKnobChanged, args = self, nodeClass = 'PanelNode', node = self.__node)
def addKnob( self, knob ):
if type(knob) == nuke.BeginTabGroup_Knob:
self.__openTabGroups += 1
elif type(knob) == nuke.EndTabGroup_Knob:
self.__openTabGroups -= 1
elif type(knob) == nuke.Tab_Knob and self.__openTabGroups <= 0:
self.addKnob( nuke.BeginTabGroup_Knob() )
self.__node.addKnob( knob )
knob.setFlag( nuke.NO_UNDO | nuke.NO_ANIMATION )
def removeKnob( self, knob ):
if type(knob) == nuke.BeginTabGroup_Knob:
self.__openTabGroups -= 1
elif type(knob) == nuke.EndTabGroup_Knob:
self.__openTabGroups += 1
self.__node.removeKnob( knob )
def knobs( self ):
return self.__node.knobs()
def writeKnobs( self, flags ):
return self.__node.writeKnobs( flags )
def readKnobs( self, s ):
return self.__node.readKnobs( s )
def knobChangedCallback(self, knob):
self.knobChanged(knob)
if knob == self.okButton:
self.finishModalDialog( True )
elif knob == self.cancelButton:
self.finishModalDialog( False )
def knobChanged(self, knob):
pass
def finishModalDialog( self, result ):
self.__modalResult = result
self.hide();
def ok( self ):
self.finishModalDialog( True )
def cancel( self ):
self.finishModalDialog( False )
def create( self ):
while self.__openTabGroups > 0:
self.addKnob(nuke.EndTabGroup_Knob())
if self.__widget == None:
self.__widget = self.__node.createWidget( self )
self.add( self.__widget, 0, 0, 1, 3 )
[docs] def show( self ):
self.addCallback()
self.create()
super(PythonPanel, self).show()
[docs] def hide( self ):
super(PythonPanel, self).hide()
def showModalDialog( self, defaultKnobText = "" ):
self.__modalResult = None;
self._isModalDialog = True
if self.okButton == None:
self._makeOkCancelButton()
if defaultKnobText == "":
defaultKnobText = self.okButton.name()
self.showModal(defaultKnobText)
return self.__modalResult
def _makeOkCancelButton(self):
while self.__openTabGroups > 0:
self.addKnob(nuke.EndTabGroup_Knob())
self.okButton = nuke.Script_Knob( "OK" )
self.addKnob( self.okButton )
self.okButton.setFlag( nuke.STARTLINE )
self.cancelButton = nuke.Script_Knob( "Cancel" )
self.addKnob( self.cancelButton )
[docs] def showModal( self, defaultKnobText = "" ):
self.addCallback()
self.create()
super(PythonPanel, self).showModal(defaultKnobText)
self.removeCallback()
def addToPane( self, pane = None ):
self.create()
if pane != None:
pane.add( self )
elif nuke.thisPane() != None:
nuke.thisPane().add( self )
self.addCallback()
return self
def accept(self):
if self._isModalDialog:
self.ok()
def reject(self):
if self._isModalDialog:
self.cancel()