andrewherd.me

Technical Artist

Maya reloading python modules

While working on a new tool inside of Maya, it gets very annoying to have to continually reload the python module or close out Maya and reload it every time you want to see a change to it.  At least I do. Especially when Maya is loaded down with all kinds of other tool bars/scripts/etc and many more things you’d find in a work environment.

Using reload on a module only works if everything is inside that one module (one file). But if there are sub modules, they won’t get reloaded. If you want to use a .ui gui file instead of converting it to a .py file every time you touch it, that won’t reload either.

What I end up doing is finding all the related modules in the sys.modules dictionary, and deleting them.

I’ll make a small script to do that for me and then call that when I need to reload my tool that I’m working on. This has worked for me for a quite a while and works with the ui files as well.

import sys
 
def deleteModules(modName):
    modsList = sys.modules
    for mod in modsList.keys():
        if mod.count(modName) > 0:
            del modsList[mod]

Now I can give it the base module name and it’ll find the other sub modules related to it and remove them.

At this point I can import the module again and it’ll actually import everything again without going to the cache first.

deleteModules('dustie')
import dustie as dst
dst.ui()

Things to watch out for would be modules named similarly to yours that you don’t want to remove. ‘library’ is probably a bad example.

Other places say this isn’t a great thing to do and you should always reload Maya. I’ve never had a problem doing this, but you may encounter something I haven’t. When Maya takes two minutes to load, I don’t want to reload very often, I’ll take my chances.

« Previous post