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

删除回忆录丶 提交于 2019-12-24 03:37:18

问题


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

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

回答1:


You need to make sure any importable modules are on you python path.

If your files are in E:/main_folder you'll need to do

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'. Long discussion here




回答2:


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 



回答3:


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:

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:

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

Hope this helps :)




回答4:


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)



来源:https://stackoverflow.com/questions/32786300/maya-how-to-create-python-scripts-with-more-than-one-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!