簡體   English   中英

Maya-如何使用多個文件創建python腳本?

[英]Maya - How to create python scripts with more than one file?

這是我在這里的第一篇文章,請了解我是一個初學者,並且正在學習“在職”。

有人可以解釋如何在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.pymainScript.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 # 

嘗試像這樣導入:

>>>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 

您需要確保python路徑上有任何可導入的模塊。

如果您的文件位於E:/main_folder ,則需要執行

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

等等。 設置方式(使用'bin / __ init__.py')是在告訴python該模塊名為bin並且它具有名為'mainScript'和'tools'的子模塊。 在這里長時間討論

嘗試這個

我的桌面上有一個文件夾,該文件夾名為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

希望這可以幫助 :)

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)

第一步 第二步

第三步:

第四步

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM