简体   繁体   中英

How do I get python to reload this module in Maya 2015? The constructor only runs the first time

I'm having trouble (I believe) getting a module to reload after the first time it is run in Maya 2015 on OSX 10.10. It works after restarting the application but I need it to refresh every time the script is executed. Here is the code:

import os

try:
    riggingToolRoot = os.environ["RIGGING_TOOL_ROOT"]
except:
    print "RIGGING_TOOL_ROOT environment variable not correctly configured"
else:
    import sys 
    print riggingToolRoot
    path = riggingToolRoot + "/Modules"

    if not path in sys.path:
        sys.path.append(path)

        import System.blueprint_UI as blueprint_UI
        reload(blueprint_UI)

        UI = blueprint_UI.Blueprint_UI()

In the constructor of Blueprint_UI there is currently only a print statement and it only runs the first time the script is run after Maya has been restarted. It seems that the reload isn't working for some reason? The first time the output is:

/Users/eric/Documents/Projects/RiggingTool
We are in the constructor

From that point on each execution just gives the following until I quit Maya and restart:

/Users/eric/Documents/Projects/RiggingTool

I've tried using:

import sys
sys.dont_write_bytecode = True

to see if it was using .pyc files but that didn't make any difference. Thanks.

On the first execution of your code, your variable path is not yet in sys.path so, sys.path gets apprended with path , your module is imported and reloaded and your UI executed.

On the second execution of your program, path is already in sys.path your if condition is False and the code inside not executed (no reload, no call to your UI).

Here is a possible solution to your problem:

Note: Comments starting with #1: are for the first execution of your program while comments starting with #2: are for the subsequent executions.

import os

try:
    riggingToolRoot = os.environ["RIGGING_TOOL_ROOT"] #I guess this is the beginning of your path where blueprint is located
except:
    print "RIGGING_TOOL_ROOT environment variable not correctly configured"
else:
    import sys 

    print riggingToolRoot  #This will be printed in both cases
    path = riggingToolRoot + "/Modules"

    #1: path is not yet in sys.path, condition True
    #2: we previously addded path to sys.path and it will stay like this until we restart Maya, condition False. path will not be appended to sys.path a second time, this is useless
    if not path in sys.path:
        sys.path.append(path)


    if not "System.blueprint_UI" in sys.modules:
        #1: We never imported System.blueprint_UI 
        #1: Let's do it
        #1: Note that blueprint_UI is just a local variable, it is not stored anywhere in sys.modules 
        import System.blueprint_UI as blueprint_UI
    else:
        #2: When you import a module, it is located in sys.modules
        #2: Try printing it, your can see all the modules already imported (Maya has quite a lot) 
        #2: Anyway, the condition is False as the module has been imported previously
        reload(blueprint_UI)

    #In both case, the module is imported and updated, we can now run the UI function and print "We are in the constructor"
    UI = blueprint_UI.Blueprint_UI()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM