nukescripts.nodepresets

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

      6 -class CreateNodePresetsPanel(nukescripts.PythonPanel):
      7 -  def __init__(self, node):
      8      self.node = node
      9

     10      nukescripts.PythonPanel.__init__( self, 'Create Node Preset', 'uk.co.thefoundry.CreateNodePreset')
     11

     12      self.menuPath = nuke.String_Knob('presetname', 'Name:')
     13      self.menuPath.setFlag(0x00001000)
     14      self.okButton = nuke.PyScript_Knob ('create', 'Create')
     15      self.okButton.setFlag(0x00001000)
     16      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
     17

     18      # ADD KNOBS

     19      self.addKnob(self.menuPath)
     20      self.addKnob(self.okButton)
     21      self.addKnob(self.cancelButton)
     22

     23 -  def createPreset(self):
     24      if createNodePreset(self.node, str(self.menuPath.value())):
     25        self.finishModalDialog( True )
     26

     27 -  def knobChanged( self, knob ):
     28      if knob == self.okButton:
     29        self.createPreset()
     30      elif knob == self.cancelButton:
     31        self.finishModalDialog( False )
     32

     33 -class UserPresetsLoadPanel(nukescripts.PythonPanel):
     34 -  def __init__(self):
     35      nukescripts.PythonPanel.__init__( self, 'Load User Preset', 'uk.co.thefoundry.LoadUserPreset')
     36      self.presets = []
     37      self.presets = nuke.getUserPresets()
     38

     39      self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets)
     40      self.okButton = nuke.PyScript_Knob ('load', 'Load')
     41      self.okButton.setFlag(0x00001000)
     42      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
     43

     44      # ADD KNOBS

     45      self.addKnob(self.menuItemChoice)
     46      self.addKnob(self.okButton)
     47      self.addKnob(self.cancelButton)
     48

     49 -  def loadPreset(self):
     50      if nuke.applyUserPreset(str(self.menuItemChoice.value())):
     51        self.finishModalDialog( True )
     52

     53 -  def knobChanged( self, knob ):
     54      if knob == self.okButton:
     55        self.loadPreset()
     56      elif knob == self.cancelButton:
     57        self.finishModalDialog( False )
     58

     59 -class UserPresetsDeletePanel(nukescripts.PythonPanel):
     60 -  def __init__(self):
     61      nukescripts.PythonPanel.__init__( self, 'Delete User Preset', 'uk.co.thefoundry.DeleteUserPreset')
     62      self.presets = []
     63      self.presets = nuke.getUserPresets()
     64

     65      self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets)
     66      self.okButton = nuke.PyScript_Knob ('delete', 'Delete')
     67      self.okButton.setFlag(0x00001000)
     68      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
     69

     70      # ADD KNOBS

     71      self.addKnob(self.menuItemChoice)
     72      self.addKnob(self.okButton)
     73      self.addKnob(self.cancelButton)
     74

     75 -  def deletePreset(self):
     76      if deleteUserNodePreset(nuke.getNodeClassName(), str(self.menuItemChoice.value())):
     77        self.finishModalDialog( True )
     78

     79 -  def knobChanged( self, knob ):
     80      if knob == self.okButton:
     81        self.deletePreset()
     82      elif knob == self.cancelButton:
     83        self.finishModalDialog( False )
     84

     85 -class PresetsLoadPanel(nukescripts.PythonPanel):
     86 -  def __init__(self):
     87      nukescripts.PythonPanel.__init__( self, 'Load Preset', 'uk.co.thefoundry.LoadPreset')
     88      self.presets = []
     89      self.presets = nuke.getPresets()
     90

     91      self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets)
     92      self.okButton = nuke.PyScript_Knob ('load', 'Load')
     93      self.okButton.setFlag(0x00001000)
     94      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
     95

     96      # ADD KNOBS

     97      self.addKnob(self.menuItemChoice)
     98      self.addKnob(self.okButton)
     99      self.addKnob(self.cancelButton)
    100

    101 -  def loadPreset(self):
    102      if nuke.applyPreset(str(self.menuItemChoice.value())):
    103        self.finishModalDialog( True )
    104

    105 -  def knobChanged( self, knob ):
    106      if knob == self.okButton:
    107        self.loadPreset()
    108      elif knob == self.cancelButton:
    109        self.finishModalDialog( False )
    110

    111 -class PresetsDeletePanel(nukescripts.PythonPanel):
    112 -  def __init__(self):
    113      nukescripts.PythonPanel.__init__( self, 'Delete Preset', 'uk.co.thefoundry.DeletePreset')
    114      self.presets = []
    115      self.presets = nuke.getPresets()
    116

    117      self.menuItemChoice = nuke.Enumeration_Knob('menuItemChoice','Menu item', self.presets)
    118      self.okButton = nuke.PyScript_Knob ('delete', 'Delete')
    119      self.okButton.setFlag(0x00001000)
    120      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
    121

    122      # ADD KNOBS

    123      self.addKnob(self.menuItemChoice)
    124      self.addKnob(self.okButton)
    125      self.addKnob(self.cancelButton)
    126

    127 -  def deletePreset(self):
    128      if deleteNodePreset(nuke.getNodeClassName(), str(self.menuItemChoice.value())):
    129        self.finishModalDialog( True )
    130

    131 -  def knobChanged( self, knob ):
    132      if knob == self.okButton:
    133        self.deletePreset()
    134      elif knob == self.cancelButton:
    135        self.finishModalDialog( False )
    136

    137 -def createNodePresetsMenu():
    138    nodePresetsStartup()
    139    #m = nuke.menu("Properties")

    140    #n = m.addMenu("Presets")

    141    #s1 = n.addMenu("Load Preset")

    142    #s1.addCommand("User Preset", "nukescripts.buildUserPresetLoadPanel()")

    143    #s1.addCommand("Shared Preset", "nukescripts.buildPresetLoadPanel()")

    144    #n.addCommand("Save as Preset", "nukescripts.buildPresetSavePanel()")

    145    #s2 = n.addMenu("Delete Preset")

    146    #s2.addCommand("User Preset", "nukescripts.buildUserPresetDeletePanel()")

    147    #s2.addCommand("Shared Preset", "nukescripts.buildPresetDeletePanel()")

    148

    149 -def populatePresetsMenu(nodeName, className):
    150    knobValues = {}
    151    node = nuke.toNode(nodeName)
    152    m = nuke.getPresetsMenu(node)
    153    # reset the menus in case of preset deletion

    154    m.clearMenu()
    155    presets = []
    156    userpresets = []
    157    userpresets = nuke.getUserPresets(node)
    158    presets = nuke.getPresets(node)
    159    allpresets = userpresets + presets
    160    m.addCommand("Save as Preset", 'nukescripts.buildPresetSavePanel("%s")' % nodeName)
    161    if allpresets != []:
    162      m.addCommand("-", "", "")
    163      for k in userpresets:
    164        m.addCommand('[User] ' + k, 'nuke.applyUserPreset("%s", "%s")\n' % (nodeName, k))
    165      for k in presets:
    166        m.addCommand(k, 'nuke.applyPreset("%s", "%s")\n' % (nodeName, k))
    167    if allpresets != []:
    168      m.addCommand("-", "", "")
    169      n = m.addMenu("Delete Preset")
    170      for k in userpresets:
    171        n.addCommand('[User] ' + k, 'nukescripts.deleteUserNodePreset("%s", "%s")\n' % (className, k))
    172      for k in presets:
    173        n.addCommand(k, 'nukescripts.deleteNodePreset("%s", "%s")\n' % (className, k))
    174

    175 -def buildUserPresetLoadPanel():
    176    presets = []
    177    presets = nuke.getUserPresets()
    178    if presets == []:
    179      nuke.message("No user presets defined.")
    180    else:
    181      UserPresetsLoadPanel().showModalDialog()
    182

    183 -def buildPresetLoadPanel():
    184    presets = []
    185    presets = nuke.getPresets()
    186    if presets == []:
    187      nuke.message("No presets defined.")
    188    else:
    189      PresetsLoadPanel().showModalDialog()
    190

    191 -def buildPresetSavePanel(nodeName):
    192    return CreateNodePresetsPanel(nuke.toNode(nodeName)).showModalDialog()
    193

    194 -def buildUserPresetDeletePanel():
    195    presets = []
    196    presets = nuke.getUserPresets()
    197    if presets == []:
    198      nuke.message("No user presets defined.")
    199    else:
    200      UserPresetsDeletePanel().showModalDialog()
    201

    202 -def buildPresetDeletePanel():
    203    presets = []
    204    presets = nuke.getPresets()
    205    if presets == []:
    206      nuke.message("No presets defined.")
    207    else:
    208      PresetsDeletePanel().showModalDialog()
    209

    210 -def saveNodePresets():
    211    userPresets = []
    212    deletedPresets = []
    213    namePair = ()
    214    knobValues = {}
    215    filename = ''
    216    userPresets = nuke.getAllUserPresets()
    217    deletedPresets = nuke.getDeletedPresets()
    218    # find the .nuke folder path

    219    for d in nuke.pluginPath():
    220      if os.path.isdir(d):
    221        if ".nuke" in d:
    222          filename = os.path.join(d, "user_presets.py")
    223          if filename != '':
    224            with open(filename, "w") as outFile:
    225              outFile.write('import nuke\n')
    226              outFile.write('def nodePresetsStartup():\n')
    227              if userPresets != []:
    228                for k in userPresets:
    229                  namePair = k
    230                  knobValues = nuke.getUserPresetKnobValues(namePair[0], namePair[1])
    231                  outFile.write('  nuke.setUserPreset("%s", "%s", %s)\n' % (k[0], k[1], repr(knobValues)))
    232              if deletedPresets != []:
    233                for k in deletedPresets:
    234                  namePair = k
    235                  outFile.write('  nuke.deletePreset("%s", "%s")\n' % (namePair[0], namePair[1]))
    236

    237 -def createNodePreset(node, name):
    238    nuke.saveUserPreset(node, name)
    239    saveNodePresets()
    240

    241 -def deleteNodePreset(classname, presetName):
    242    if nuke.ask("Are you sure you want to delete preset " + presetName + "?"):
    243      nuke.deletePreset(classname, presetName)
    244      saveNodePresets()
    245

    246 -def deleteUserNodePreset(classname, presetName):
    247    if nuke.ask("Are you sure you want to delete preset " + presetName + "?"):
    248      nuke.deleteUserPreset(classname, presetName)
    249      saveNodePresets()
    250

    251  try:
    252    from user_presets import *
    253  except:
    254 -  def nodePresetsStartup(): pass
    255