Adverti horiz upsell
Random Rotation of Objects using MEL
Random Rotation of Objects using MEL
amstos, added 2006-04-26 09:42:18 UTC 57,513 views  Rating:
(3 ratings)
Page 1 of 1

This tutorial explains the rotation of multiple objects randomly each frame using Maya's powerful scripting language MEL. Each and every step is executed using MEL commands, to have a good understanding about Maya�s scripting language.

Open the script editor in Maya. The top pane shows the history of the command and their results. The bottom pane is where we can enter the MEL commands and execute by pressing the "Enter" key found in the far right hand side of the keyboard.

1. Position your Maya's view-port as seen in "Image1.jpg" (Outliner and Script editor opened and to be positioned accordingly).
 
click for larger version

2. Type "sphere;" in the script editor and execute. Executing this command would create a sphere with a name "nurbsSphere1".

3. Type "rename nurbsSphere1 a1;" in the script editor and execute. This renames the already created sphere with the name "nurbsSphere1" to a new name "a1".

4. Type "select a1; duplicate -rr; for ($i=1; $i<9; ++$i) duplicate -rr -st;" in the script editor and execute. Your view-port should resemble "Image2.jpg".

click for larger version

This selects the object "a1" and duplicates it 9 times. You should be having totally 10 objects in the scene starting from "a1" to "a10"

5. Type the following MEL commands in the same script editor and execute.


for($i=1;$i<=10;$i++)
{
select ("a"+$i);
$x = rand(-5,5);
$y = rand(1,10);
$z = rand(-5,5);
move $x $y $z;
select -d;
};



The above commands would move the ten objects in different positions, within the range you specify inside the "rand" function. Your view-port should resemble "Image3.jpg".

click for larger version

Don�t' worry if the spheres was placed differently, as the random function in Maya generates values randomly. So, please continue..

6. Open the Expression Editor (WindowAnimation EditorsExpression Editor) Type the following MEL commands and execute.

for($i=1;$i<=10;$i++)
{
$x = rand(1,1000);
select ("a"+$i);
rotate $x $x $x;
select -d;
};


These commands would change the rotation of each objects randomly. As we are typing in the Expression Editor, the rotation would be changing for each frame, giving each objects different rotation values. Your view-port should resemble "Image4.jpg".

click for larger version

You can map a ramp shader to the object to see the result in the shaded mode.

7. Select all the ten objects and apply a new Lambert shader.

8. Map a Ramp shader to the color attribute ("Image5.jpg") of the newly created Lambert shader (lambert2).

click for larger version


Change the Ramp Type to "U Ramp". See "Image6.jpg" for reference.

click for larger version

9. Click anywhere on the view-port and press "6" key to view the objects in the shaded and textured mode. Your view-port should resemble "Image7.jpg".

click for larger version

That's it, now play and see each object rotating of a different direction.

If you want to reduce the rotation speed of the object, simply select all the objects and �Bake� the simulation (EditKeysBake Simulation) and scale all the keys in the Graph Editor in horizontal direction. Be sure to delete the Expression we wrote previously, after baking the keys or else the objects would still be controlled by the Expression and your Scaling of keys in the Graph Editor won�t affect the object.

Note: This process of rotation of each objects randomly, can also be achieved setting manual keys for each of the object. This tutorial is just to show the power of MEL in a basic level.

"If at first you don�t� succeed, try a bigger hammer"

Author: Prashanth VS
Website: www.prashanthvfx.com
Suggestions and feedbacks can be mailed to maya@prashanthvfx.com