将自定义 python 脚本导入 Maya
import a custom python script to maya
我目前正在尝试将自定义脚本导入 Maya。我找到了这个 example。我的目标是能够在外部编辑脚本,每次点击时重新加载它。
我在 python 控制台中尝试了以下脚本,它似乎有效。不幸的是,当我在 Maya 中单击自定义按钮时出现一些错误。
所以这是我在 maya 中自定义按钮的脚本
import sys
import os
def psource(module):
file = os.path.basename( module )
dir = os.path.dirname( module )
toks = file.split( '.' )
modname = toks[0]
# Check if dirrectory is really a directory
if( os.path.exists( dir ) ):
# Check if the file directory already exists in the sys.path array
paths = sys.path
pathfound = 0
for path in paths:
if(dir == path):
pathfound = 1
# If the dirrectory is not part of sys.path add it
if not pathfound:
sys.path.append( dir )
# exec works like MEL's eval but you need to add in globals()
# at the end to make sure the file is imported into the global
# namespace else it will only be in the scope of this function
exec ('import ' + modname) in globals()
# reload the file to make sure its up to date
exec( 'reload( ' + modname + ' )' ) in globals()
# This returns the namespace of the file imported
return modname
# When you import a file you must give it the full path
psource( '/The/correct/path/to/my/script/import_test_model.py' )
import_test_model.main()
虽然这是我的自定义脚本
def main():
print "funziona"
return
if __name__ == "__main__":
main()
这是我收到的错误消息
...
# When you import a file you must give it the full path
psource( '/Users/rostyslavkostyuk/Documents/developing/py_maya/import_test_model.py' )
import_test_model.main()
# Error: SyntaxError: file <string> line 1: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
我不知道出了什么问题,但为了解决这个问题,我只是删除了旧按钮,并创建了新按钮,它开始起作用了。
我目前正在尝试将自定义脚本导入 Maya。我找到了这个 example。我的目标是能够在外部编辑脚本,每次点击时重新加载它。
我在 python 控制台中尝试了以下脚本,它似乎有效。不幸的是,当我在 Maya 中单击自定义按钮时出现一些错误。
所以这是我在 maya 中自定义按钮的脚本
import sys
import os
def psource(module):
file = os.path.basename( module )
dir = os.path.dirname( module )
toks = file.split( '.' )
modname = toks[0]
# Check if dirrectory is really a directory
if( os.path.exists( dir ) ):
# Check if the file directory already exists in the sys.path array
paths = sys.path
pathfound = 0
for path in paths:
if(dir == path):
pathfound = 1
# If the dirrectory is not part of sys.path add it
if not pathfound:
sys.path.append( dir )
# exec works like MEL's eval but you need to add in globals()
# at the end to make sure the file is imported into the global
# namespace else it will only be in the scope of this function
exec ('import ' + modname) in globals()
# reload the file to make sure its up to date
exec( 'reload( ' + modname + ' )' ) in globals()
# This returns the namespace of the file imported
return modname
# When you import a file you must give it the full path
psource( '/The/correct/path/to/my/script/import_test_model.py' )
import_test_model.main()
虽然这是我的自定义脚本
def main():
print "funziona"
return
if __name__ == "__main__":
main()
这是我收到的错误消息
...
# When you import a file you must give it the full path
psource( '/Users/rostyslavkostyuk/Documents/developing/py_maya/import_test_model.py' )
import_test_model.main()
# Error: SyntaxError: file <string> line 1: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
# Error: invalid syntax #
我不知道出了什么问题,但为了解决这个问题,我只是删除了旧按钮,并创建了新按钮,它开始起作用了。