123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- import os
- import imp
- import sys
- import re
- import inspect
-
- pluginSubDir = "pythonplugins"
- gimmickSubDir = "gimmicks"
-
- baseDir = os.path.dirname(__file__)
- pluginBaseDir = os.path.join( baseDir ,pluginSubDir)
- import __builtin__
- import site
- import nuke
- import nukescripts
-
-
- class KellerNukePluginException(Exception):
- def __init__(self, *args, **kwargs):
- Exception.__init__(self, *args, **kwargs)
-
- class KellerNukePlugin():
-
- STATE_UNLOADED = 00
- STATE_ERROR = 05
- STATE_LOADED = 10
- STATE_INACTIVE = 20
- STATE_ACTIVE = 30
-
- _STATE_STR_ = { 00: "UNLOADED",
- 05: "ERROR",
- 10: "LOADED",
- 20: "INACTIVE",
- 30: "ACTIVE"
- }
-
-
- def __init__(self, module):
- pass
- self.module = module
- self.pluginName = self.__class__.__name__
- self.state = KellerNukePlugin.STATE_LOADED
- self.valid = True
-
- def setInvalid(self):
- self.valid = False
- self.state = KellerNukePlugin.STATE_UNLOADED
-
- def checkValid(self):
- if not self.valid:
- raise KellerNukePluginException("plugin unloaded. instance is no longer valid")
-
- def getName(self):
- return self.pluginName
-
- def getState(self):
- return self.state
-
- def getStateAsString(self):
- return KellerNukePlugin._STATE_STR_[self.getState()]
-
- def getModule(self):
- return self.module
-
-
- def activate(self):
- self.checkValid()
-
- if (self.state == KellerNukePlugin.STATE_INACTIVE) or (self.state == KellerNukePlugin.STATE_LOADED):
- self.configurePluginInternal()
- self.state = KellerNukePlugin.STATE_ACTIVE
-
-
- #else:
- # raise KellerNukePluginException("Plugin has wrong state for this operation")
-
- def deactivate(self):
- self.checkValid()
- if self.state == KellerNukePlugin.STATE_ACTIVE:
- try:
- self.unconfigurePluginInternal()
- except:
- # TODO, handle this
- logDebug("Error while unconfiguring plugin %s" % self.getName())
- self.state = KellerNukePlugin.STATE_INACTIVE
- #else:
- # raise KellerNukePluginException("Plugin has wrong state for this operation")
-
-
-
-
-
- def configurePluginInternal(self):
- pass
-
- logDebug("configure plugin %s" % self.pluginName)
-
- # configure folders
- baseDir = os.path.dirname(__file__)
-
-
- self.configurePlugin()
-
- def unconfigurePluginInternal(self):
- self.unconfigurePlugin()
- logDebug("unconfigure plugin %s" % self.getName())
-
-
- def unconfigurePlugin(self):
- pass
-
- def configurePlugin(self):
- pass
-
- def __str2__(self):
- return "%s > %s[%s]" % (super(KellerNukePlugin, self), self.getName(), self.getStateAsString())
-
-
- def logDebug(msg):
- import inspect, sys, os
-
- frm = inspect.stack()[1]
- mod = inspect.getmodule(frm[0])
- caller = mod.__name__
- #caller = os.path.basename(sys._getframe(0).f_back.f_code.co_filename)
-
- nuke.debug("[%s] %s" % (caller,msg))
-
-
- class KellerPluginManagerImpl():
- def __init__(self):
- pass
- self.kellerPluginPool = dict()
- self.serviceData = dict()
-
-
- def getPlugins(self):
- return self.kellerPluginPool.values()
-
- def getPluginByName(self, pluginName):
-
- if pluginName in self.kellerPluginPool:
- return self.kellerPluginPool[pluginName]
- return None
-
- def deactivatePlugins(self):
-
- # iterate over list COPY
- for plugin in self.kellerPlugins.values[:]:
- if plugin.getState() == KellerNukePlugin.STATE_ACTIVE:
- plugin.deactivate()
-
- def dumpPlugins(self):
-
-
- pattern = "{0:<25} {1:<10}\n"
-
- s = ""
- s = s + pattern.format("Name", "State")
- s = s + "---------------------------------------------------------------------------------\n"
-
-
- for plugin in self.kellerPluginPool.values():
- s = s + pattern.format(plugin.getName(), plugin.getStateAsString())
-
-
- def reloadPluginByName(self, pluginName):
-
- plugin = self.getPluginByName(pluginName)
-
- if plugin is None:
- logDebug("plugin %s not found " % pluginName)
- return
-
-
- return self.reloadPlugin(plugin)
-
- def reloadPlugin(self, plugin):
-
- pluginName = plugin.getName()
-
-
- if (plugin.getState() == KellerNukePlugin.STATE_ACTIVE):
- plugin.deactivate()
-
-
-
- if (plugin.getState() == KellerNukePlugin.STATE_INACTIVE):
-
- plugin.setInvalid()
-
- # remove from pool
- del self.kellerPluginPool[pluginName]
-
- reload(plugin.module)
-
- plugin = self.loadPluginModuleContents(plugin.module)
-
- return plugin
- # add to pool
-
- # add to plugin pool
-
-
-
- def loadPluginModuleContents(self, module):
-
-
- for (mname, mobj) in inspect.getmembers(module):
- #print mname
- #print mobj
- #print AbstractNukePlugin
- if inspect.ismodule(mobj):
- #nuke.debug("importing %s" % mname)
- #__import__(mname)
- __builtin__.__dict__[mname] = mobj
-
- if (inspect.isclass(mobj)) and (issubclass(mobj, KellerNukePlugin)):
- #nuke.debug(">>>> Found plugin definition for %s" % mname)
- minstance = mobj(module)
-
- # add to pool
- self.kellerPluginPool[minstance.getName()] = minstance
-
-
- minstance.activate()
- return minstance
-
-
- def setServiceData(self, name, service):
- self.serviceData[name] = service
-
- def getServiceData(self, name):
- if name in self.serviceData.keys():
- return self.serviceData[name]
- else:
- return None
-
- def removeServiceData(self, name):
- if name in self.serviceData.keys():
- del self.serviceData[name]
-
- def loadPluginDir(self, pluginDir, loadPluginInstance=False):
- if not os.path.isdir(pluginDir):
- return
- #logDebug("Adding plugin dir %s" % pluginDir)
- #site.addsitedir(pluginDir)
-
- # get python type descriptions
- type_descriptions = dict()
- for description in imp.get_suffixes():
- type_descriptions[description[0]] = description
-
-
- #print "basedir is %s " %pluginBaseDir
- #nuke.debug("basedir is %s" % pluginBaseDir)
- pythonFiles = os.listdir(pluginDir)
-
- for pyFile in pythonFiles:
- filename = os.path.basename(pyFile)
- #print "found " + filename
-
- match = re.match("(?P<prefix>.*)(?P<postfix>\..*)", filename)
-
- if match is not None:
- prefix = match.group("prefix")
- ext = match.group("postfix")
-
-
- if ext in [".py"]:
-
- fileObj = open(os.path.join(pluginDir,filename))
-
- module = imp.load_module(prefix, fileObj, os.path.join(pluginDir,filename), type_descriptions[ext])
- __builtin__.__dict__[module.__name__] = module
-
-
-
- if loadPluginInstance:
- self.loadPluginModuleContents(module)
-
- fileObj.close()
-
-
-
-
-
- # create PluginManager instance
- __builtin__.KellerPluginManager = KellerPluginManagerImpl()
-
- __builtin__.KellerNukePlugin = KellerNukePlugin
- __builtin__.KellerNukePluginException = KellerNukePluginException
-
- __builtin__.nuke = nuke
- __builtin__.nukescripts = nukescripts
- __builtin__.WORKGROUP_BASEDIR = baseDir
- __builtin__.logDebug = logDebug
-
- sys.dont_write_bytecode = True
- # allowed python suffixes for modules
- #suffixes = [str(suff[0]) for suff in imp.get_suffixes()]
-
-
-
- def addSiteDir(pluginDir):
- if not os.path.isdir(pluginDir):
- return
- #logDebug("Adding to site: %s" % pluginDir)
- if pluginDir not in sys.path:
- site.addsitedir(pluginDir)
-
-
-
-
- def loadPlugins(pluginBaseDir, loadPluginInstance = False):
-
- pluginDirList = os.listdir(pluginBaseDir)
-
- for pluginDir in pluginDirList:
- addSiteDir(os.path.join(pluginBaseDir,pluginDir))
- # process plugins
- for pluginDir in pluginDirList:
- KellerPluginManager.loadPluginDir(os.path.join(pluginBaseDir,pluginDir), loadPluginInstance)
-
-
-
- #def reloadPlugins():
- # unloadPlugins()
- # loadPlugins()
- def init(pluginBaseDir):
- # add all dirs to site and make them visible
-
- logDebug("Initializing Keller PluginCore...")
-
- # process and register all plugins, but to not activate
- loadPlugins(pluginBaseDir, loadPluginInstance=False)
-
- def start(pluginBaseDir):
- # process all plugins and activate instances
- loadPlugins(pluginBaseDir, loadPluginInstance=True)
|