我遇到麻烦(我相信)在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的构造函数中,当前只有一个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文件,但这没有任何区别。感谢。
答案 0 :(得分:1)
在您的代码首次执行时,您的变量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()