nukescripts.toolsets

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

# Source Code for Module nukescripts.toolsets

      1  import os
      2  import nuke
      3  import nukescripts
      4
      5 -class CreateToolsetsPanel(nukescripts.PythonPanel):
      6 -  def __init__(self):
      7      nukescripts.PythonPanel.__init__( self, 'Create ToolSet', 'uk.co.thefoundry.CreateToolset')
      8
      9      # CREATE KNOBS
     10      # Loop through and find all user folders
     11      self.userFolders = []
     12      for d in nuke.pluginPath():
     13        if os.path.isdir(d):
     14          if ".nuke" in d:
     15            dircontents = os.listdir(d)
     16            if "ToolSets" in dircontents:
     17              fullPath = os.path.join(d, "ToolSets")
     18              self.buildFolderList(fullPath, '')
     19
     20      self.menuItemChoice = nuke.CascadingEnumeration_Knob('menuItemChoice','ToolSets menu', ['root'] + self.userFolders)
     21      self.menuItemChoice.setTooltip("The menu location that the ToolSet will appear in. Specify 'root' to place the ToolSet in the main ToolSets menu.")
     22      self.menuPath = nuke.String_Knob('itemName', 'Menu item:')
     23      self.menuPath.setFlag(0x00001000)
     24      self.menuPath.setTooltip("ToolSet name. Use the '/' character to create a new submenu for this ToolSet, eg to create a ToolSet named 'Basic3D' and place it in a new submenu '3D', type '3D/Basic3D'. Once created the 3D menu will appear in the ToolSet menu.")
     25      self.okButton = nuke.PyScript_Knob ('create', 'Create')
     26      #self.okButton.setToolTip("Create a ToolSet from the currently selected nodes with the given name")
     27      self.okButton.setFlag(0x00001000)
     28      self.cancelButton = nuke.PyScript_Knob ('cancel', 'Cancel')
     29
     30      # ADD KNOBS
     31      self.addKnob(self.menuItemChoice)
     32      self.addKnob(self.menuPath)
     33      self.addKnob(self.okButton)
     34      self.addKnob(self.cancelButton)
     35
     36    # BUILD A LIST Of PRE_CREATED FOLDER LOCATIONS
     37 -  def buildFolderList(self, fullPath, menuPath):
     38      filecontents = sorted(os.listdir(fullPath), key=str.lower)
     39      for group in filecontents:
     40        if os.path.isdir(os.path.join(fullPath, group)):
     41          self.userFolders.append(menuPath + group)
     42          self.buildFolderList(fullPath + '/' + group, menuPath + group + '/')
     43
     44 -  def createPreset(self):
     45      if nuke.createToolset(str(self.menuPath.value())):
     46        self.finishModalDialog( True )
     47
     48 -  def getPresetPath(self):
     49            if str(self.menuItemChoice.value()) == "root":
     50                    self.menuPath.setValue("")
     51            else:
     52                    self.menuPath.setValue(self.menuItemChoice.value() + "/")
     53
     54 -  def knobChanged( self, knob ):
     55      if knob == self.okButton:
     56        self.createPreset()
     57      elif knob == self.cancelButton:
     58        self.finishModalDialog( False )
     59      elif knob == self.menuItemChoice:
     60        self.getPresetPath()
     61
     62  # NUKESCRIPT FUNCTIONS
     63
     64 -def addToolsetsPanel():
     65    res = False
     66    if nuke.nodesSelected() == True:
     67      res = CreateToolsetsPanel().showModalDialog()
     68      # now force a rebuild of the menu
     69      refreshToolsetsMenu()
     70    else:
     71      nuke.message("No nodes are selected")
     72    return res
     73
     74 -def deleteToolset(rootPath, fileName):
     75    if nuke.ask('Are you sure you want to delete ToolSet %s?' %fileName):
     76      os.remove(fileName)
     77      # if this was the last file in this directory, the folder will need to be deleted.
     78      # Walk the directory tree from the root and recursively delete empty directories
     79      checkForEmptyToolsetDirectories(rootPath)
     80      # now force a rebuild of the menu
     81      refreshToolsetsMenu()
     82
     83 -def checkForEmptyToolsetDirectories(currPath):
     84    removed = True
     85    while removed == True:
     86      removed = False
     87      for root, dirs, files in os.walk(currPath):
     88        if files == [] and dirs == []:
     89          os.rmdir(root)
     90          removed = True
     91
     92 -def refreshToolsetsMenu():
     93    toolbar = nuke.menu("Nodes")
     94    m = toolbar.findItem("ToolSets")
     95    if m != None:
     96      m.clearMenu()
     97      createToolsetsMenu(toolbar)
     98
     99 -def createToolsetsMenu(toolbar):
    100    m = toolbar.addMenu("ToolSets", "ToolbarToolsets.png")
    101    m.addCommand("Create", "nukescripts.toolsets.addToolsetsPanel()", "", icon="ToolsetCreate.png")
    102    m.addCommand("-", "", "")
    103    if buildToolsetsMenu(m, False):
    104      m.addCommand("-", "", "")
    105      n = m.addMenu("Delete", "ToolsetDelete.png")
    106      buildToolsetsMenu(n, True)
    107
    108 -def buildToolsetsMenu(m, delete):
    109    ret = False
    110    for d in nuke.pluginPath():
    111      if delete and d.find(".nuke") == -1:
    112        continue
    113      if os.path.isdir(d):
    114        dircontents = os.listdir(d)
    115        if "ToolSets" in dircontents:
    116          fullPath = "/".join([d, "ToolSets"])
    117          if createToolsetFiles(m, fullPath, fullPath, delete):
    118            m.addCommand("-", "", "")
    119            ret = True
    120    return ret
    121
    122 -def createToolsetFiles(m, rootPath, fullPath, delete):
    123    filecontents = sorted(os.listdir(fullPath), key=str.lower)
    124    # First list all directories
    125    retval = False
    126    if filecontents != []:
    127      for group in filecontents:
    128        if os.path.isdir("/".join([fullPath, group])):
    129          n = m.addMenu(group)
    130          retval = createToolsetFiles(n, rootPath, "/".join([fullPath, group]), delete)
    131          # if we are deleting, and the sub directory is now empty, delete the directory also
    132          if delete and os.listdir(fullPath)==[]:
    133            os.rmdir(fullPath)
    134      # Now list individual files
    135      for group in filecontents:
    136        fullFileName = "/".join([fullPath, group])
    137        if not os.path.isdir(fullFileName):
    138          extPos = group.find(".nk")
    139          if extPos != -1 and extPos == len(group) - 3:
    140            group = group.replace('.nk', '')
    141            if delete:
    142              m.addCommand(group, 'nukescripts.toolsets.deleteToolset("%s", "%s")' % (rootPath, fullFileName), "")
    143              retval = True
    144            else:
    145              m.addCommand(group, 'nuke.loadToolset("%s")' % fullFileName, "")
    146              retval = True
    147    return retval
    148