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 m.addCommand("Save as Preset", 'nukescripts.buildPresetSavePanel("%s")' % nodeName) 251 if allpresets != []: 252 m.addCommand("-", "", "") 253 for k in userpresets: 254 m.addCommand('[User] ' + k, 'nuke.applyUserPreset("%s", "%s")\n' % (nodeName, k)) 255 for k in presets: 256 m.addCommand(k, 'nuke.applyPreset("%s", "%s")\n' % (nodeName, k)) 257 if allpresets != []: 258 m.addCommand("-", "", "") 259 n = m.addMenu("Delete Preset") 260 for k in userpresets: 261 n.addCommand('[User] ' + k, 'nukescripts.deleteUserNodePreset("%s", "%s")\n' % (className, k)) 262 for k in presets: 263 n.addCommand(k, 'nukescripts.deleteNodePreset("%s", "%s")\n' % (className, k))
264
265 -def buildUserPresetLoadPanel():
266 presets = [] 267 presets = nuke.getUserPresets() 268 if presets == []: 269 nuke.message("No user presets defined.") 270 else: 271 UserPresetsLoadPanel().showModalDialog()
272
273 -def buildPresetLoadPanel():
274 presets = [] 275 presets = nuke.getPresets() 276 if presets == []: 277 nuke.message("No presets defined.") 278 else: 279 PresetsLoadPanel().showModalDialog()
280
281 -def buildPresetSavePanel(nodeName):
282 return CreateNodePresetsPanel(nuke.toNode(nodeName)).showModalDialog()
283
284 -def buildUserPresetDeletePanel():
285 presets = [] 286 presets = nuke.getUserPresets() 287 if presets == []: 288 nuke.message("No user presets defined.") 289 else: 290 UserPresetsDeletePanel().showModalDialog()
291
292 -def buildPresetDeletePanel():
293 presets = [] 294 presets = nuke.getPresets() 295 if presets == []: 296 nuke.message("No presets defined.") 297 else: 298 PresetsDeletePanel().showModalDialog()
299
300 -def getItemDirName(d, item):
301 # Convert a preset's name (including it's menu hierarchy) into the corresponding directory 302 # for it's user_presets.py file 303 itemName = os.path.join(d, "NodePresets") 304 itemName = os.path.join(itemName, item[1]) 305 306 length = len(os.path.basename(itemName)) 307 308 itemName = itemName[:len(itemName) - length - 1] 309 310 return itemName
311
312 -def saveNodePresets():
313 314 # Get the root location for writing preset files to. By default we get the nuke user folder (.nuke), 315 # and a NodePresets sub-directory is created below this. 316 # user_presets.py files are then written in a directory tree inside this in a hierarchy that mimics their 317 # names at creation (ie '/' separated strings to specify a relative directory path) 318 319 # find the .nuke folder path 320 d = getNukeUserFolder() 321 if d != None and os.path.isdir(d): 322 userPresets = nuke.getAllUserPresets() 323 deletedPresets = nuke.getDeletedPresets() 324 325 for k in userPresets: 326 dirName = getItemDirName(d, k) 327 328 try: 329 os.makedirs(dirName) 330 except OSError as exc: 331 if exc.errno == errno.EEXIST: 332 pass 333 else: raise 334 335 with open(os.path.join(dirName, "user_presets.py"), "w") as outfile: 336 outfile.write('import nuke\n') 337 outfile.write('def nodePresetsStartup():\n') 338 339 if userPresets != []: 340 for k in userPresets: 341 filename = os.path.join(getItemDirName(d, k), "user_presets.py") 342 343 with open(filename, "a") as outfile: 344 namePair = k 345 knobValues = nuke.getUserPresetKnobValues(namePair[0], namePair[1]) 346 outfile.write(' nuke.setUserPreset("%s", "%s", %s)\n' % (k[0], k[1], repr(knobValues))) 347 348 if deletedPresets != []: 349 filename = os.path.join(d, "user_presets.py") 350 with open(filename, "w") as outfile: 351 outfile.write('import nuke\n') 352 outfile.write('def nodePresetsStartup():\n') 353 for k in deletedPresets: 354 namePair = k 355 outfile.write(' nuke.deletePreset("%s", "%s")\n' % (namePair[0], namePair[1]))
356
357 -def createNodePreset(node, name):
358 nuke.saveUserPreset(node, name) 359 saveNodePresets()
360
361 -def deleteNodePreset(classname, presetName):
362 if nuke.ask("Are you sure you want to delete preset " + presetName + "?"): 363 nuke.deletePreset(classname, presetName) 364 saveNodePresets()
365
366 -def deleteUserNodePreset(classname, presetName):
367 if nuke.ask("Are you sure you want to delete preset " + presetName + "?"): 368 nuke.deleteUserPreset(classname, presetName) 369 saveNodePresets()
370