如何让 python 在 Maya 2015 中重新加载此模块?构造函数只运行第一次

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

我在 OSX 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 的构造函数中,目前只有一个打印语句,并且在 Maya 重新启动后,脚本第一次 运行 仅 运行s。似乎由于某种原因重新加载不起作用?第一次输出为:

/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 被追加 path,您的模块已导入并重新加载并执行了 UI。

第二次执行你的程序时,path已经在sys.path你的if条件为False并且里面的代码没有执行(没有重新加载,没有调用你的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()