[cgkit-commits] maya/maya_wrapper/maya pluginmanager.py,NONE,1.1 pluginmanagergui.py,NONE,1.1
Brought to you by:
mbaas
From: Matthias B. <mb...@us...> - 2006-04-05 07:51:59
|
Update of /cvsroot/cgkit/maya/maya_wrapper/maya In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26369 Added Files: pluginmanager.py pluginmanagergui.py Log Message: Added code to manage Python plugins --- NEW FILE: pluginmanagergui.py --- # Python plugin manager window import os.path, glob from maya.GUI import * from maya import pluginmanager from maya import mel import maya._mel # PyPluginManagerWindow class PyPluginManagerWindow(Window): """Python plugin manager window. """ def __init__(self): """Constructor. """ Window.__init__(self, title="Python Plug-In Manager", menuBar=False) self.buildUI() def onBrowse(self): filename = mel.fileDialog(directoryMask="*.py") if filename=="": return pluginmanager.loadPyPlugin(filename) self.onRefresh() def onRefresh(self): mel.deleteUI(str(self._toplayout)) self.buildUI() def onClose(self): self.visible = False def onLoadPlugin(self, filename): print "Loading plugin ", filename pluginmanager.loadPyPlugin(filename) def onUnloadPlugin(self, filename): plugin = pluginmanager._pluginName(filename) print "Unloading plugin ", plugin pluginmanager.unloadPyPlugin(plugin) # buildUI def buildUI(self): """Create the UI. """ # toplayout = FormLayout() toplayout = mel.formLayout() self._toplayout = toplayout scroll = ScrollLayout(parent=toplayout, horizontalScrollBarThickness=0, childResizable=True) self._scrolllayout = scroll self._frameparent = ColumnLayout(parent = self._scrolllayout, adjustableColumn = True, columnAttach = ("both",0), visible = False) paths = pluginmanager._pluginPaths() # Create the plugin sections... all_plugins = pluginmanager.registeredPyPlugins() for path in paths: files = glob.glob(os.path.join(path, "*.py")) files = filter(lambda f: pluginmanager._isPyPlugin(f), files) for file in files: pluginname = pluginmanager._pluginName(file) pinfo = pluginmanager.pyPluginInfo(pluginname) if pinfo!=None and file==pinfo.filename: all_plugins.remove(pluginname) self.addPluginSection(path, files) # Add all registed plugins that are not located in the standard # plugin paths... remaining = map(lambda p: pluginmanager.pyPluginInfo(p).filename, all_plugins) self.addPluginSection("Other Registed Plugins", remaining) self._frameparent.visible = True # buttons = RowLayout(parent=toplayout, numberOfColumns=3) buttons = mel.formLayout(parent=toplayout) b1 = Button(label="Browse", command=self.onBrowse) b2 = Button(label="Refresh", command=self.onRefresh) b3 = Button(label="Close", command=self.onClose) mel.formLayout(buttons, edit=None, attachForm=[(str(b1), "left", 4), (str(b1), "top", 4), (str(b1), "bottom", 4)], attachPosition=[(str(b1), "right", 2, 33)]) mel.formLayout(buttons, edit=None, attachControl=[(str(b2), "left", 2, str(b1)), (str(b2), "right", 2, str(b3))], attachForm=[(str(b2), "top", 4), (str(b2), "bottom", 4)]) mel.formLayout(buttons, edit=None, attachPosition=[(str(b3), "left", 2, 66)], attachForm=[(str(b3), "right", 4), (str(b3), "top", 4), (str(b3), "bottom", 4)]) mel.formLayout(toplayout, edit=None, attachForm=[(str(scroll), "top", 0), (str(scroll), "left", 0), (str(scroll), "right", 0)], attachControl=[(str(scroll), "bottom", 0, str(buttons))]) mel.formLayout(toplayout, edit=None, attachForm=[(str(buttons), "bottom", 0), (str(buttons), "left", 0), (str(buttons), "right", 0)]) # addPluginSection def addPluginSection(self, name, files): """Add one plugin section (one FrameLayout). name is the name of the section and files a list of files that should appear in that section. """ FrameLayout(parent = self._frameparent, label = name, borderStyle = "etchedOut", collapsable = True, font = "boldLabelFont") col = ColumnLayout(adjustableColumn=True) for file in files: pluginname = pluginmanager._pluginName(file) pinfo = pluginmanager.pyPluginInfo(pluginname) if pinfo==None: loaded_flag = False else: loaded_flag = pinfo.isLoaded() r = RowLayout(parent=col, numberOfColumns=3)#, columnAttach=[(1,"left",4), (2,"left",0), (3,"left",0)]) mel.rowLayout(str(r), edit=None, columnAttach=[(1,"left",8), (2,"left",0), (3,"left",0)]) mel.rowLayout(str(r), edit=None, rowAttach=[(1,"both",1)]) mel.text(label=os.path.basename(file)) CheckBox(label = "loaded", value = loaded_flag, onCommand = (self.onLoadPlugin,file), offCommand = (self.onUnloadPlugin,file)) CheckBox(label="auto load", enable=False) def PyPluginManager(): """Open the Python plugin manager. """ win = PyPluginManagerWindow() win.show() --- NEW FILE: pluginmanager.py --- ########################################################################### # cgkit - Python Computer Graphics Kit # Copyright (C) 2006 Matthias Baas (ba...@ir...) # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # cgkit homepage: http://cgkit.sourceforge.net ########################################################################### """Manage Python plugins. """ import sys, os, os.path, types import maya # Key: plugin name / Value: PluginInfo-Object _plugin_registry = {} # PluginInfo class PluginInfo: """Plugin information. """ def __init__(self, filename, pluginname, initfunc, uninitfunc, initstatus): self._filename = filename self._pluginname = pluginname self._initfunc = initfunc self._uninitfunc = uninitfunc self._initstatus = initstatus # isLoaded def isLoaded(self): """Return True if the plugin has been successfully loaded. """ return bool(self._initstatus) def _getFilename(self): return self._filename filename = property(_getFilename) def _getPluginname(self): return self._pluginname pluginname = property(_getPluginname) def _getInitStatus(self): return self._initstatus initstatus = property(_getInitStatus) # loadPyPlugin def loadPyPlugin(filenames): """Load Python plug-ins into Maya. """ if isinstance(filenames, types.StringTypes): filenames = [filenames] for filename in filenames: if os.path.splitext(filename)[1]=="": filename += ".py" fullfilename = _findPluginFile(filename) if fullfilename==None: raise ValueError, 'Plugin "%s" not found.'%filename _loadSinglePyPlugin(fullfilename) # unloadPyPlugin def unloadPyPlugin(pluginnames): """Unload Python plug-ins from Maya. pluginnames is a list of plugin names or a single plugin name. """ global _plugin_registry if isinstance(pluginnames, types.StringTypes): pluginnames = [pluginnames] for pluginname in pluginnames: if _plugin_registry.has_key(pluginname): # Call the uninit function and remove the plugin pinfo = _plugin_registry[pluginname] if pinfo.initstatus: pinfo._uninitfunc(maya.pluginObject()) del _plugin_registry[pluginname] else: print 'Warning: Python plugin "%s" not registered'%pluginname # pyPluginInfo def pyPluginInfo(pluginname): """Return a PluginInfo object or None if the plugin does not exist. """ global _plugin_registry info = _plugin_registry.get(pluginname) return info # registeredPyPlugins def registeredPyPlugins(): """Return a list with the plugin names of all registered Python plugins. """ global _plugin_registry return _plugin_registry.keys() # _loadSinglePyPlugin def _loadSinglePyPlugin(filename): """Load a Python plug-in into Maya. filename must be the absolute plugin file name. """ global _plugin_registry if not _isPyPlugin(filename): raise ValueError, 'File "%s" is no Python Maya plugin.'%filename # Determine the plugin name (base filename without extension) pluginname = _pluginName(filename) # Unload the plugin if it was already loaded... if _plugin_registry.has_key(pluginname): unloadPyPlugin(pluginname) # Execute the Python file... global_ns = {} execfile(filename, global_ns) # Obtain the initialization/uninitialization functions... init = global_ns.get("initializePlugin") uninit = global_ns.get("uninitializePlugin") if init==None or uninit==None: raise RuntimeError, 'Plugin "%s": init/uninit functions not found'%pluginname # Call the init function... stat = init(maya.pluginObject()) # Create and initialize a PluginInfo object pinfo = PluginInfo(filename=filename, pluginname=pluginname, initfunc=init, uninitfunc=uninit, initstatus=stat) # Store the info object... _plugin_registry[pluginname] = pinfo # _isPyPlugin def _isPyPlugin(filename): """Check if a file is a Python Maya plugin. A Python file is a Python Maya plugin when it contains the two functions initializePlugin() and uninitializePlugin(). """ flags = 0 for line in file(filename, "rt"): if line[:20]=="def initializePlugin": flags |= 0x01 if flags==0x03: return True elif line[:22]=="def uninitializePlugin": flags |= 0x02 if flags==0x03: return True return False # _pluginName def _pluginName(filename): """Return the plugin name for a given filename. """ return os.path.splitext(os.path.basename(filename))[0] # _findPluginFile def _findPluginFile(filename): """Search for a file in the plugin paths. filename is the (partial) file name of the plugin. The return value is the absolute filename if the file exists or None if the file was not found. """ if os.path.isabs(filename): if os.path.isfile(filename): return filename else: return None paths = _pluginPaths() for path in paths: fn = os.path.join(os.path.normpath(path), filename) if os.path.isfile(fn): return fn return None # _pluginPaths def _pluginPaths(): """Return a list of plugin paths. """ paths = os.getenv("MAYA_PLUG_IN_PATH") if paths==None: return [] if sys.platform=="win32": paths = paths.split(";") else: paths = paths.split(":") paths = filter(lambda p: p!="", paths) return paths |