简体   繁体   中英

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

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.

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

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

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)

第一步 第二步

第三步:

第四步

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM