Friday, March 26, 2010

Mel Lesson 4 Practical Usage 1B

Dynamically copy transform value from one object to the other
continued from Mel Lesson 4 Practical Usage 1A

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

//----------------------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");


Lets modify the above codes to make it take value from any object instead of pCylinder.
---------------------------------------------------------------------------------------------------
ARRAY
string $myObject [] = `ls -sl`;
The above code basically means list selected it stores all the objects name that are selected in maya in the variable called $myObject. Note that this is a variable array unlike a variable it can store more than one value. If you run the above comman after selecting a cone, a cylinder and a cube $myObject will have all the three names stored in it. (Remember the variable stores just the names of the objects and not the object itself.)

You can find the values of the variable $myObject by the using the below command.
print ($myObject[0]);
//result "cone";
print ($myObject[1]);
//result "cylinder";
print ($myObject[2]);
//result "cube";
I guess you have understood what array is.
---------------------------------------------------------------------------------------------------
Now select any object you wish to copy the transform value. Say for example you have selected R_Hnd_Cntrl
Run the below command.
string $myObject [] = `ls -sl`;
//result: R_Hnd_Cntrl
since you selected one object only, the object you selected would be in $myObject[0];
check whether you are right by executing the below.
print $myObject [0];
//result: R_Hnd_Cntrl

Now replace $myObject [0] instead of pCylinder in the command we used in our earlier exercise.
$objTx = getAttr ("pCylinder1.translateX");
 
Since $myObject [0] is a variable and not a string like pCylinder, you have to use it without quotes, which will tell maya to fetch the value of the variable.


$objTx = getAttr ($myObject[0]+".translateX");

You use the "+" to join a string and a variable or a variable and a variable. It is called concatenate-ing

Try the following command to understand better
print ($myObject[0]+".translateX") 
//result: R_Hnd_Cntrl.translateX

To be continued...

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.

Stereoscopy

Concept
Our two eyes sees objects from a slightly different angle. This around 2 inch difference in point of view helps one eye see the sides of an object better than the other eye. This difference is called parallax. We know how far an object is from us due to this parallax.

How stereoscopy works
The effect of stereoscopy is created by taking 2 images which is shot with a mild horizontal offset (only) and forcefully displaying the left image to the left eye (only) and right image to the right eye (only).


A Quick Experiment
Requirement:
1. A digital still camera
2. A system with photoshop
3. 3D anaglyph glasses (Blue Red glasses)

Procedure:
Take a photo of any thing, preferably that covers object that is far and near.
Slightly shift you camera to your right about 6inches but looking at the same places.
Strictly no up and down and no rotation. Try to be as accurate as possible. Best way is to place your camera on a table take one pic, shift it to the right slightly take another one.
First photo is your left image and second your right image.

Bring these two picture to Photoshop.
Make a psd file with both images as 2 layers.

First check the images whether it will work:
Reduce the opacity of the layer that is on top and see whether the images are matching horizontally. There will be vertical offset though.

If it is looking good reset the opacity back to 100%.


Now the challenge is to force your left eye see the image that is taken from left and the right eye see the image that is taken from right.

There are many methods, for home experiment we can only think of anaglyph glasses (spectacles with red color glass for one eye blue for the other).

What these glasses do?
Basically, the red side of the glass cannot see red channel of your image and the blue side cannot see the blue channel.

So in photoshop we have to select the left image and remove red channel.
Select the right image and keep only the red channel (remove blue and green channel). Then modify the mode of the top layer into multiply. Which will produce the anaglyph 3D image and there you go.
See this image with your 3D glasses. If image is not appearing in 3D, reverse the glasses and check.