Wednesday, September 02, 2009

Maya and Python - 1st try

C:\Documents and Settings\{username}\My Documents\maya\{version}\Maya.env
PYTHONPATH="C:\Documents and Settings\{username}\My Documents\maya\scripts"
* for Mac OS, DON'T use double quotes "..."
instead eg.: PYTHONPATH = /Users/username/Documents/maya/scripts

default for Mac OSX: ~/Library/Preferences/Autodesk/maya//scripts

start new hello.py file
put into the "scripts" folder

start maya. open Script Editor window
import hello
(to reload, use reload(hello))

.pyc compiled python also works in the scripts folder

sample py code:
print("hello world");
from maya.cmds import *
file(f=True, new=True); # new file
#select(all=True) # select all objects
#delete(all=True) # delete all objects. not supported anymore. Use "file -f -new;" instead.
# create a polygon cube
# store all names
names = polyCube(ch=True, o=True,w=5.038671,h=3.648038,d=3.788829,cuv=4)
# use for loop to print out the object and node name
#for s in names:
# print(s);
cube = names[0]
print(cube)
move(10, 0, 0, cube, r=True) # move (10, 0, 0), note that the order of arguments is different from MEL

just realised from the maya help doc:
"Objects and arguments are passed to commands as they would be in MEL, but they must be passed in the following order:

command arguments object flags/named arguments

This is different from MEL where the ordering requires that objects appear at the end of the argument list. However, Python requires that named arguments come after all other arguments."
eg.: move(10, 0, 0, cube, r=True) # python
MEL: move -r 10 0 0 cube

&

"Maya Python implementation requires you to assign a boolean argument to those flags that do not normally take any arguments."
eg.: file(f=True, new=True); # python
MEL: file -f -new

save the following python script on shelf:
import hello
reload(hello)

Script Editor > File > Save Script to Shelf...
this will facilitate easier execution of the hello.py script

No comments: