Friday, February 1, 2013

Attributes

1) getAttr and setAttr

    import maya.cmds
    loc = maya.cmds.spaceLocator()[0]; //create a new locator and store the name of its transform mode in a variable called loc
    sx = maya.cmds.getAttr(loc+ '.scaleX'); //store the locator's x-scale in a variable named sx
    print(sx); // print the result, which will be 1 by default
    sx *= 2; //double the sx value
    maya.cmds.setAttr(loc+'.scaleX', sx); //assign the new value to the node's attribute by passing a string with the node's name, a period, and the attribute name

2) Compound Attributes
    print(maya.cmds.xform(loc, q=True, translation=True)); //print the locator's translation using the xform command
    [0.0, 0.0, 0.0] //the result
    maya.cmds.xform(loc, translation=[0,1,0]); //using the xform command to set a new translation value (using a list)
    print(maya.cmds.getAttr(loc+'.translate')); //print the locator's translation using getAttr command
    [(0.0, 1.0, 0.0)] // the command returns a list that contains a tuple
    maya.cmds.setAttr(loc+'.translate', 1, 2, 3); //using setAttr to set a new translation value for the locator
Result:

3) connectAttr and disconnectAttr
    The basic requirement for attributes to be connected is that they be of the same type.  
import maya.cmds;
sphere = maya.cmds.polySphere()[0];
cube = maya.cmds.polyCube()[0];
//create a sphere and a cube and store the names of their transform nodes
maya.cmds.connectAttr(cube+'.ry', sphere+'.ty'); //connect the cube's y-rotation to the sphere's y-translation
maya.cmds.select(cube);
maya.cmds.disconnectAttr(cube+'.ry', sphere+'.ty');
# Result: Disconnect pCube2.rotate.rotateY from pSphere2translate.translateY. #
//execute the following lines to create a multiplyDivide node between the two attributes to scale the effect
mult = maya.cmds.createNode('multiplyDivide');
maya.cmds.connectAttr(cube+'.rt', mult+'.input1X');
maya.cmds.setAttr(mult+'.input2X', 1.0/90.0);
maya.cmds.connectAttr(mult+'.outputX', sphere+'.ty');
maya.cmds.select(cube);
Result: if you rotate the cube, the sphere translates 1 unit for every 90 degrees of rotation.

1 comment: