Entering Python commands

Just like MEL, you can enter Python commands in the Command Line or the Script Editor. The Command Line can only accept single line Python commands, while the Script Editor provides a method to input multiple Python commands and view the results.

The Script Editor displays a running history of executed commands and the results of commands that Maya executes. You can copy most commands from the history section of the Script Editor and paste them in the input section to execute them. However, the results of Python commands are not echoed to the Script Editor exactly like MEL.

For more information, see To set up the Script Editor.

To type a Python command in the Script Editor

  1. Select Window > General Editors > Script Editor.
  2. Select a Python tab in the Script Editor.

  3. Type the following command in the input section of the Script Editor and execute it by pressing on the numeric part of your keyboard, or pressing + .
    import maya.cmds as cmds

    You now have access to Maya commands through the maya.cmds module.

    NoteThis is how you load modules in Python. For more information about other Python modules, see the online Python documentation.

    By convention throughout the documentation and the Python command reference, the maya.cmds module is renamed cmds, but you can rename the module anything you like.

    In the Script Editor, the Enter (Windows) or Return (Mac OS X) key above the Shift key does not execute a command. It starts a new line so you can type several commands before executing them.

    TipYou can import maya.cmds automatically at startup by creating a file called userSetup.py in your Maya scripts directory. The userSetup.py file can contain any Python commands you want to execute at startup.

    For more information, see Initializing the Maya Environment in and for Python.

  4. Create a polygonal cube by typing the following:

    A polygonal cube is created at the origin. This is equivalent to using the polyCube command in MEL.

    The command is removed from the input section of the Script Editor after execution. The command and the result of the command is output to the upper section of the Script Editor. The results of Python commands are displayed in the Script Editor between a set of Python commenting characters (#).

You can also enter commands in the Command Line.

Show the Command Line by selecting Display > UI Elements. By default the Command Line is in MEL script entry mode. You can change the mode by clicking on the text in the lower left hand corner. To enter Python commands, the command line must be in Python mode.

To use a Maya command with positional arguments

  1. Move the polygonal cube that you created with the following Python command:

    The polygonal cube moves to the XYZ co-ordinates 1,2,3.

    The move command accepts XYZ co-ordinates as its commands arguments.

  2. Scale the polygonal cube with the following command:

  3. Re-scale the polygonal cube with the following command:

    When a command is not provided with enough command arguments, the command uses the default arguments. The polygonal cube X scale is set to three, but the Y and Z scale values are set to their default argument one.

  4. Delete the selected polygonal cube.