Trees | Indices | Help |
|
---|
|
1 # callbacks.py 2 # 3 # Callbacks from Nuke to user-defined Python. 4 # Nuke actually calls "nuke.onCreate()" but users will normally use 5 # the default versions of these functions and use "nuke.addOnCreate()" 6 # to add to the list of callbacks that the default calls. 7 8 import types 9 import nuke 10 1113 if not callable(call): 14 raise ValueError("call must be a callable") 15 if type(args) != types.TupleType: 16 args = (args,) 17 if type(kwargs) != types.DictType: 18 raise ValueError("kwargs must be a dictionary") 19 if dict.has_key(nodeClass): 20 list = dict[nodeClass] 21 # make it appear only once in list 22 try: 23 list.remove((call,args,kwargs,node)) 24 except: 25 pass 26 list.append((call,args,kwargs,node)) 27 else: 28 dict[nodeClass] = [(call,args,kwargs,node)]2931 if type(args) != types.TupleType: 32 args = (args,) 33 if dict.has_key(nodeClass): 34 list = dict[nodeClass] 35 try: 36 list.remove((call,args,kwargs,node)) 37 except: 38 pass3941 list = dict.get(nuke.thisClass()) 42 node = nuke.thisNode() 43 if list: 44 for f in list: 45 if f[3] == None or f[3] is node: 46 f[0](*f[1],**f[2]) 47 list = dict.get('*') 48 if list: 49 for f in list: 50 if f[3] == None or f[3] is node: 51 f[0](*f[1],**f[2])52 53 onUserCreates={}55 """Add code to execute when user creates a node""" 56 _addCallback(onUserCreates, call, args, kwargs, nodeClass)58 """Remove a previously-added callback with the same arguments.""" 59 _removeCallback(onUserCreates, call, args, kwargs, nodeClass)63 64 onCreates={}66 """Add code to execute when a node is created or undeleted""" 67 _addCallback(onCreates, call, args, kwargs, nodeClass)69 """Remove a previously-added callback with the same arguments.""" 70 _removeCallback(onCreates, call, args, kwargs, nodeClass)73 74 onScriptLoads={}76 """Add code to execute when a script is loaded""" 77 _addCallback(onScriptLoads, call, args, kwargs, nodeClass)79 """Remove a previously-added callback with the same arguments.""" 80 _removeCallback(onScriptLoads, call, args, kwargs, nodeClass)83 84 onScriptSaves={}86 """Add code to execute before a script is saved""" 87 _addCallback(onScriptSaves, call, args, kwargs, nodeClass)89 """Remove a previously-added callback with the same arguments.""" 90 _removeCallback(onScriptSaves, call, args, kwargs, nodeClass)93 94 onScriptCloses={}96 """Add code to execute before a script is closed""" 97 _addCallback(onScriptCloses, call, args, kwargs, nodeClass)99 """Remove a previously-added callback with the same arguments.""" 100 _removeCallback(onScriptCloses, call, args, kwargs, nodeClass)103 104 onDestroys={}106 """Add code to execute when a node is destroyed""" 107 _addCallback(onDestroys, call, args, kwargs, nodeClass)109 """Remove a previously-added callback with the same arguments.""" 110 _removeCallback(onDestroys, call, args, kwargs, nodeClass)113 114 knobChangeds={}116 """Add code to execute when the user changes a knob 117 The knob is availble in nuke.thisKnob() and the node in nuke.thisNode(). 118 This is also called with dummy knobs when the control panel is opened 119 or when the inputs to the node changes. The purpose is to update other 120 knobs in the control panel. Use addUpdateUI() for changes that 121 should happen even when the panel is closed.""" 122 _addCallback(knobChangeds, call, args, kwargs, nodeClass, node)124 """Remove a previously-added callback with the same arguments.""" 125 _removeCallback(knobChangeds, call, args, kwargs, nodeClass, node)128 129 updateUIs={}131 """Add code to execute on every node when things change. This is done 132 during idle, you cannot rely on it being done before it starts updating 133 the viewer""" 134 _addCallback(updateUIs, call, args, kwargs, nodeClass)136 """Remove a previously-added callback with the same arguments.""" 137 _removeCallback(updateUIs, call, args, kwargs, nodeClass)140 141 # autolabel is somewhat different due to it returning a string 142 autolabels={}144 """Add code to execute on every node to produce the text to draw on it 145 in the DAG. Any value other than None is converted to a string and used 146 as the text. None indicates that previously-added functions should 147 be tried""" 148 _addCallback(autolabels, call, args, kwargs, nodeClass)150 """Remove a previously-added callback with the same arguments.""" 151 _removeCallback(autolabels, call, args, kwargs, nodeClass)153 list = autolabels.get(nuke.thisClass()) 154 if list: 155 for f in list[::-1]: 156 s = f[0](*f[1],**f[2]) 157 if s != None: return s 158 list = autolabels.get('*') 159 if list: 160 for f in list[::-1]: 161 s = f[0](*f[1],**f[2]) 162 if s != None: return s163 164 # Normal rendering callbacks 165 beforeRenders={}167 """Add code to execute before starting any renders""" 168 _addCallback(beforeRenders, call, args, kwargs, nodeClass)170 """Remove a previously-added callback with the same arguments.""" 171 _removeCallback(beforeRenders, call, args, kwargs, nodeClass)174 175 beforeFrameRenders={}177 """Add code to execute before each frame of a render""" 178 _addCallback(beforeFrameRenders, call, args, kwargs, nodeClass)180 """Remove a previously-added callback with the same arguments.""" 181 _removeCallback(beforeFrameRenders, call, args, kwargs, nodeClass)184 185 afterFrameRenders={}187 """Add code to execute after each frame of a render""" 188 _addCallback(afterFrameRenders, call, args, kwargs, nodeClass)190 """Remove a previously-added callback with the same arguments.""" 191 _removeCallback(afterFrameRenders, call, args, kwargs, nodeClass)194 195 afterRenders={}197 """Add code to execute after any renders""" 198 _addCallback(afterRenders, call, args, kwargs, nodeClass)200 """Remove a previously-added callback with the same arguments.""" 201 _removeCallback(afterRenders, call, args, kwargs, nodeClass)204 205 renderProgresses={}207 """Add code to execute when the progress bar updates during any renders""" 208 _addCallback(renderProgresses, call, args, kwargs, nodeClass)210 """Remove a previously-added callback with the same arguments.""" 211 _removeCallback(renderProgresses, call, args, kwargs, nodeClass)214 215 # Callbacks for internal use only 216 _beforeRecordings={}218 """Add code to execute before viewer recording""" 219 _addCallback(_beforeRecordings, call, args, kwargs, nodeClass)221 """Remove a previously-added callback with the same arguments.""" 222 _removeCallback(_beforeRecordings, call, args, kwargs, nodeClass)225 226 _afterRecordings={}228 """Add code to execute after viewer recording""" 229 _addCallback(_afterRecordings, call, args, kwargs, nodeClass)231 """Remove a previously-added callback with the same arguments.""" 232 _removeCallback(_afterRecordings, call, args, kwargs, nodeClass)235 236 _beforeReplays={}238 """Add code to execute before viewer replay""" 239 _addCallback(_beforeReplays, call, args, kwargs, nodeClass)241 """Remove a previously-added callback with the same arguments.""" 242 _removeCallback(_beforeReplays, call, args, kwargs, nodeClass)245 246 _afterReplays={}248 """Add code to execute after viewer replay""" 249 _addCallback(_afterReplays, call, args, kwargs, nodeClass)251 """Remove a previously-added callback with the same arguments.""" 252 _removeCallback(_afterReplays, call, args, kwargs, nodeClass)255 256 # Special functions to perform background callbacks as these have no node as 257 # context.259 if not callable(call): 260 raise ValueError("call must be a callable") 261 if type(args) != types.TupleType: 262 args = (args,) 263 if type(kwargs) != types.DictType: 264 raise ValueError("kwargs must be a dictionary") 265 # make it appear only once in list 266 try: 267 list.remove((call,args,kwargs)) 268 except: 269 pass 270 list.append((call,args,kwargs))271273 if type(args) != types.TupleType: 274 args = (args,) 275 try: 276 list.remove((call,args,kwargs)) 277 except: 278 pass279 283 284 # Background rendering callbacks 285 beforeBackgroundRenders=[]287 """Add code to execute before starting any background renders. 288 The call must be in the form of: 289 def foo(context): 290 pass 291 292 The context object that will be passed in is a dictionary containing the following elements: 293 id => The identifier for the task that's about to begin 294 295 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 296 """ 297 _addBackgroundCallback(beforeBackgroundRenders, call, args, kwargs)299 """Remove a previously-added callback with the same arguments.""" 300 _removeBackgroundCallback(beforeBackgroundRenders, call, args, kwargs)303 304 # There is no logical place for this to be called at the moment, so don't expose it. 305 #def addBeforeBackgroundFrameRender(call, args=(), kwargs={}): 306 # """Add code to execute before each frame of a background render""" 307 # _addBackgroundCallback(beforeBackgroundFrameRenders, call, args, kwargs) 308 #def removeBeforeBackgroundFrameRender(call, args=(), kwargs={}): 309 # """Remove a previously-added callback with the same arguments.""" 310 # _removeBackgroundCallback(beforeBackgroundFrameRenders, call, args, kwargs) 311 #def beforeBackgroundFrameRender(): 312 # _doBackgroundCallbacks(beforeBackgroundFrameRenders) 313 314 afterBackgroundFrameRenders=[]316 """Add code to execute after each frame of a background render. 317 The call must be in the form of: 318 def foo(context): 319 pass 320 321 The context object that will be passed in is a dictionary containing the following elements: 322 id => The identifier for the task that's making progress 323 frame => the current frame number being rendered 324 numFrames => the total number of frames that is being rendered 325 frameProgress => the number of frames rendered so far. 326 327 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 328 """ 329 _addBackgroundCallback(afterBackgroundFrameRenders, call, args, kwargs)331 """Remove a previously-added callback with the same arguments.""" 332 _removeBackgroundCallback(afterBackgroundFrameRenders, call, args, kwargs)335 336 afterBackgroundRenders=[]338 """Add code to execute after any background renders. 339 The call must be in the form of: 340 def foo(context): 341 pass 342 343 The context object that will be passed in is a dictionary containing the following elements: 344 id => The identifier for the task that's ended 345 346 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 347 """ 348 _addBackgroundCallback(afterBackgroundRenders, call, args, kwargs)350 """Remove a previously-added callback with the same arguments.""" 351 _removeBackgroundCallback(afterBackgroundRenders, call, args, kwargs)354 355 # filenameFilter is somewhat different due to it returning a string 356 filenameFilters={}358 """Add a function to modify filenames before Nuke passes them to 359 the operating system. The first argument to the function is the 360 filename, and it should return the new filename. None is the same as 361 returning the string unchanged. All added functions are called 362 in backwards order.""" 363 _addCallback(filenameFilters, call, args, kwargs, nodeClass)365 """Remove a previously-added callback with the same arguments.""" 366 _removeCallback(filenameFilters, call, args, kwargs, nodeClass)367369 global filenameFilters 370 if filenameFilters: 371 # Run the filename through registered callbacks, starting with class-specific 372 # ones. There are issues with calling thisClass() here so only do it if a 373 # class-specific callback has been registered 374 allNodesFilter = filenameFilters.get('*', []) 375 if len(filenameFilters) > 1 or not allNodesFilter: 376 classFilter = filenameFilters.get(nuke.thisClass(), []) 377 for f in classFilter[::-1]: 378 s = f[0](filename,*f[1],**f[2]) 379 if s != None: filename = s 380 for f in allNodesFilter[::-1]: 381 s = f[0](filename,*f[1],**f[2]) 382 if s != None: filename = s 383 else: 384 # For back-compatibility allow user to define a filenameFix() function: 385 import __main__ 386 if __main__.__dict__.has_key('filenameFix'): 387 return __main__.__dict__['filenameFix'](filename) 388 # For even further back-compatibility let them define a tcl filename_fix function: 389 return nuke.tcl("filename_fix",filename) 390 return filename391 392 validateFilenames={}394 """Add a function to validate a filename in Write nodes. The first argument 395 is the filename and it should return a Boolean as to whether the filename is valid 396 or not. If a callback is provided, it will control whether the Render button of Write nodes 397 and the Execute button of WriteGeo nodes is enabled or not.""" 398 _addCallback(validateFilenames, call, args, kwargs, nodeClass)400 """Remove a previously-added callback.""" 401 _removeCallback(validateFilenames, call, args, kwargs, nodeClass)403 import __main__ 404 list = validateFilenames.get(nuke.thisClass()) 405 valid = True 406 407 if list: 408 for f in list: 409 b = f[0](filename) 410 if b == False: valid = False 411 list = validateFilenames.get('*') 412 if list: 413 for f in list: 414 b = f[0](filename) 415 if b == False: valid = False 416 return valid417 418420 import __main__ 421 list = filters.get( 'Root' ) 422 if list: 423 for f in list: 424 s = f[0](filename) 425 filename = s 426 427 return filename428 429 autoSaveFilters={}431 """addAutoSaveFilter(filter) -> None 432 433 Add a function to modify the autosave filename before Nuke saves the current script on an autosave timeout. 434 435 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 436 437 @param filter: A filter function. The first argument to the filter is the current autosave filename. 438 The filter should return the filename to save the autosave to.""" 439 _addCallback(autoSaveFilters, filter, (), {}, 'Root')440442 """Remove a previously-added callback with the same arguments.""" 443 _removeCallback(autoSaveFilters, call, (), {}, 'Root')444446 """Internal function. Use addAutoSaveFilter to add a callback""" 447 return _doAutoSaveCallbacks( autoSaveFilters, filename )448 449 450 autoSaveRestoreFilters={}452 """addAutoSaveRestoreFilter(filter) -> None 453 454 Add a function to modify the autosave restore file before Nuke attempts to restores the autosave file. 455 456 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 457 458 @param filter: A filter function. The first argument to the filter is the current autosave filename. 459 This function should return the filename to load autosave from or it should return None if the autosave file should be ignored.""" 460 _addCallback(autoSaveRestoreFilters, filter, (), {}, 'Root')461463 """Remove a previously-added callback with the same arguments.""" 464 _removeCallback(autoSaveRestoreFilters, filter, (), {}, 'Root')465467 """Internal function. Use addAutoSaveRestoreFilter to add a callback""" 468 return _doAutoSaveCallbacks( autoSaveRestoreFilters, filename )469 470 autoSaveDeleteFilters={}472 """addAutoSaveDeleteFilter(filter) -> None 473 474 Add a function to modify the autosave filename before Nuke attempts delete the autosave file. 475 476 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 477 478 @param filter: A filter function. The first argument to the filter is the current autosave filename. 479 This function should return the filename to delete or return None if no file should be deleted.""" 480 _addCallback(autoSaveDeleteFilters, filter, (), {}, 'Root')481483 """Remove a previously-added callback with the same arguments.""" 484 _removeCallback(autoSaveDeleteFilters, filter, (), {}, 'Root')485487 """Internal function. Use addAutoSaveDeleteFilter to add a callback""" 488 return _doAutoSaveCallbacks( autoSaveDeleteFilters, filename )489
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Wed Sep 25 20:04:12 2019 | http://epydoc.sourceforge.net |