Modify attributes with scripts

To modify multiple attributes simultaneously, you can use the Attribute Spreadsheet (Windows > General Editors > Attribute Spread Sheet).

Alternatively, you can do so via scripting.

The following example demonstrates how to explore and use MEL scripting to disable the visibility of the all selected objects in your scene.

  1. Query the name of the Visibility attribute.

    In order to modify an attribute, you must know its exact attribute name.

    Select your object. In its shape node, for example pPlaneShape1, expand the Object Display section of the Attribute Editor and disable the Visibility attribute.

    The MEL command associated with your action is displayed in the top pane of the Script Editor; for example:

    setAttr "pPlaneShape1.visibility" 0;

    If you include this command in your script, it will perform the exact same action as you just did through the user interface.

  2. In your script, list the transform nodes of all selected objects in the scene.
    string $nodes[] = `ls -selection`;
  3. Create a loop and obtain the shape node of each object from its transform node.

    Because the visibility attribute is part of the shape node, you must first obtain the shape node from the transform node. Use the listRelatives command. See How can I get the name of a (selected) shape node.

  4. Use the setAttr command to set the visibility of each shape node to 0.

    The final script is as follows:

    {
    //Lists the transform nodes of all selected objects
    string $nodes[] = `ls -selection`;
    
    for ($node in $nodes)
    {
    //Loop through each object and obtain its shape node
    string $shapes[] = `listRelatives -shapes $node`;
    
    //Set the visibility attribute of each shape node to 0
    //The shape node is saved to the 1st (or 0th) element of the $shape array
    setAttr ($shapes[0] + ".visibility") (0);
    }
    }
  5. Save the custom script to the shelf.

      You can save your custom scripts to the shelf if you plan to run them often.

    1. In the Script Editor, select the lines of the script that you want to add, and middle-drag the selection to the shelf.

      A dialog appears asking you to select the language of your script.

      After selecting the language, an icon representing your custom script is now added to the Shelf.

    2. Right-click the icon and select Edit to open the Shelf Editor window.

      You can use this editor to customize your new Shelf addition. For example, customize the icon in the Shelves tab, or modify your command in the Command tab.

Querying the names of attributes

In addition to performing an action through the Maya interface and examining the result that is printed in the top pane of the Script Editor, you can also query the names of attributes in the following ways:

Modifying selected nodes

To get and set attributes, use the getAttr and setAttr commands.

You may want to change the attributes of selected nodes, or all nodes of a specific type, or all nodes in your scene.

Related topics