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 populateToolsetsMenu(m, False): 104 m.addCommand("-", "", "") 105 n = m.addMenu("Delete", "ToolsetDelete.png") 106 populateToolsetsMenu(n, True)
107
108 -def traversePluginPaths(m, delete, allToolsetsList, isLocal):
109 ret = False 110 if delete and (not isLocal): 111 return True 112 excludePaths = nuke.getToolsetExcludePaths() 113 for d in nuke.pluginPath(): 114 d = d.replace('\\', '/') 115 ignore = False 116 for i in excludePaths: 117 i = i.replace('\\', '/') 118 if d.find(i) != -1: 119 ignore = True 120 break 121 if ignore: 122 continue 123 if (not isLocal) and (d.find(".nuke") != -1): 124 continue 125 if isLocal and (d.find(".nuke") == -1): 126 continue 127 if os.path.isdir(d): 128 dircontents = os.listdir(d) 129 if "ToolSets" in dircontents: 130 fullPath = "/".join([d, "ToolSets"]) 131 if createToolsetMenuItems(m, fullPath, fullPath, delete, allToolsetsList, isLocal): 132 ret = True 133 return ret
134
135 -def populateToolsetsMenu(m, delete):
136 ret = False 137 allToolsetsList = [] 138 # first build the menu for toolsets in shared locations 139 if traversePluginPaths(m, delete, allToolsetsList, False): 140 m.addCommand("-", "", "") 141 ret = True 142 # now do toolsets in the local .nuke 143 if traversePluginPaths(m, delete, allToolsetsList, True): 144 ret = True 145 return ret
146
147 -def createToolsetMenuItems(m, rootPath, fullPath, delete, allToolsetsList, isLocal):
148 filecontents = sorted(os.listdir(fullPath), key=str.lower) 149 excludePaths = nuke.getToolsetExcludePaths() 150 # First list all directories 151 retval = False 152 if filecontents != []: 153 for group in filecontents: 154 newPath = "/".join([fullPath, group]) 155 ignore = False 156 if newPath.find(".svn") != -1: 157 ignore = True 158 else: 159 for i in excludePaths: 160 i = i.replace('\\', '/') 161 if newPath.find(i) != -1: 162 ignore = True 163 break 164 if os.path.isdir(newPath) and not ignore: 165 menuName = group 166 if isLocal and (menuName in allToolsetsList): 167 menuName = "[user] " + menuName 168 elif not isLocal: 169 allToolsetsList.append(menuName) 170 n = m.addMenu(menuName) 171 retval = createToolsetMenuItems(n, rootPath, "/".join([fullPath, group]), delete, allToolsetsList, isLocal) 172 # if we are deleting, and the sub directory is now empty, delete the directory also 173 if delete and os.listdir(fullPath)==[]: 174 os.rmdir(fullPath) 175 # Now list individual files 176 for group in filecontents: 177 fullFileName = "/".join([fullPath, group]) 178 if not os.path.isdir(fullFileName): 179 extPos = group.find(".nk") 180 if extPos != -1 and extPos == len(group) - 3: 181 group = group.replace('.nk', '') 182 if delete: 183 m.addCommand(group, 'nukescripts.toolsets.deleteToolset("%s", "%s")' % (rootPath, fullFileName), "") 184 retval = True 185 else: 186 # get the filename below toolsets 187 i = fullFileName.find("ToolSets/") 188 if i != -1: 189 subfilename = fullFileName[i:] 190 else: 191 # should never happen, but just in case ... 192 subfilename = fullfilename 193 if isLocal and (subfilename in allToolsetsList): 194 # if we've already appended [user] to the menu name, don't need it on the filename 195 if (i != -1) and subfilename[len("ToolSets/"):].find("/") == -1: 196 group = "[user] " + group 197 elif not isLocal: 198 allToolsetsList.append(subfilename) 199 m.addCommand(group, 'nuke.loadToolset("%s")' % fullFileName, "") 200 retval = True 201 return retval
202