Using flags in Python
 
 
 

Flags are used with commands to modify their execution. Python implements flags as named arguments. Some flags in MEL do not require values. As all named arguments in Python require a value, these flags are given the value True to enable the flag, or False to ignore the flag. When setting multiple named arguments, argument-value pairs are separated by commas within the brackets. Command arguments must appear before named arguments in a command.

To use flags in Python

  1. Create a polygonal cube with the width defined at creation time by typing the following:
    cmds.polyCube(width=5)
    

    The width flag allows you to set the width of a cube at creation time.

  2. Create a polygonal sphere with the radius and subdivisions in X and name defined at creation time.
    cmds.polySphere(radius=1, subdivisionsX=4, name="testSphere")
    

    You can use multiple flags to modify the execution of a command by separating the flag-value pairs with a comma. The radius flag sets the radius of the created sphere. The subdivisionsX flag sets the number of subdivisions in X for the sphere.

    Note

    A list of flags available for a command can be output to the Script editor by typing cmds.help("command"). For example: cmds.help("sphere")

To use flags with command arguments in Python

  1. Move the polygonal sphere by typing the following in the Script Editor:
    cmds.move(2,2,2)
    
  2. Select the polygonal cube.
  3. Move the polygonal cube by typing the following:
    cmds.move(1,2,3)
    
  4. Move the polygonal cube relative to its current position by typing the following:
    cmds.move(-1,0,-2,relative=True)
    

    The polygonal cube moves relative to its current position. The relative flag must be placed after the command arguments, as in Python, when passing arguments, named arguments must appear after positional arguments. The command arguments must always be the first arguments of the command.

  5. Move the polygonal sphere without selecting it by typing the following:
    cmds.move(3,2,1,"testSphere",relative=True)
    

    The name of the object must be passed as a string after the command arguments, but before the flags.

To use flags with multiple arguments in Python

  1. Create a yellow tinted point light by typing the following:
    cmds.pointLight(rgb=(1,1,0.5))
    

    The point light command creates a point light at the origin. The rgb flag specifies the color of the light. The rgb flag require three values; red, green and blue.

    When flags require multiple values, the values must be packed within Python’s tuple or list data types. Tuples are a method of storing multiple items of homogenous data within a single variable, similar to an array in MEL. The list is similar to the tuple, but supports multiple data types within a single variable. The above command packs the RGB values into a tuple. You could also use a list to achieve a similar effect.

  2. Move the currently selected light by typing the following:
    cmds.move(12,15,12)
    
  3. Create a blue tinted light by typing the following:
    cmds.pointLight(rgb=[0.2,0.2,1])
    

    Square brackets [] indicate lists, and parentheses () indicate tuples.

    The values for the rgb flag can be packed into either a list or a tuple and perform the identical operation.

  4. Move the currently selected light by typing the following:
    cmds.move(12,15,-5)
    
  5. Select the polygonal cube and polygonal sphere by clicking the polygonal cube, then shift clicking the sphere.
  6. Frame the selected objects by pressing the f hotkey on your keyboard.
  7. Render the scene by pressing the render current frame button on the Status line.

    The Render View opens and displays the following image.

  8. Select all objects in the scene and delete them by typing the following
    cmds.select(allDagObjects=True)
    cmds.delete()
    

    Using the Maya command select with the allDagObjects flag selects objects that exist physically in the scene such as geometry, IK chains and measure tools. These appear on an internal Maya representation called the DAG. For more information, see DAG in the Maya User Guide.