Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kellerGimmicks.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. import os
  2. PLUGIN_VERSION = "20140226"
  3. # subfolder for gimmicks
  4. GIMMICK_SUBDIR = "gimmicks"
  5. # gimmick file extension
  6. GIMMICK_EXT = ".gimmick"
  7. MENU_NAME = "Gimmick"
  8. SERVICE_GIMMICKS = "KellerGimmicksPlugin.gimmicks"
  9. #_MENU_TOKEN_SEPARATOR_ = "|"
  10. #_MENU_KNOB_NAME_ = "_kMenu"
  11. GIMMICK_BASEDIR = os.path.join(WORKGROUP_BASEDIR, GIMMICK_SUBDIR)
  12. GIMMICK_TEMPLATE_SUBDIR = "Templates"
  13. GIMMICK_GROUP_SUBDIR = "Gimmicks"
  14. class KellerGimmicksPlugin(KellerNukePlugin):
  15. def configurePlugin(self):
  16. # register menu entry
  17. self.menu = nuke.menu("Nuke")
  18. #m = menu.addMenu("&Edit")
  19. m = self.menu.addMenu(MENU_NAME)
  20. m.addCommand("Save from Node...", "kellerGimmicks.saveGimmick()")
  21. m.addCommand("Update Nodes", "kellerGimmicks.updateGimmicks()")
  22. #nuke.debug("basedir ist %s " % self.getBaseDir())
  23. KellerPluginManager.setServiceData(SERVICE_GIMMICKS, dict())
  24. registerGimmicks(GIMMICK_BASEDIR)
  25. def unconfigurePlugin(self):
  26. self.menu.removeItem(MENU_NAME)
  27. # remove gimmicks
  28. removeGimmicks(GIMMICK_BASEDIR)
  29. KellerPluginManager.removeServiceData(SERVICE_GIMMICKS)
  30. class KellerGimmick():
  31. pass
  32. def __init__(self, file=None):
  33. self._initFile(file)
  34. self._gimmickData = dict()
  35. def _initFile(self, file):
  36. import re
  37. print ("created gimmick %s" % file)
  38. self.menuLocation = list()
  39. self.file = None
  40. if file is not None:
  41. # init menu location from filename
  42. if GIMMICK_BASEDIR is not None:
  43. # check if valid directory
  44. if not os.path.isdir(GIMMICK_BASEDIR):
  45. # TODO, raise specific exception
  46. raise Exception
  47. gimmickPath = os.path.dirname(file)
  48. #if gimmickPath == GIMMICK_BASEDIR:
  49. # relPath = ""
  50. #else:
  51. relPath = os.path.relpath(gimmickPath, GIMMICK_BASEDIR)
  52. #nuke.debug("RELPATH: %s" %relPath)
  53. relPath = relPath.replace("\\","/")
  54. menuEntries = relPath.split("/")
  55. # remove . element
  56. if "." in menuEntries:
  57. menuEntries.remove(".")
  58. self.setMenuLocation(menuEntries)
  59. self.file = os.path.normpath(file).replace("\\","/")
  60. # get name from file
  61. m = re.match("(.*)\..*", os.path.basename(file))
  62. if m is not None:
  63. self.name = m.group(1)
  64. else:
  65. self.name=os.path.basename(file)
  66. else: # filename is none
  67. self.file = None
  68. self.name = "unknown"
  69. def getFile(self):
  70. return self.file
  71. def load(self, file):
  72. pass
  73. def setMenuLocation(self, menuLocation):
  74. self.menuLocation = menuLocation
  75. def addData(self,name, value):
  76. self._gimmickData[name] = value
  77. def _createDefaultGimmickData(self):
  78. import time
  79. self.addData("Creation Time", time.ctime())
  80. def _saveAs(self, file):
  81. try:
  82. nuke.nodeCopy(file)
  83. except:
  84. nuke.message("Error saving node to %s" % file)
  85. self._initFile(file)
  86. # append gimmick data
  87. self._createDefaultGimmickData()
  88. self._saveGimmickData()
  89. def setName(self, name):
  90. self.name = name
  91. def getName(self):
  92. return self.name
  93. def addMenuLocation(self, menuString):
  94. print ("added %s" % menuString)
  95. self.menuLocation.append(menuString)
  96. def removeFromMenu(self):
  97. print (self.menuEntry)
  98. if self.menuEntry is not None:
  99. print ("remove from menu, entry=%s" % str(self.menuEntry))
  100. (parent, commandName) = self.menuEntry
  101. parent.removeItem(commandName)
  102. def _registerMenu(self):
  103. #nuke.debug("register menulocation %s" % str(self.menuLocation))
  104. self._registerMenuInternal(self.menuLocation, -1, len(self.menuLocation))
  105. def _registerMenuInternal(self, entries, level, numLevels, parent=None):
  106. if level > numLevels:
  107. return
  108. if (level == -1):
  109. #
  110. # Root Menu
  111. #
  112. #nuke.debug( "creating root menu NODES ")
  113. parent = nuke.menu("Nodes")
  114. #print ("Menu %s" % entries[level])
  115. elif (level >= 0 ) and (level < numLevels):
  116. #
  117. # Sub menues
  118. #
  119. #nuke.debug( "submenu %s" % entries[level] )
  120. parent = parent.addMenu(entries[level],icon="gimmicks.png")
  121. elif (level == numLevels):
  122. #
  123. # register command
  124. #
  125. #nuke.debug( "register command ")
  126. parent.addCommand(self.name,"kellerGimmicks.createGimmick(\"%s\")" % self.file,icon="gimmicks.png")
  127. self.menuEntry = (parent, self.name)
  128. #else:
  129. # done
  130. # return
  131. # continue
  132. self._registerMenuInternal(entries, level+1, numLevels, parent)
  133. def getMenuLocation(self, menuString):
  134. self.menuLocation
  135. def pasteGimmick(self):
  136. pass
  137. def _saveGimmickData(self):
  138. #return
  139. with open(self.file, 'a+') as f:
  140. content = f.read()
  141. f.seek(0,0)
  142. print (self._gimmickData)
  143. for key in self._gimmickData.keys():
  144. f.write("# %s=%s\n" % (key, self._gimmickData[key]))
  145. f.close()
  146. @staticmethod
  147. def loadFromFile(file):
  148. file = os.path.normpath(file)
  149. gimmick = KellerGimmick(file)
  150. #f = open(file,'r')
  151. #script = f.read()
  152. #f.close()
  153. #import re
  154. #for line in script.split("\n"):
  155. # tokens = line.strip().split()
  156. #if ( (len(tokens)>0) and (tokens[0].startswith(_MENU_KNOB_NAME_)) ):
  157. # gimmick.addMenuLocation(tokens[1])
  158. #if ( (len(tokens)>0) and (tokens[0].startswith("name")) ):
  159. # gimmick.setName(tokens[1])
  160. #gimmick.registerMenus()
  161. #match = re.findall("_kMenu\d*\s*(?P<menu>[a-zA-Z0-9_|]*)\s*", script)
  162. return gimmick
  163. #def createGimmick(file):
  164. # nuke.nodePaste(file)
  165. def updateGimmicks():
  166. removeGimmicks(GIMMICK_BASEDIR)
  167. registerGimmicks(GIMMICK_BASEDIR)
  168. def saveGimmick():
  169. selection = nuke.selectedNodes();
  170. if len(selection) == 0:
  171. nuke.message("No node selected")
  172. return
  173. defaultName = "customTemplate" + GIMMICK_EXT
  174. folderProposal = GIMMICK_BASEDIR
  175. if len(selection) == 1:
  176. n = selection[0]
  177. if isinstance(n, nuke.Group):
  178. # use name of group by default
  179. defaultName = n.name() + GIMMICK_EXT
  180. #nuke.message("Node must be a group type")
  181. folderProposal = os.path.join(GIMMICK_BASEDIR, GIMMICK_GROUP_SUBDIR)
  182. else:
  183. folderProposal = os.path.join(GIMMICK_BASEDIR, GIMMICK_TEMPLATE_SUBDIR)
  184. saveFile = nuke.getFilename("Save...", pattern="*.gimmick", default=os.path.join(folderProposal, defaultName), type="save")
  185. if saveFile is None:
  186. return
  187. if not saveFile.endswith(".gimmick"):
  188. saveFile = saveFile + ".gimmick"
  189. if os.path.isdir(saveFile):
  190. # is already a dir, append the filename
  191. saveFile = os.path.join(saveFile, defaultName)
  192. else:
  193. saveFile = saveFile
  194. gimmick = KellerGimmick()
  195. creatorName = nuke.getInput("Enter your name:","")
  196. if creatorName:
  197. gimmick.addData("Creator", creatorName)
  198. if os.path.exists(saveFile):
  199. if not nuke.ask("File exists, overwrite?"):
  200. return
  201. backupFile(saveFile)
  202. gimmick._saveAs(saveFile)
  203. gimmick._registerMenu()
  204. # register new gimmick
  205. # TODO
  206. def backupFile(file):
  207. import shutil
  208. try:
  209. shutil.copy(file, file+".bak")
  210. except:
  211. nuke.debug("Cannot create backup of gimmick %s" % file)
  212. def removeGimmicks(folder):
  213. # TODO: retrieve list of gimmicks and remove from menu
  214. gimmicks = KellerPluginManager.getServiceData(SERVICE_GIMMICKS)
  215. for gimmick in gimmicks.values():
  216. print ("removing %s" % gimmick)
  217. gimmick.removeFromMenu()
  218. KellerPluginManager.setServiceData(SERVICE_GIMMICKS, dict())
  219. def createGimmick(file):
  220. nuke.nodePaste(file)
  221. def registerGimmicks(folder, menu=None):
  222. if (menu is None):
  223. menu = list()
  224. #menu = nuke.menu("Nodes")
  225. files = os.listdir(folder)
  226. for file in files:
  227. absfile = os.path.join(folder, file)
  228. if file.endswith(GIMMICK_EXT):
  229. # register command
  230. gimmick = KellerGimmick.loadFromFile(absfile)
  231. gimmick._registerMenu()
  232. gimmicks = KellerPluginManager.getServiceData(SERVICE_GIMMICKS)
  233. gimmicks[gimmick.getName()] = gimmick
  234. #menu.addCommand(gimmick.getName(),"kellerGimmicks.createGimmick(\"%s\")" % gimmick.getFile())
  235. # create gimmick
  236. #kGimmick = KellerGimmick.parseFromFile(os.path.join(folder,file))
  237. if os.path.isdir(absfile):
  238. # create menu
  239. #menu = menu.addMenu(file)
  240. menu.append(file)
  241. # recurse
  242. registerGimmicks(absfile, menu=menu,)
  243. if __name__ == "__main__":
  244. #print ("start")
  245. registerGimmicks("p:/env/nuke/_workgroup/_Projects/temp_schnelli/gimmicks")