Friday, March 12, 2010

Mel Lesson 4 Practical Usage 1A

In this tutorial we will
1. Create a Cylinder and Cone
2. Store the transform values of the Cylinder in a variable (Tip: see lesson 2 Variable).
3. Paste the transform values from the variables on to cone with a click of a button.

1. Create a Cylinder
Create a cylinder and rotate and move it a little so that it has some transformation values.




2. Store the transform values of the Cylinder in a variable

Open your Maya Script Editor
Tip: See MEL lesson 0

Enter the following in the Script Editor:
getAttr ("pCylinder1.translateX")

You will get the below result in the history panel of the script editor
// Result: 1.628443 //

The below explains the what each word means in the above command
We need to store this value in a variable in order to use in future.
All we need to do now is
1. Create a variable (use a name that makes sense)
2. For the value part, tell Maya to fetch the translate X attribute of pCylinder1.

Execute the below command.
$objTx = getAttr ("pCylinder1.translateX")

Maya stores the translate X value of pCylinder1 in the variable $objTx.

If you now now tell Maya to show the value of $objTx, using the below command
print $objTx

it would display the translate X value of pCylinder1:
1.628443

3. Paste the transform values from the variables on to cone
Now create a Cone.
Execute the below code.
setAttr ("pCone1.translateX") $objTx

You will notice that the cone would have moved in translate X, which matched the translate X of pCylinder1.

The below codes will move the Cone to the Cylinder exact position on screen:

//====================================

//----------------------copy-------------------
$objTx = getAttr ("pCylinder1.translateX");
$objTy = getAttr ("pCylinder1.translateY");
$objTz = getAttr ("pCylinder1.translateZ");
$objRx = getAttr ("pCylinder1.rotateX");
$objRy = getAttr ("pCylinder1.rotateY");
$objRz = getAttr ("pCylinder1.rotateZ");
//----------------------paste-------------------

setAttr ("pCone1.translateX") $objTx;
setAttr ("pCone1.translateY") $objTy;
setAttr ("pCone1.translateZ") $objTz;
setAttr ("pCone1.rotateX") $objRx;
setAttr ("pCone1.rotateY") $objRy;
setAttr ("pCone1.rotateZ") $objRz;
//====================================
It is not always pCylinder1 and pCone1 that you want to copy and paste transformation values.
In the next Lesson 4 we will learn how to copy transformation dynamically from any object to any object by a click of a button.

No comments:

Post a Comment