将自定义python脚本导入maya

时间:2018-03-08 15:55:47

标签: python-2.7 scripting maya

我目前正在尝试将自定义脚本导入maya。我找到了example。我的gole是能够在外部编辑脚本,在每次点击时重新加载它。

我在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 # 

1 个答案:

答案 0 :(得分:0)

我不知道问题是什么,但要解决它我只是删除了旧按钮,并创建了新按钮,它开始起作用。