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 # LiveGroup callbacks 257 _onLiveGroupMakeLocalCallbacks={}259 """Add code to execute after LiveGroup is made local""" 260 _addCallback(_onLiveGroupMakeLocalCallbacks, call, args, kwargs, nodeClass)262 """Remove a previously-added callback with the same arguments.""" 263 _removeCallback(_onLiveGroupMakeLocalCallbacks, call, args, kwargs, nodeClass)266 267 _onLiveGroupPrePublishCallbacks={}269 """Add code to execute before LiveGroup is published.""" 270 _addCallback(_onLiveGroupPrePublishCallbacks, call, args, kwargs, nodeClass)272 """Remove a previously-added callback with the same arguments.""" 273 _removeCallback(_onLiveGroupPrePublishCallbacks, call, args, kwargs, nodeClass)276 277 _onLiveGroupPublishCallbacks={}279 """Add code to execute after LiveGroup has been published""" 280 _addCallback(_onLiveGroupPublishCallbacks, call, args, kwargs, nodeClass)282 """Remove a previously-added callback with the same arguments.""" 283 _removeCallback(_onLiveGroupPublishCallbacks, call, args, kwargs, nodeClass)286 287 _onLiveGroupReloadCallbacks={}289 """Add code to execute after LiveGroup has been loaded or reloaded""" 290 _addCallback(_onLiveGroupReloadCallbacks, call, args, kwargs, nodeClass)292 """Remove a previously-added callback with the same arguments.""" 293 _removeCallback(_onLiveGroupReloadCallbacks, call, args, kwargs, nodeClass)296 297 # End LiveGroup callbacks 298 299 # Special functions to perform background callbacks as these have no node as 300 # context.302 if not callable(call): 303 raise ValueError("call must be a callable") 304 if type(args) != types.TupleType: 305 args = (args,) 306 if type(kwargs) != types.DictType: 307 raise ValueError("kwargs must be a dictionary") 308 # make it appear only once in list 309 try: 310 list.remove((call,args,kwargs)) 311 except: 312 pass 313 list.append((call,args,kwargs))314316 if type(args) != types.TupleType: 317 args = (args,) 318 try: 319 list.remove((call,args,kwargs)) 320 except: 321 pass322 326 327 # Background rendering callbacks 328 beforeBackgroundRenders=[]330 """Add code to execute before starting any background renders. 331 The call must be in the form of: 332 def foo(context): 333 pass 334 335 The context object that will be passed in is a dictionary containing the following elements: 336 id => The identifier for the task that's about to begin 337 338 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 339 """ 340 _addBackgroundCallback(beforeBackgroundRenders, call, args, kwargs)342 """Remove a previously-added callback with the same arguments.""" 343 _removeBackgroundCallback(beforeBackgroundRenders, call, args, kwargs)346 347 # There is no logical place for this to be called at the moment, so don't expose it. 348 #def addBeforeBackgroundFrameRender(call, args=(), kwargs={}): 349 # """Add code to execute before each frame of a background render""" 350 # _addBackgroundCallback(beforeBackgroundFrameRenders, call, args, kwargs) 351 #def removeBeforeBackgroundFrameRender(call, args=(), kwargs={}): 352 # """Remove a previously-added callback with the same arguments.""" 353 # _removeBackgroundCallback(beforeBackgroundFrameRenders, call, args, kwargs) 354 #def beforeBackgroundFrameRender(): 355 # _doBackgroundCallbacks(beforeBackgroundFrameRenders) 356 357 afterBackgroundFrameRenders=[]359 """Add code to execute after each frame of a background render. 360 The call must be in the form of: 361 def foo(context): 362 pass 363 364 The context object that will be passed in is a dictionary containing the following elements: 365 id => The identifier for the task that's making progress 366 frame => the current frame number being rendered 367 numFrames => the total number of frames that is being rendered 368 frameProgress => the number of frames rendered so far. 369 370 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 371 """ 372 _addBackgroundCallback(afterBackgroundFrameRenders, call, args, kwargs)374 """Remove a previously-added callback with the same arguments.""" 375 _removeBackgroundCallback(afterBackgroundFrameRenders, call, args, kwargs)378 379 afterBackgroundRenders=[]381 """Add code to execute after any background renders. 382 The call must be in the form of: 383 def foo(context): 384 pass 385 386 The context object that will be passed in is a dictionary containing the following elements: 387 id => The identifier for the task that's ended 388 389 Please be aware that the current Nuke context will not make sense in the callback (e.g. nuke.thisNode will return a random node). 390 """ 391 _addBackgroundCallback(afterBackgroundRenders, call, args, kwargs)393 """Remove a previously-added callback with the same arguments.""" 394 _removeBackgroundCallback(afterBackgroundRenders, call, args, kwargs)397 398 # filenameFilter is somewhat different due to it returning a string 399 filenameFilters={}401 """Add a function to modify filenames before Nuke passes them to 402 the operating system. The first argument to the function is the 403 filename, and it should return the new filename. None is the same as 404 returning the string unchanged. All added functions are called 405 in backwards order.""" 406 _addCallback(filenameFilters, call, args, kwargs, nodeClass)408 """Remove a previously-added callback with the same arguments.""" 409 _removeCallback(filenameFilters, call, args, kwargs, nodeClass)410412 import __main__ 413 list = filenameFilters.get(nuke.thisClass()) 414 if list: 415 for f in list[::-1]: 416 s = f[0](filename,*f[1],**f[2]) 417 if s != None: filename = s 418 list = filenameFilters.get('*') 419 if list: 420 for f in list[::-1]: 421 s = f[0](filename,*f[1],**f[2]) 422 if s != None: filename = s 423 if not len(filenameFilters): 424 # For back-compatibility allow user to define a filenameFix() function: 425 if __main__.__dict__.has_key('filenameFix'): 426 return __main__.__dict__['filenameFix'](filename) 427 # For even further back-compatibility let them define a tcl filename_fix function: 428 return nuke.tcl("filename_fix",filename) 429 return filename430 431 validateFilenames={}433 """Add a function to validate a filename in Write nodes. The first argument 434 is the filename and it should return a Boolean as to whether the filename is valid 435 or not. If a callback is provided, it will control whether the Render button of Write nodes 436 and the Execute button of WriteGeo nodes is enabled or not.""" 437 _addCallback(validateFilenames, call, args, kwargs, nodeClass)439 """Remove a previously-added callback.""" 440 _removeCallback(validateFilenames, call, args, kwargs, nodeClass)442 import __main__ 443 list = validateFilenames.get(nuke.thisClass()) 444 valid = True 445 446 if list: 447 for f in list: 448 b = f[0](filename) 449 if b == False: valid = False 450 list = validateFilenames.get('*') 451 if list: 452 for f in list: 453 b = f[0](filename) 454 if b == False: valid = False 455 return valid456 457459 import __main__ 460 list = filters.get( 'Root' ) 461 if list: 462 for f in list: 463 s = f[0](filename) 464 filename = s 465 466 return filename467 468 autoSaveFilters={}470 """addAutoSaveFilter(filter) -> None 471 472 Add a function to modify the autosave filename before Nuke saves the current script on an autosave timeout. 473 474 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 475 476 @param filter: A filter function. The first argument to the filter is the current autosave filename. 477 The filter should return the filename to save the autosave to.""" 478 _addCallback(autoSaveFilters, filter, (), {}, 'Root')479481 """Remove a previously-added callback with the same arguments.""" 482 _removeCallback(autoSaveFilters, call, (), {}, 'Root')483485 """Internal function. Use addAutoSaveFilter to add a callback""" 486 return _doAutoSaveCallbacks( autoSaveFilters, filename )487 488 489 autoSaveRestoreFilters={}491 """addAutoSaveRestoreFilter(filter) -> None 492 493 Add a function to modify the autosave restore file before Nuke attempts to restores the autosave file. 494 495 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 496 497 @param filter: A filter function. The first argument to the filter is the current autosave filename. 498 This function should return the filename to load autosave from or it should return None if the autosave file should be ignored.""" 499 _addCallback(autoSaveRestoreFilters, filter, (), {}, 'Root')500502 """Remove a previously-added callback with the same arguments.""" 503 _removeCallback(autoSaveRestoreFilters, filter, (), {}, 'Root')504506 """Internal function. Use addAutoSaveRestoreFilter to add a callback""" 507 return _doAutoSaveCallbacks( autoSaveRestoreFilters, filename )508 509 autoSaveDeleteFilters={}511 """addAutoSaveDeleteFilter(filter) -> None 512 513 Add a function to modify the autosave filename before Nuke attempts delete the autosave file. 514 515 Look at rollingAutoSave.py in the nukescripts directory for an example of using the auto save filters. 516 517 @param filter: A filter function. The first argument to the filter is the current autosave filename. 518 This function should return the filename to delete or return None if no file should be deleted.""" 519 _addCallback(autoSaveDeleteFilters, filter, (), {}, 'Root')520522 """Remove a previously-added callback with the same arguments.""" 523 _removeCallback(autoSaveDeleteFilters, filter, (), {}, 'Root')524526 """Internal function. Use addAutoSaveDeleteFilter to add a callback""" 527 return _doAutoSaveCallbacks( autoSaveDeleteFilters, filename )528
Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Tue Dec 5 06:16:28 2017 | http://epydoc.sourceforge.net |