Skip to content

Instantly share code, notes, and snippets.

@theodox
Last active January 3, 2016 02:29
Show Gist options
  • Save theodox/8395842 to your computer and use it in GitHub Desktop.
Save theodox/8395842 to your computer and use it in GitHub Desktop.
Maya tools shim. add all the zip files in the current directory to the python path. Modules will be loaded in alphabetical order. Once all zips are added to the path, each zip file which contains module with the same name (CASE SENSITIVE) itself will automatically be imported using "import <zipnname>". All side-effects will happen inside a maya …
'''
add all the zip files in the current directory to the python path.
Modules will be loaded in alphabetical order. Once all zips are added to the path,
each zip file which contains module with the same name (CASE SENSITIVE) itself will
automatically be imported using "import <zipnname>". All side-effects will happen
inside a maya executeDeferred call (so the maya environment should be fully loaded).
It's a good idea to be very sparing in the use of auto-loading modules!
(c) 2014 Steve Theodore All rights Reserved
'''
import site
import os
import zipimport
import sys
import traceback
import maya.utils
def _load_zips():
this_dir = os.path.dirname(__file__)
neighbors = os.listdir(this_dir)
neighbors.sort() # alpha order provides predictability
zips = []
for eachfile in neighbors:
fullpath = os.path.join(this_dir, eachfile)
if eachfile.lower()[-3:] == "zip":
site.addsitedir(fullpath)
zips.append( (fullpath, eachfile) )
for eachpath, eachzip in zips:
print >> sys.__stdout__, "-" * 100
zipper = zipimport.zipimporter(eachpath )
try:
zipmod = zipper.load_module(eachzip[:-4])
if zipmod: print >> sys.__stdout__, "loaded %s" % zipmod
except:
print >> sys.__stdout__, traceback.format_exc(0)
print >> sys.__stdout__, "-" * 100
# the oddball print syntax forces the display to the output window
maya.utils.executeDeferred(_load_zips)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment