暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

kellerPluginCore.py 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. import os
  2. import imp
  3. import sys
  4. import re
  5. import inspect
  6. pluginSubDir = "pythonplugins"
  7. gimmickSubDir = "gimmicks"
  8. baseDir = os.path.dirname(__file__)
  9. pluginBaseDir = os.path.join( baseDir ,pluginSubDir)
  10. import __builtin__
  11. import site
  12. import nuke
  13. import nukescripts
  14. class KellerNukePluginException(Exception):
  15. def __init__(self, *args, **kwargs):
  16. Exception.__init__(self, *args, **kwargs)
  17. class KellerNukePlugin():
  18. STATE_UNLOADED = 00
  19. STATE_ERROR = 05
  20. STATE_LOADED = 10
  21. STATE_INACTIVE = 20
  22. STATE_ACTIVE = 30
  23. _STATE_STR_ = { 00: "UNLOADED",
  24. 05: "ERROR",
  25. 10: "LOADED",
  26. 20: "INACTIVE",
  27. 30: "ACTIVE"
  28. }
  29. def __init__(self, module):
  30. pass
  31. self.module = module
  32. self.pluginName = self.__class__.__name__
  33. self.state = KellerNukePlugin.STATE_LOADED
  34. self.valid = True
  35. def setInvalid(self):
  36. self.valid = False
  37. self.state = KellerNukePlugin.STATE_UNLOADED
  38. def checkValid(self):
  39. if not self.valid:
  40. raise KellerNukePluginException("plugin unloaded. instance is no longer valid")
  41. def getName(self):
  42. return self.pluginName
  43. def getState(self):
  44. return self.state
  45. def getStateAsString(self):
  46. return KellerNukePlugin._STATE_STR_[self.getState()]
  47. def getModule(self):
  48. return self.module
  49. def activate(self):
  50. self.checkValid()
  51. if (self.state == KellerNukePlugin.STATE_INACTIVE) or (self.state == KellerNukePlugin.STATE_LOADED):
  52. self.configurePluginInternal()
  53. self.state = KellerNukePlugin.STATE_ACTIVE
  54. #else:
  55. # raise KellerNukePluginException("Plugin has wrong state for this operation")
  56. def deactivate(self):
  57. self.checkValid()
  58. if self.state == KellerNukePlugin.STATE_ACTIVE:
  59. try:
  60. self.unconfigurePluginInternal()
  61. except:
  62. # TODO, handle this
  63. logDebug("Error while unconfiguring plugin %s" % self.getName())
  64. self.state = KellerNukePlugin.STATE_INACTIVE
  65. #else:
  66. # raise KellerNukePluginException("Plugin has wrong state for this operation")
  67. def configurePluginInternal(self):
  68. pass
  69. logDebug("configure plugin %s" % self.pluginName)
  70. # configure folders
  71. baseDir = os.path.dirname(__file__)
  72. self.configurePlugin()
  73. def unconfigurePluginInternal(self):
  74. self.unconfigurePlugin()
  75. logDebug("unconfigure plugin %s" % self.getName())
  76. def unconfigurePlugin(self):
  77. pass
  78. def configurePlugin(self):
  79. pass
  80. def __str2__(self):
  81. return "%s > %s[%s]" % (super(KellerNukePlugin, self), self.getName(), self.getStateAsString())
  82. def logDebug(msg):
  83. import inspect, sys, os
  84. frm = inspect.stack()[1]
  85. mod = inspect.getmodule(frm[0])
  86. caller = mod.__name__
  87. #caller = os.path.basename(sys._getframe(0).f_back.f_code.co_filename)
  88. nuke.debug("[%s] %s" % (caller,msg))
  89. class KellerPluginManagerImpl():
  90. def __init__(self):
  91. pass
  92. self.kellerPluginPool = dict()
  93. self.serviceData = dict()
  94. def getPlugins(self):
  95. return self.kellerPluginPool.values()
  96. def getPluginByName(self, pluginName):
  97. if pluginName in self.kellerPluginPool:
  98. return self.kellerPluginPool[pluginName]
  99. return None
  100. def deactivatePlugins(self):
  101. # iterate over list COPY
  102. for plugin in self.kellerPlugins.values[:]:
  103. if plugin.getState() == KellerNukePlugin.STATE_ACTIVE:
  104. plugin.deactivate()
  105. def dumpPlugins(self):
  106. pattern = "{0:<25} {1:<10}\n"
  107. s = ""
  108. s = s + pattern.format("Name", "State")
  109. s = s + "---------------------------------------------------------------------------------\n"
  110. for plugin in self.kellerPluginPool.values():
  111. s = s + pattern.format(plugin.getName(), plugin.getStateAsString())
  112. def reloadPluginByName(self, pluginName):
  113. plugin = self.getPluginByName(pluginName)
  114. if plugin is None:
  115. logDebug("plugin %s not found " % pluginName)
  116. return
  117. return self.reloadPlugin(plugin)
  118. def reloadPlugin(self, plugin):
  119. pluginName = plugin.getName()
  120. if (plugin.getState() == KellerNukePlugin.STATE_ACTIVE):
  121. plugin.deactivate()
  122. if (plugin.getState() == KellerNukePlugin.STATE_INACTIVE):
  123. plugin.setInvalid()
  124. # remove from pool
  125. del self.kellerPluginPool[pluginName]
  126. reload(plugin.module)
  127. plugin = self.loadPluginModuleContents(plugin.module)
  128. return plugin
  129. # add to pool
  130. # add to plugin pool
  131. def loadPluginModuleContents(self, module):
  132. for (mname, mobj) in inspect.getmembers(module):
  133. #print mname
  134. #print mobj
  135. #print AbstractNukePlugin
  136. if inspect.ismodule(mobj):
  137. #nuke.debug("importing %s" % mname)
  138. #__import__(mname)
  139. __builtin__.__dict__[mname] = mobj
  140. if (inspect.isclass(mobj)) and (issubclass(mobj, KellerNukePlugin)):
  141. #nuke.debug(">>>> Found plugin definition for %s" % mname)
  142. minstance = mobj(module)
  143. # add to pool
  144. self.kellerPluginPool[minstance.getName()] = minstance
  145. minstance.activate()
  146. return minstance
  147. def setServiceData(self, name, service):
  148. self.serviceData[name] = service
  149. def getServiceData(self, name):
  150. if name in self.serviceData.keys():
  151. return self.serviceData[name]
  152. else:
  153. return None
  154. def removeServiceData(self, name):
  155. if name in self.serviceData.keys():
  156. del self.serviceData[name]
  157. def loadPluginDir(self, pluginDir, loadPluginInstance=False):
  158. if not os.path.isdir(pluginDir):
  159. return
  160. #logDebug("Adding plugin dir %s" % pluginDir)
  161. #site.addsitedir(pluginDir)
  162. # get python type descriptions
  163. type_descriptions = dict()
  164. for description in imp.get_suffixes():
  165. type_descriptions[description[0]] = description
  166. #print "basedir is %s " %pluginBaseDir
  167. #nuke.debug("basedir is %s" % pluginBaseDir)
  168. pythonFiles = os.listdir(pluginDir)
  169. for pyFile in pythonFiles:
  170. filename = os.path.basename(pyFile)
  171. #print "found " + filename
  172. match = re.match("(?P<prefix>.*)(?P<postfix>\..*)", filename)
  173. if match is not None:
  174. prefix = match.group("prefix")
  175. ext = match.group("postfix")
  176. if ext in [".py"]:
  177. fileObj = open(os.path.join(pluginDir,filename))
  178. module = imp.load_module(prefix, fileObj, os.path.join(pluginDir,filename), type_descriptions[ext])
  179. __builtin__.__dict__[module.__name__] = module
  180. if loadPluginInstance:
  181. self.loadPluginModuleContents(module)
  182. fileObj.close()
  183. # create PluginManager instance
  184. __builtin__.KellerPluginManager = KellerPluginManagerImpl()
  185. __builtin__.KellerNukePlugin = KellerNukePlugin
  186. __builtin__.KellerNukePluginException = KellerNukePluginException
  187. __builtin__.nuke = nuke
  188. __builtin__.nukescripts = nukescripts
  189. __builtin__.WORKGROUP_BASEDIR = baseDir
  190. __builtin__.logDebug = logDebug
  191. sys.dont_write_bytecode = True
  192. # allowed python suffixes for modules
  193. #suffixes = [str(suff[0]) for suff in imp.get_suffixes()]
  194. def addSiteDir(pluginDir):
  195. if not os.path.isdir(pluginDir):
  196. return
  197. #logDebug("Adding to site: %s" % pluginDir)
  198. if pluginDir not in sys.path:
  199. site.addsitedir(pluginDir)
  200. def loadPlugins(pluginBaseDir, loadPluginInstance = False):
  201. pluginDirList = os.listdir(pluginBaseDir)
  202. for pluginDir in pluginDirList:
  203. addSiteDir(os.path.join(pluginBaseDir,pluginDir))
  204. # process plugins
  205. for pluginDir in pluginDirList:
  206. KellerPluginManager.loadPluginDir(os.path.join(pluginBaseDir,pluginDir), loadPluginInstance)
  207. #def reloadPlugins():
  208. # unloadPlugins()
  209. # loadPlugins()
  210. def init(pluginBaseDir):
  211. # add all dirs to site and make them visible
  212. logDebug("Initializing Keller PluginCore...")
  213. # process and register all plugins, but to not activate
  214. loadPlugins(pluginBaseDir, loadPluginInstance=False)
  215. def start(pluginBaseDir):
  216. # process all plugins and activate instances
  217. loadPlugins(pluginBaseDir, loadPluginInstance=True)