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

Source Code for Module nukescripts.nodepresets

  1  import os 
  2  import nuke 
  3  import nukescripts 
  4  import fileinput 
  5  import imp 
  6  import errno 
  7   
8 -def getNukeUserFolder():
9 for d in nuke.pluginPath(): 10 if os.path.isdir(d): 11 if d.endswith(".nuke"): 12 return d 13 return None
14
15 -def buildPresetFileList(fullPath):
16 fileList = [] 17 if not os.path.exists(fullPath) or not os.path.isdir(fullPath): 18 return fileList 19 20 fileName = fullPath + "/user_presets.py" 21 if os.path.exists(fileName): 22 fileList.append(fileName) 23 24 filecontents = os.listdir(fullPath + '/') 25 for d in filecontents: 26 newPath = os.path.join(fullPath, d) 27 if os.path.isdir(newPath): 28 fileList += buildPresetFileList(newPath) 29 30 return fileList
31
32 -class CreateNodePresetsPanel(nukescripts.PythonPanel):
33 - def __init__(self, node):
34 self.node = node 35 36 # Loop through and find all user folders 37 userFolder = getNukeUserFolder() 38 newFileList = [] 39 if userFolder != None: 40 userFolder = os.path.join(userFolder, "NodePresets") 41 fileList = buildPresetFileList(userFolder) 42 43 for file in fileList: 44 # We need to convert these absolute filenames into items that can be passed to a cascading enumeration knob. 45 # First strip the nuke user folder path from the start 46 newFileName = file[len(str(userFolder)):].replace("\\", "/") 47 48 # Now strip the "/user_presets.py" part from the end 49 endSize = 16 50 51 if len(newFileName) > endSize: 52 newFileName = newFileName[:len(newFileName) - endSize] 53 54 if newFileName != "/user_presets.py": 55 if newFileName[:1] == '/': 56 newFileName = newFileName[1:] 57 newFileList.append(newFileName) 58 59 nukescripts.PythonPanel.__init__( self, 'Create Node Preset', 'uk.co.thefoundry.CreateNodePreset') 60 61 self.menuItemChoice = nuke.CascadingEnumeration_Knob('menuItemChoice','Presets menu', ['root'] + newFileList) 62 self.menuItemChoice.setTooltip("The menu location that the Preset will appear in. Specify 'root' to place the Preset in the main Presets menu.") 63 self.menuPath = nuke.String_Knob('presetname', 'Name:') 64 self.menuPath.setFlag(0x00001000) 65 self.okButton = nuke.PyScript_Knob ('create', 'Create') 66 self.okButton.setFlag(0x00001000) 67 self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel') 68 69 # ADD KNOBS 70 self.addKnob(self.menuItemChoice) 71 self.addKnob(self.menuPath) 72 self.addKnob(self.okButton) 73 self.addKnob(self.cancelButton)
74
75 - def createPreset(self):
76 if createNodePreset(self.node, str(self.menuPath.value())): 77 self.finishModalDialog( True )
78
79 - def getPresetPath(self):
80 if str(self.menuItemChoice.value()) == "root": 81 self.menuPath.setValue("") 82 else: 83 self.menuPath.setValue(self.menuItemChoice.value() + "/")
84
85 - def knobChanged( self, knob ):
86 if knob == self.okButton: 87 self.createPreset() 88 elif knob == self.cancelButton: 89 self.finishModalDialog( False ) 90 elif knob == self.menuItemChoice: 91 self.getPresetPath()
92
93 -class UserPresetsLoadPanel(nukescripts.PythonPanel):
94 - def __init__(self):
95 nukescripts.PythonPanel.__init__( self, 'Load User Preset', 'uk.co.thefoundry.LoadUserPreset') 96 self.presets = [] 97 self.presets = nuke.getUserPresets() 98 99 self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets) 100 self.okButton = nuke.PyScript_Knob ('load', 'Load') 101 self.okButton.setFlag(0x00001000) 102 self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel') 103 104 # ADD KNOBS 105 self.addKnob(self.menuItemChoice) 106 self.addKnob(self.okButton) 107 self.addKnob(self.cancelButton)
108
109 - def loadPreset(self):
110 if nuke.applyUserPreset(str(self.menuItemChoice.value())): 111 self.finishModalDialog( True )
112
113 - def knobChanged( self, knob ):
114 if knob == self.okButton: 115 self.loadPreset() 116 elif knob == self.cancelButton: 117 self.finishModalDialog( False )
118
119 -class UserPresetsDeletePanel(nukescripts.PythonPanel):
120 - def __init__(self):
121 nukescripts.PythonPanel.__init__( self, 'Delete User Preset', 'uk.co.thefoundry.DeleteUserPreset') 122 self.presets = [] 123 self.presets = nuke.getUserPresets() 124 125 self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets) 126 self.okButton = nuke.PyScript_Knob ('delete', 'Delete') 127 self.okButton.setFlag(0x00001000) 128 self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel') 129 130 # ADD KNOBS 131 self.addKnob(self.menuItemChoice) 132 self.addKnob(self.okButton) 133 self.addKnob(self.cancelButton)
134
135 - def deletePreset(self):
136 if deleteUserNodePreset(nuke.getNodeClassName(), str(self.menuItemChoice.value())): 137 self.finishModalDialog( True )
138
139 - def knobChanged( self, knob ):
140 if knob == self.okButton: 141 self.deletePreset() 142 elif knob == self.cancelButton: 143 self.finishModalDialog( False )
144
145 -class PresetsLoadPanel(nukescripts.PythonPanel):
146 - def __init__(self):
147 nukescripts.PythonPanel.__init__( self, 'Load Preset', 'uk.co.thefoundry.LoadPreset') 148 self.presets = [] 149 self.presets = nuke.getPresets() 150 151 self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets) 152 self.okButton = nuke.PyScript_Knob ('load', 'Load') 153 self.okButton.setFlag(0x00001000) 154 self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel') 155 156 # ADD KNOBS 157 self.addKnob(self.menuItemChoice) 158 self.addKnob(self.okButton) 159 self.addKnob(self.cancelButton)
160
161 - def loadPreset(self):
162 if nuke.applyPreset(str(self.menuItemChoice.value())): 163 self.finishModalDialog( True )
164
165 - def knobChanged( self, knob ):
166 if knob == self.okButton: 167 self.loadPreset() 168 elif knob == self.cancelButton: 169 self.finishModalDialog( False )
170
171 -class PresetsDeletePanel(nukescripts.PythonPanel):
172 - def __init__(self):
173 nukescripts.PythonPanel.__init__( self, 'Delete Preset', 'uk.co.thefoundry.DeletePreset') 174 self.presets = [] 175 self.presets = nuke.getPresets() 176 177 self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets) 178 self.okButton = nuke.PyScript_Knob ('delete', 'Delete') 179 self.okButton.setFlag(0x00001000) 180 self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel') 181 182 # ADD KNOBS 183 self.addKnob(self.menuItemChoice) 184 self.addKnob(self.okButton) 185 self.addKnob(self.cancelButton)
186
187 - def deletePreset(self):
188 if deleteNodePreset(nuke.getNodeClassName(), str(self.menuItemChoice.value())): 189 self.finishModalDialog( True )
190
191 - def knobChanged( self, knob ):
192 if knob == self.okButton: 193 self.deletePreset() 194 elif knob == self.cancelButton: 195 self.finishModalDialog( False )
196
197 -def processPresetFile(location):
198 fulName = "" 199 if str(location).endswith('/') or str(location).endswith('\\'): 200 fulName = str(location) + "user_presets.py" 201 else: 202 fulName = str(location) + "/user_presets.py" 203 204 if os.path.exists(fulName): 205 try: 206 module = imp.load_source('user_presets', fulName) 207 module.nodePresetsStartup() 208 except: 209 pass
210
211 -def createNodePresetsMenu():
212 for d in nuke.pluginPath(): 213 214 if ".nuke" in d: 215 nuke.setReadOnlyPresets(False) 216 else: 217 nuke.setReadOnlyPresets(True) 218 219 # First look for a user_presets.py in the nuke path 220 processPresetFile(d) 221 222 # Now load all user_presets.py files in a directory tree below nodePresets 223 fulName = os.path.join(d, "NodePresets") 224 userFiles = buildPresetFileList(fulName) 225 226 for pyFile in userFiles: 227 try: 228 module = imp.load_source('user_presets', pyFile) 229 module.nodePresetsStartup() 230 except: 231 pass 232 233 nuke.setReadOnlyPresets(False) 234 235 # now parse the .nuke root presets again to make sure deleted items get processed properly 236 processPresetFile(getNukeUserFolder())
237 238
239 -def populatePresetsMenu(nodeName, className):
240 knobValues = {} 241 node = nuke.toNode(nodeName) 242 m = nuke.getPresetsMenu(node) 243 # reset the menus in case of preset deletion 244 m.clearMenu() 245 presets = [] 246 userpresets = [] 247 userpresets = nuke.getUserPresets(node) 248 presets = nuke.getPresets(node) 249 allpresets = userpresets + presets 250 251 buildSavePanelFunc = lambda node = node: nukescripts.buildPresetSavePanel("", node) 252 m.addCommand("Save as Preset", buildSavePanelFunc) 253 254 if allpresets != []: 255 m.addCommand("-", "", "") 256 for k in userpresets: 257 f = lambda node=node, k=k : nuke.applyUserPreset("", k, node) 258 m.addCommand('[User] ' + k, f) 259 for k in presets: 260 f = lambda node=node, k=k : nuke.applyPreset("", k, node) 261 m.addCommand(k, f) 262 if allpresets != []: 263 m.addCommand("-", "", "") 264 n = m.addMenu("Delete Preset") 265 for k in userpresets: 266 n.addCommand('[User] ' + k, 'nukescripts.deleteUserNodePreset("%s", "%s")\n' % (className, k)) 267 for k in presets: 268 n.addCommand(k, 'nukescripts.deleteNodePreset("%s", "%s")\n' % (className, k))
269
270 -def buildUserPresetLoadPanel():
271 presets = [] 272 presets = nuke.getUserPresets() 273 if presets == []: 274 nuke.message("No user presets defined.") 275 else: 276 UserPresetsLoadPanel().showModalDialog()
277
278 -def buildPresetLoadPanel():
279 presets = [] 280 presets = nuke.getPresets() 281 if presets == []: 282 nuke.message("No presets defined.") 283 else: 284 PresetsLoadPanel().showModalDialog()
285
286 -def buildPresetSavePanel(nodeName, node = None):
287 if (node == None): 288 node = nuke.toNode(nodeName) 289 return CreateNodePresetsPanel(node).showModalDialog()
290
291 -def buildUserPresetDeletePanel():
292 presets = [] 293 presets = nuke.getUserPresets() 294 if presets == []: 295 nuke.message("No user presets defined.") 296 else: 297 UserPresetsDeletePanel().showModalDialog()
298
299 -def buildPresetDeletePanel():
300 presets = [] 301 presets = nuke.getPresets() 302 if presets == []: 303 nuke.message("No presets defined.") 304 else: 305 PresetsDeletePanel().showModalDialog()
306
307 -def getItemDirName(d, item):
308 # Convert a preset's name (including it's menu hierarchy) into the corresponding directory 309 # for it's user_presets.py file 310 itemName = os.path.join(d, "NodePresets") 311 itemName = os.path.join(itemName, item[1]) 312 313 length = len(os.path.basename(itemName)) 314 315 itemName = itemName[:len(itemName) - length - 1] 316 317 return itemName
318
319 -def saveNodePresets():
320 321 # Get the root location for writing preset files to. By default we get the nuke user folder (.nuke), 322 # and a NodePresets sub-directory is created below this. 323 # user_presets.py files are then written in a directory tree inside this in a hierarchy that mimics their 324 # names at creation (ie '/' separated strings to specify a relative directory path) 325 326 # find the .nuke folder path 327 d = getNukeUserFolder() 328 if d != None and os.path.isdir(d): 329 userPresets = nuke.getAllUserPresets() 330 deletedPresets = nuke.getDeletedPresets() 331 332 for k in userPresets: 333 dirName = getItemDirName(d, k) 334 335 try: 336 os.makedirs(dirName) 337 except OSError as exc: 338 if exc.errno == errno.EEXIST: 339 pass 340 else: raise 341 342 with open(os.path.join(dirName, "user_presets.py"), "w") as outfile: 343 outfile.write('import nuke\n') 344 outfile.write('def nodePresetsStartup():\n') 345 346 if userPresets != []: 347 for k in userPresets: 348 filename = os.path.join(getItemDirName(d, k), "user_presets.py") 349 350 with open(filename, "a") as outfile: 351 namePair = k 352 knobValues = nuke.getUserPresetKnobValues(namePair[0], namePair[1]) 353 outfile.write(' nuke.setUserPreset("%s", "%s", %s)\n' % (k[0], k[1], repr(knobValues))) 354 355 if deletedPresets != []: 356 filename = os.path.join(d, "user_presets.py") 357 with open(filename, "w") as outfile: 358 outfile.write('import nuke\n') 359 outfile.write('def nodePresetsStartup():\n') 360 for k in deletedPresets: 361 namePair = k 362 outfile.write(' nuke.deletePreset("%s", "%s")\n' % (namePair[0], namePair[1]))
363
364 -def createNodePreset(node, name):
365 nuke.saveUserPreset(node, name) 366 saveNodePresets()
367
368 -def deleteNodePreset(classname, presetName):
369 if nuke.ask("Are you sure you want to delete preset " + presetName + "?"): 370 nuke.deletePreset(classname, presetName) 371 saveNodePresets()
372
373 -def deleteUserNodePreset(classname, presetName):
374 if nuke.ask("Are you sure you want to delete preset " + presetName + "?"): 375 nuke.deleteUserPreset(classname, presetName) 376 saveNodePresets()
377