setAttr, GetAttr, ConnectAttr (2024)

Description: There are some main commands that you will use in most all your scripts. These commands allow you to get, set, add and connect attributes of nodes. This content is covered in chapter 5 in the text book.

Overview
Most MEL scripts ultimately serve one purpose:To automate building complex Maya scenes. This means to do what you do in the interface;
Make Nodes,
Make attributes,
Connect Nodes together,
and Create expressions.

A typical MEL script does at least one of the following:
Presents an interface to the Maya user to collect info about what the user wants the script to do.
Creates Nodes
Creates custom attributes for nodes that it has created
connects attributes for nodes it has created
creates and connects expression script nodes

Strategies for planning a MEL script
The MEL application is the entire collection of scripts that are necessary to do something useful. There are typically four stages in the act of writing a MEL script:
Planning what you want the script to do; Some time the hardest part
Planing how you want the application to work
Building the Application
testing


Pitfalls of some programmers are:

Trying to pin down everything before they start and never start because you can never think of everything
Not planning out well enough
Best practices approach would be:
Do it all by hand first
Make and follow useful rules for yourself: naming conventions, standard interface style etc
Hack something together and throw it away
Listen to your critics
Leave time for testing
Under promise and over deliver
Once you have a clear idea of what you want to create
and how your problem breaks down into the basic steps of :
Getting information,
Making Nodes,
Adding attributes,
Connecting attributes,
and adding expressions

it's time to start building the MEL script. Do not make the mistake of jumping right in before you have thought it out the best you can.

Query, Edit and Create:
You can query and edit some of the attributes of a command. This allows you to be able to find out values or change values in a simple way.
// Some common tools to get info about an attribute and change attributes are:
Query, Edit and Create which is the default setting.

sphere -name "mySphere" -radius 2;
sphere -query -radius "mySphere";
sphere -edit -radius 10 "mySphere";

float $radius = ` sphere -query -radius "mySphere"`;

sphere -name "secondSphere" -radius $radius;
sphere -edit -radius ($radius/2) "secondSphere";

Setting & Getting attributes of nodes
Example 1:
// create and set attributes on NURBS circle
circle -c 0 0 0 -nr 0 1 0 -sw 180 -r 1 -d 3 -ut 0 -tol 0.000610236 -s 8 -ch 1; objectMoveCommand;

setAttr "makeNurbCircle1.sweep" 180; // change the value yourself

setAttr "makeNurbCircle1.sections" 16; // change the value yourself

Example 2:
// common MEL scripts

//create a spotlight
spotLight
-name "mySpotLight"
-intensity 4
-penumbra -10
-rgb .5.3.2;

//set attributes of the light
setAttr "mySpotLightShape.intensity" 10;
float $prenumbra =`getAttr "mySpotLightShape.penumbraAngle"`; // get values from a node and apply to a variable
print $prenumbra;
setAttr "mySpotLightShape.penumbraAngle" 10; // change the value yourself

float $prenumbra =`getAttr "mySpotLightShape.penumbraAngle" `; // get values from a node and apply to a variable
print $prenumbra;

// set color individually
// setAttr "mySpotLightShape.colorR" 0.1;
// setAttr "mySpotLightShape.colorG" 0.3;
// setAttr "mySpotLightShape.colorB" 0.6;

setAttr "mySpotLightShape.color" 0.50 0.3 0.2; // set color all at once

//////////////////////////////////////////////////////////////////////////////////////////////
// create and set attributes on NURBS circle
circle -c 0 0 0 -nr 0 1 0 -sw 180 -r 1 -d 3 -ut 0 -tol 0.000610236 -s 8 -ch 1; objectMoveCommand;

setAttr "makeNurbCircle1.sweep" 360;

setAttr "makeNurbCircle1.sections" 16;

Connecting Attributes using connectAttr
// example 1: connecting attributes of sphere transZ and cube rotX
sphere -name "mySphere";
polyCube -name "myCube";
connectAttr mySphere.translateZ myCube.rotateX;

//example 2: Create simple light rig
circle -name "controlObject";
addAttr -longName "lightIntensity" -minValue 0 -maxValue 100 -keyable 1;
// adding a new attribute to this node

spotLight -name "keyLight";
connectAttr controlObject.lightIntensity keyLightShape.intensity;
parent "keyLight" "controlObject";
//Now setAttributes on the light using code, change color, add shadows and set shadow attributes.

Basic three Point Light rig: Before I wrote the script I planned out on paper what I wanted it to do.
// this script will create a standard 3 point lighting set up.
//all the lights will be pointed toward origin but not at 0,0,0
//There is a main Nurbs circle control object that is the parent of the light group. It allows
//for positioning of the lights as a group and has intensity controls for each light in the setup
// all the lights have an initial warm/ cool color set up. Key light and fill are warm and backlights are cool
//all lights are named appropriately

//defines key light
spotLight -name keyLight -intensity 75.0 -rgb 1.0 0.979 0.722 -penumbra -10 -decayRate 2;
setAttr "keyLightShape.penumbraAngle" -10;
setAttr "keyLightShape.dropoff" 5;
setAttr "keyLight.translateX" -5;
setAttr "keyLight.translateY" 4;
setAttr "keyLight.translateZ" 5;
setAttr "keyLight.rotateX" -23;
setAttr "keyLight.rotateY" -45;
//add depthMapShadows
setAttr "keyLightShape.useDepthMapShadows" 1;
setAttr "keyLightShape.dmapResolution" 650;
setAttr "keyLightShape.dmapFilterSize" 3;
setAttr "keyLightShape.shadowColor" -type double3 0.0817177 0.11258 0.189873 ;

//creates and sets the fill Light
spotLight -name fillLight -intensity 10.0 -rgb 1.0 0.979 0.722 -penumbra -10 -decayRate 2;
setAttr "fillLightShape.penumbraAngle" -10;
setAttr "fillLightShape.dropoff" 5;
setAttr "fillLight.translateX" 4;
setAttr "fillLight.translateY" .9;
setAttr "fillLight.translateZ" 5;
setAttr "fillLight.rotateX" 1.8;
setAttr "fillLight.rotateY" 38;
//add depthMapShadows
setAttr "fillLightShape.useDepthMapShadows" 1;
setAttr "fillLightShape.dmapResolution" 400;
setAttr "fillLightShape.dmapFilterSize" 6;
setAttr "fillLightShape.shadowColor" -type double3 0.0817177 0.11258 0.189873 ;

//Creates and sets the BackLightLeft
directionalLight -name backLightLeft -intensity 1.5 -rgb .601 0.807 0.902 -decayRate 2;
setAttr "backLightLeft.translateX" 4;
setAttr "backLightLeft.translateZ" -6;
setAttr "backLightLeft.rotateY" 132;

//Creates and sets the BackLightRight
directionalLight -name BackLightRight -intensity 1.5 -rgb .601 0.807 0.902 -decayRate 2;
setAttr "BackLightRight.translateX" -4;
setAttr "BackLightRight.translateZ" -6;
setAttr "BackLightRight.rotateY" 227;

//create the control object for the 3 point light set up
circle -name pointLightCNTRL;
setAttr pointLightCNTRL.scaleX 4.5;
setAttr pointLightCNTRL.scaleY 4.5;
setAttr pointLightCNTRL.scaleZ 4.5;

//Create group and parent control for Light setup
select keyLight fillLight backLightLeft BackLightRight ;
group -name pointLightGRP;
select pointLightGRP pointLightCNTRL ;
parent;

//adding attributes to the 3point light CNTRL
addAttr -ln "KeyLightIntensity" -at double -min -20 -max 2000 -dv 1 |pointLightCNTRL;
setAttr -e-keyable true |pointLightCNTRL.KeyLightIntensity;

addAttr -ln "fillLightIntensity" -at double -min -20 -max 2000 -dv 1 |pointLightCNTRL;
setAttr -e-keyable true |pointLightCNTRL.fillLightIntensity;

addAttr -ln "backLightLeftIntensity" -at double -min -20 -max 2000 -dv 1 |pointLightCNTRL;
setAttr -e-keyable true |pointLightCNTRL.backLightLeftIntensity;

addAttr -ln "BackLightRightIntensity" -at double -min -20 -max 2000 -dv 1 |pointLightCNTRL;
setAttr -e-keyable true |pointLightCNTRL.BackLightRightIntensity;

//connect attributes to PointLightCNTRL:
connectAttr -f pointLightCNTRL.KeyLightIntensity keyLightShape.intensity;
connectAttr -f pointLightCNTRL.fillLightIntensity fillLightShape.intensity;
connectAttr -f pointLightCNTRL.backLightLeftIntensity backLightLeftShape.intensity;
connectAttr -f pointLightCNTRL.BackLightRightIntensity BackLightRightShape.intensity;
// Connected pointLightCNTRL.BackLightRightIntensity to BackLightRightShape.intensity. //

Exercises
1.) Create two different objects that have one or more attributes connected.

2.) Create a control object that has new attributes added to it. Connect that attribute to a different object. For example, create a nurbs circle that controls the scale factor of an object

setAttr, GetAttr, ConnectAttr (2024)
Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 5849

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.