maya Getting started with maya Simple Python example


Example

Open the Maya listener with the button at the lower right corner of the help line. This opens the script listener.

Create a Python tab from the tab bar.

Here's a very basic script that will print out the positions of the cameras in a default scene. Enter this into the listener:

import maya.cmds as cmds          
cameras = cmds.ls(type ='camera')  
for each_camera in cameras:
    parent = cmds.listRelatives(each_camera, parent=True)
    position = cmds.xform(parent, q=True, translation=True)
    print each_camera, "is at", position

Select the script an execute it with CTRL+enter;

Here's another simple example that generates a random collection of cubes. It uses the python random module to generate random values.

import maya.cmds as cmds
import random
         
for n in range(25):
    cube, cubeShape = cmds.polyCube()
    x = random.randrange(-50, 50)
    y = random.randrange(-50, 50)
    z = random.randrange(-50, 50)
    cmds.xform(cube, t = (x,y,z))