` ` ``

Python in Maya

Saty Raghavachary

Code snippets etc.

Bring up MAYA -ver 2011.1.0_64

je guardians-maya
sq maya2011
MAYA -adb previz

import maya.cmds 
import maya.mel
from pymel.core import *

dir() # all the 'MEL' cmds!

for n in maya.cmds.ls():
  print type(n)
# oh no!!! maya.cmds is simply MEL in Python's clothing :(

for n in ls():
  print type(n)

values = ['one', 'three', 'two', 'three', 'four']
maya.mel.eval( 'stringArrayRemoveDuplicates( {"'+'","'

values = ['one', 'three', 'two', 'three', 'four']
mel.stringArrayRemoveDuplicates( values )

mel.eval( '''global proc myScript( string $stringArg, 
float $floatArray[] )
{ float $donuts = `ls -type camera`;}''')
mel.myScript('test',[]) # MelConversionError is thrown

All globals are in a dictionary:
melGlobals['$gGridDisplayGridLinesDefault'] = 15

// in MEL:
string $sel[] = `ls -sl`;
string $shapes[] = `listRelatives -s $sel[0]`;
string $conn[] = `listConnections -s 1 -d 0 $shapes[0]`;
setAttr ($conn[0] + ".radius") 1;

# Back in Py:

# intelligent nodes..
camTrans, cam = camera()  # create a new camera
fov = cam.getHorizontalFieldOfView()
cam.dolly( -3 )

dir(cam) # all of cam's methods/members

s = polySphere()[0]


if s.visibility.isKeyable() and not s.visibility.isLocked():
    print s.visibility.type()

dir(s.getChildren()) # what are we seeing?? 


maya.mel.eval("curve -d 3 -p 0 0 0 -p 1 0 0 -p 0 1 0 -p 0 0 1")
mel.eval("curve -d 3 -p 0 0 0 -p 1 0 0 -p 0 1 0 -p 0 0 1")

mel.eval("sphere -r 5")

basedir = sceneName().parent

s = polySphere()[0]
for face in s.faces:
    if face.getNormal('world').y > 0.0:
       select( face, add=1)

if 'numbers' not in optionVar:
    optionVar['numbers'] = [1,24,47]
numArray = optionVar.pop('numbers')

camXform, camShape = camera()
sphere = polySphere()[0]
sphere | camXform  # parent the camera to the sphere

camXform.tx >> camXform.ty  # connect operator
camXform.tx // camXform.ty  # disconnect operator

# print help for camShape's methods (but not members)
for i in dir(camShape):
    if (type(eval('camShape.%s' % i))) == type(camShape.tumble):
        help(eval('camShape.%s' % i))

Interestingly, you can write 'Python script plugins', which make use of Python-wrapped C++ Maya API calls. In other words - in the past, Maya plugins *had* to be coded in C++, but now, we can use Python. Advantage - cleaner/simpler syntax, and the ability to mix in code from dozens/hundreds of other Python modules.

Here is an example: download this file (helixCmd.py) to any directory (eg. /tmp or your home dir), and load it into Maya via the plugin manager (as you would, any other '.so' plugin). Then run this in the Python script-ed win:

import maya
maya.cmds.spHelix(p=0.3, r=7)

Cool! The helix curve that shows up in the scene, via the maya.cmds.spHelix() Python call that you just made, was generated from Python code contained in helixCmd.py. For fun, you can also run the plugin code via MEL (be sure to switch to the MEL tab first!):

// we're running, via MEL, Python code in 
// our 'helixCmd.py' plugin file :)
spHelix -p 0.4 -r 10; 

Study the code in helixCmd.py, make minor modifications (eg. look for the place where CV x,y,z values are computed, and set them to random (hint: 'import random') values :) Re-load the plugin, and run again (MEL or Python) - you should be able to see the result of your changes.

Making changes to existing code and observing what that does is one good way to learn programming. Another is to have a project/pet idea that you want to try out, and use that as a means to learn (ie look up necessary calls to make the code do what you want).

After more reading up and experimentation, you should be able to write Python script plugins in these categories:

  • deformers
  • emitters
  • fields
  • solvers
  • shape generators (surfaces, curves)
  • shaders
  • constraints
  • manipulators
  • general-purpose nodes (eg. a 'Perlin noise' node)
  • 'command' plugins (such as helixCmd.py above)
  • ...

So how do you learn more? Look at C++ plugin code - you will be able to 'port' these to Python without much effort. A huge collection of C++ plugin source files can be found in the '....devkit/plug-ins' directory of Maya's installation (eg. paste this in the browser URL box: file:///rel/third_party/maya/2011.5.3_64/devkit/plug-ins).