简体   繁体   English

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

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

It's my first post here, please understand that I'm a beginner and that I'm learning "on-the-job". 这是我在这里的第一篇文章,请了解我是一个初学者,并且正在学习“在职”。

Can someone explain how can I import files from a different module in a Maya python script? 有人可以解释如何在Maya python脚本中从其他模块导入文件吗? I'm getting the following error: 我收到以下错误:

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

Here are my directories and codes: 这是我的目录和代码:

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

I'm trying to import tools.py in mainScript.py 我试图导入tools.pymainScript.py

EDIT: 编辑:

Ok, as it won't fit in a comment I edit this post to add precisions. 好的,因为它不适合评论,所以我编辑了这篇文章以增加精度。 I moved the 'Main_folder' on my Desktop and ran the script once again in Maya. 我将“ Main_folder”移到了桌面上,并再次在Maya中运行了脚本。 It still doesn't work but I have a more complete error traceback. 它仍然不起作用,但是我有一个更完整的错误回溯。 Here it is: 这里是:

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

Try importing like: 尝试像这样导入:

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

You need to make sure any importable modules are on you python path. 您需要确保python路径上有任何可导入的模块。

If your files are in E:/main_folder you'll need to do 如果您的文件位于E:/main_folder ,则需要执行

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

and so on. 等等。 The way you have it set up (with 'bin/__init__.py') you're telling python that the module is named bin and it has submodules named 'mainScript' and 'tools'. 设置方式(使用'bin / __ init__.py')是在告诉python该模块名为bin并且它具有名为'mainScript'和'tools'的子模块。 Long discussion here 在这里长时间讨论

Try this 尝试这个

I have a folder that is on my desktop and the folder is called Combo and has a script called combo.py and here is how I access it: 我的桌面上有一个文件夹,该文件夹名为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. 

In your case if you need to access tools.py then your path will be like: 在您的情况下,如果您需要访问tools.py,那么您的路径将类似于:

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

Hope this helps :) 希望这可以帮助 :)

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