繁体   English   中英

如何在Maya 2015中获取Python重新加载此模块? 构造函数仅在第一次运行

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

我在模块首次在OS X 10.10的Maya 2015中运行后无法重装模块(我相信)。 重新启动应用程序后它可以工作,但是每次执行脚本时我都需要刷新它。 这是代码:

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()

在Blueprint_UI的构造函数中,当前只有一条print语句,并且该语句仅在Maya重新启动后第一次运行脚本时运行。 似乎重新加载由于某种原因无法正常工作? 第一次输出是:

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

从这一点开始,每次执行仅给出以下内容,直到我退出Maya并重新启动:

/Users/eric/Documents/Projects/RiggingTool

我试过使用:

import sys
sys.dont_write_bytecode = True

看看是否使用的是.pyc文件,但这没有任何区别。 谢谢。

在你的代码的第一次执行,你的变量path尚未在sys.path如此, sys.path获取与apprended path ,你的模块导入和重载和您的用户界面执行。

在程序的第二次执行中, if条件为False且path不在sys.path并且内部代码未执行(不重新加载,不调用UI)。

这是您的问题的可能解决方案:

注意:#1:开头的注释用于程序的第一次执行,而以#2:开头的注释用于后续的执行。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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