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 self._makeOkCancelButton() 110 if defaultKnobText == "": 111 defaultKnobText = self.okButton.name() 112 self.showModal(defaultKnobText) 113 return self.__modalResult
114
115 - def _makeOkCancelButton(self):
116 while self.__openTabGroups > 0: 117 self.addKnob(nuke.EndTabGroup_Knob()) 118 self.okButton = nuke.Script_Knob( "OK" ) 119 self.addKnob( self.okButton ) 120 self.okButton.setFlag( nuke.STARTLINE ) 121 self.cancelButton = nuke.Script_Knob( "Cancel" ) 122 self.addKnob( self.cancelButton )
123
124 - def showModal( self, defaultKnobText = "" ):
125 self.addCallback() 126 self.create() 127 super(PythonPanel, self).showModal(defaultKnobText) 128 self.removeCallback()
129
130 - def addToPane( self, pane = None ):
131 self.create() 132 if pane != None: 133 pane.add( self ) 134 elif nuke.thisPane() != None: 135 nuke.thisPane().add( self ) 136 self.addCallback() 137 return self
138
139 - def accept(self):
140 if self._isModalDialog: 141 self.ok()
142
143 - def reject(self):
144 if self._isModalDialog: 145 self.cancel()
146 147
148 -class WidgetKnob():
149 - def __init__(self, widget):
150 self.widgetClass = widget
151
152 - def makeUI(self):
153 self.widget = self.widgetClass() 154 return self.widget
155
156 -def registerWidgetAsPanel ( widget, name, id, create = False ):
157 """registerWidgetAsPanel(widget, name, id, create) -> PythonPanel 158 159 Wraps and registers a widget to be used in a Nuke panel. 160 161 widget - should be a string of the class for the widget 162 name - is is the name as it will appear on the Pane menu 163 id - should the the unique ID for this widget panel 164 create - if this is set to true a new NukePanel will be returned that wraps this widget 165 166 Example ( using PyQt ) 167 168 import nuke 169 import PyQt4.QtCore as QtCore 170 import PyQt4.QtGui as QtGui 171 from nukescripts import panels 172 173 class NukeTestWindow(QtGui.QWidget): 174 def __init__(self, parent=None): 175 QtGui.QWidget.__init__(self, parent) 176 self.setLayout( QtGui.QVBoxLayout() ) 177 self.myTable = QtGui.QTableWidget( ) 178 self.myTable.header = ['Date', 'Files', 'Size', 'Path' ] 179 self.myTable.size = [ 75, 375, 85, 600 ] 180 self.layout().addWidget( self.myTable ) 181 182 nukescripts.registerWidgetAsPanel('NukeTestWindow', 'NukeTestWindow', 'uk.co.thefoundry.NukeTestWindow' ) 183 184 """ 185 186 187 class Panel( PythonPanel ): 188 189 def __init__(self, widget, name, id): 190 PythonPanel.__init__(self, name, id ) 191 self.customKnob = nuke.PyCustom_Knob( name, "", "__import__('nukescripts').panels.WidgetKnob(" + widget + ")" ) 192 self.addKnob( self.customKnob )
193 194 def addPanel(): 195 return Panel( widget, name, id ).addToPane() 196 197 menu = nuke.menu('Pane') 198 menu.addCommand( name, addPanel) 199 registerPanel( id, addPanel ) 200 201 if ( create ): 202 return Panel( widget, name, id ) 203 else: 204 return None 205