nukescripts.panels

Package nukescripts :: Module panels |
[hide private]
[frames] | no frames]

# Source Code for Module nukescripts.panels

      1  # Copyright (c) 2009 The Foundry Visionmongers Ltd.  All Rights Reserved.
      2
      3  import sys
      4  import nuke
      5  import pyui
      6
      7  __panels = dict()
      8
      9 -def registerPanel( id, command ):
     10    __panels[id] = command
     11
     12 -def unregisterPanel( id, command ):
     13    del __panels[id]
     14
     15 -def restorePanel( id ):
     16    try:
     17      return __panels[id]()
     18    except:
     19      print "Can't restore panel '", id, "' because it hasn't been registered."
     20      return None
     21
     22 -def PythonPanelKnobChanged(widget):
     23    widget.knobChangedCallback(nuke.thisKnob())
     24
     25 -class PythonPanel( pyui.Dialog ):
     26
     27 -  def __init__( self, title = "", id = "", scrollable = True ):
     28      pyui.Dialog.__init__( self, title, id, scrollable )
     29      self.__node = nuke.PanelNode()
     30      self.__widget = None
     31      self.__openTabGroups = 0
     32      self.okButton = None
     33      self.cancelButton = None
     34      self._isModalDialog = False
     35
     36 -  def addCallback( self ):
     37      nuke.addKnobChanged(PythonPanelKnobChanged, args = self, nodeClass = 'PanelNode', node = self.__node)
     38
     39 -  def removeCallback( self ):
     40      nuke.removeKnobChanged(PythonPanelKnobChanged, args = self, nodeClass = 'PanelNode', node = self.__node)
     41
     42 -  def addKnob( self, knob ):
     43      if type(knob) == nuke.BeginTabGroup_Knob:
     44        self.__openTabGroups += 1
     45      elif type(knob) == nuke.EndTabGroup_Knob:
     46        self.__openTabGroups -= 1
     47      elif type(knob) == nuke.Tab_Knob and self.__openTabGroups <= 0:
     48        self.addKnob( nuke.BeginTabGroup_Knob() )
     49
     50      self.__node.addKnob( knob )
     51      knob.setFlag( nuke.NO_UNDO | nuke.NO_ANIMATION )
     52
     53 -  def removeKnob( self, knob ):
     54      if type(knob) == nuke.BeginTabGroup_Knob:
     55        self.__openTabGroups -= 1
     56      elif type(knob) == nuke.EndTabGroup_Knob:
     57        self.__openTabGroups += 1
     58
     59      self.__node.removeKnob( knob )
     60
     61 -  def knobs( self ):
     62      return self.__node.knobs()
     63
     64 -  def writeKnobs( self, flags ):
     65      return self.__node.writeKnobs( flags )
     66
     67 -  def readKnobs( self, s ):
     68      return self.__node.readKnobs( s )
     69
     70 -  def knobChangedCallback(self, knob):
     71      self.knobChanged(knob)
     72      if knob == self.okButton:
     73        self.finishModalDialog( True )
     74      elif knob == self.cancelButton:
     75        self.finishModalDialog( False )
     76
     77 -  def knobChanged(self, knob):
     78      pass
     79
     80 -  def finishModalDialog( self, result ):
     81      self.__modalResult = result
     82      self.hide();
     83
     84 -  def ok( self ):
     85      self.finishModalDialog( True )
     86
     87 -  def cancel( self ):
     88      self.finishModalDialog( False )
     89
     90 -  def create( self ):
     91      while self.__openTabGroups > 0:
     92        self.addKnob(nuke.EndTabGroup_Knob())
     93      if self.__widget == None:
     94        self.__widget = self.__node.createWidget( self )
     95        self.add( self.__widget, 0, 0, 1, 3 )
     96
     97 -  def show( self ):
     98      self.addCallback()
     99      self.create()
    100      super(PythonPanel, self).show()
    101
    102 -  def hide( self ):
    103      super(PythonPanel, self).hide()
    104
    105 -  def showModalDialog( self, defaultKnobText = "" ):
    106      self.__modalResult = None;
    107      self._isModalDialog = True
    108      if self.okButton == None:
    109        while self.__openTabGroups > 0:
    110          self.addKnob(nuke.EndTabGroup_Knob())
    111        self.okButton = nuke.Script_Knob( "OK" )
    112        self.addKnob( self.okButton )
    113        self.okButton.setFlag( nuke.STARTLINE )
    114        self.cancelButton = nuke.Script_Knob( "Cancel" )
    115        self.addKnob( self.cancelButton )
    116      if defaultKnobText == "":
    117        defaultKnobText = self.okButton.name()
    118      self.showModal(defaultKnobText)
    119      return self.__modalResult
    120
    121 -  def showModal( self, defaultKnobText = "" ):
    122      self.addCallback()
    123      self.create()
    124      super(PythonPanel, self).showModal(defaultKnobText)
    125      self.removeCallback()
    126
    127 -  def addToPane( self, pane = None ):
    128      self.create()
    129      if pane != None:
    130        pane.add( self )
    131      elif nuke.thisPane() != None:
    132        nuke.thisPane().add( self )
    133      self.addCallback()
    134      return self
    135
    136 -  def accept(self):
    137      if self._isModalDialog:
    138        self.ok()
    139
    140 -  def reject(self):
    141      if self._isModalDialog:
    142        self.cancel()
    143
    144
    145 -class WidgetKnob():
    146 -  def __init__(self, widget):
    147      self.widgetClass = widget
    148
    149 -  def makeUI(self):
    150      self.widget = self.widgetClass()
    151      return self.widget
    152
    153 -def registerWidgetAsPanel ( widget, name, id, create = False ):
    154    """registerWidgetAsPanel(widget, name, id, create) -> PythonPanel
    155
    156      Wraps and registers a widget to be used in a Nuke panel.
    157
    158      widget - should be a string of the class for the widget
    159      name - is is the name as it will appear on the Pane menu
    160      id - should the the unique ID for this widget panel
    161      create - if this is set to true a new NukePanel will be returned that wraps this widget
    162
    163      Example ( using PyQt )
    164
    165      import nuke
    166      import PyQt4.QtCore as QtCore
    167      import PyQt4.QtGui as QtGui
    168      from nukescripts import panels
    169
    170      class NukeTestWindow(QtGui.QWidget):
    171        def __init__(self, parent=None):
    172          QtGui.QWidget.__init__(self, parent)
    173          self.setLayout( QtGui.QVBoxLayout() )
    174          self.myTable    = QtGui.QTableWidget( )
    175          self.myTable.header = ['Date', 'Files', 'Size', 'Path' ]
    176          self.myTable.size = [ 75, 375, 85, 600 ]
    177          self.layout().addWidget( self.myTable )
    178
    179      nukescripts.registerWidgetAsPanel('NukeTestWindow', 'NukeTestWindow', 'uk.co.thefoundry.NukeTestWindow' )
    180
    181    """
    182
    183
    184    class Panel( PythonPanel ):
    185
    186      def __init__(self, widget, name, id):
    187        PythonPanel.__init__(self, name, id )
    188        self.customKnob = nuke.PyCustom_Knob( "web", "", "__import__('nukescripts').panels.WidgetKnob(" + widget + ")" )
    189        self.addKnob( self.customKnob  )
    190
    191    def addPanel():
    192      return Panel( widget, name, id ).addToPane()
    193
    194    menu = nuke.menu('Pane')
    195    menu.addCommand( name, addPanel)
    196    registerPanel( id, addPanel )
    197
    198    if ( create ):
    199      return Panel( widget, name, id )
    200    else:
    201      return None
    202