importing python

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  03 March 2013
Unhappy importing python

I was working on my autoRig and noticed that the code ranged to 1000 some lines and was wondering if it was possible to break it up into different python files. I would like to be able to import the python file and call the functions in that file.
  03 March 2013
you do it like with any other python program:

* organize your code into modules and packages
* add source path to maya's python path
* import your "main" function into maya and call it there.

More detail explanation could be very long, depending how much of the above you understand.

Debugging is a bit problematic with this, as once a module is imported, python will cache it, unless you specifically ask python to "forget" it. I tend to not rely on "reload()" function as it only reloads one imported module, but not it's dependencies, and that causes a lot of headaches. Instead, I ask python to "forget" the whole package, like this (excerpt from my ngSkinTools dev utils) -calling the below function "unload()". Any subsequent imports from that package will cause python to re-read source files.

DEFAULT_RELOAD_PACKAGES = ['ngSkinTools','ngSkinToolsTest']

def unload(silent=True,packages=None):
    performs unloading. 
        * silent flag specifies if utility should print the unloaded modules
        * packages: array of packages to unload. specify None to use 
          defaults (DEFAULT_RELOAD_PACKAGES variable)
    if packages is None:
        packages = DEFAULT_RELOAD_PACKAGES
    # construct reload list
    reloadList =[] 
    for i in sys.modules.keys():
        for package in packages:
            if i.startswith(package):

    # unload everything
    for i in reloadList:
            if sys.modules[i] is not None:
                if not silent:
                    print "unloaded "+i
        except Exception,err:
            #print "failed to unload "+i
Victor - skinning plugin for Autodesk Maya
  03 March 2013
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 10:15 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.