Maya - 如何使用多个文件创建python脚本?

时间:2015-09-25 16:05:15

标签: python maya

这是我在这里的第一篇文章,请理解我是初学者,并且我正在学习"在职"。

有人可以解释如何从Maya python脚本中的其他模块导入文件?我收到以下错误:

Error: ImportError: file E:/.../bin/mainScript.py line 17: No module named tools

以下是我的目录和代码:

Main_folder\
|-- install.mel
|-- ReadMeFirst.txt
`-- bin\
    |-- __init__.py
    |-- mainScript.py
    |-- tools.py
    `-- presets\
        |-- bipedPreset.txt
        |-- quadrupedPreset.txt
        `-- [...] .txt

我试图在tools.py

中导入mainScript.py

编辑:

好的,因为它不适合评论我编辑这篇文章以增加精度。我移动了'Main_folder'在我的桌面上,再次在Maya中运行脚本。它仍然无法正常工作,但我有一个更完整的错误追溯。这是:

# Error: Error in  maya.utils._guiExceptHook:
#   File "C:\Program Files\Autodesk\Maya2014\Python\lib\site-packages\maya\utils.py", line 332, in formatGuiException
#     result = u'%s: file %s line %s: %s' % (exceptionType.__name__, file, line, exceptionMsg)
# UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 11: ordinal not in range(128)
# 
# Original exception was:
# Traceback (most recent call last):
#   File "<maya console>", line 3, in <module>
#   File "C:/Users/UKDP/Desktop/Main_folder/bin/mainScript.py", line 17, in <module>
#     from tools import ClassTest
# ImportError: No module named tools # 

4 个答案:

答案 0 :(得分:0)

尝试导入如下:

>>>import san.libs.stringops

Where the san is dir(in san create __init__.py)
libs is a dir(in libs create __init__.py) 
and stringops.py is imported 

答案 1 :(得分:0)

您需要确保任何可导入的模块都在您的python路径上。

如果您的文件位于E:/main_folder,则需要执行

import sys
sys.path.append("E:/main_folder")
import bin.tools
import bin.mainScript

等等。你设置它的方式(使用&#39; bin / __ init __。py&#39;)你告诉python模块名为bin并且它有名为&#39; mainScript&#的子模块39;和&#39;工具&#39;。长时间的讨论here

答案 2 :(得分:0)

试试这个

我的桌面上有一个文件夹,该文件夹名为Combo,并且有一个名为combo.py的脚本,以下是我访问它的方法:

import sys #imports system's directory module
sys.path.insert(0,"C:/Users/dharmin.doshi/Desktop") #Add your directory, do not add your folder in the path unless the script is inside of folder inside of folder. Path needs to be in quotes and 0 is added as needed by the argument.
from Combo(Folder name) import combo (my python script)
reload (combo) #Optional to reload it every time you make changes to the script.
combo.run() #Call the function that runs the code. 

如果您需要访问tools.py,那么您的路径将如下:

sys.path.insert(0, "MainFolder/bin")
import tools

希望这会有所帮助:)

答案 3 :(得分:0)

exe1.py
import san.libs.stringops
import san.libs.maths
import san.libs.utils.ran
import san.printing

newstr = san.libs.stringops.add2string("hey","hi")
san.printing.myprint(newstr)

result = san.libs.maths.add(2,3)
san.printing.myprint(result)

random_number = san.libs.utils.ran.getnumber()
san.printing.myprint(random_number)

1st step 2nd step

step3

step 4