f
f
Global SectionArticlesBookstoreDictionaryDownloadsDVDsEventsForumsGalleryJob ListingsNewsNewslettersSchools
 Advanced Search
USER: PASS:  Login  Register

Subscribe
Site Links
Home > Maya > Tutorials > Scripting > Mel > Random Rotation of Objects using MEL > Page 1 Change background colorChange background colorChange background colorChange background color
Random Rotation of Objects using MEL
Tutorial by Prashanth
Login to add a Bookmark 1 votes for an average rating of 5.00

Updated:   04/26/06

Works on:    Irix  Windows  Linux  MacOS-X
Maya Versions:   1.x, 2.x, 3.x, 4.x, 5.x, 6.x, 7.x
Readership Level:   Basic
Owner:   amstos
Author Name:  Prashanth
Homepage:   http://www.prashanthvfx.com

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 Mayas 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

Dont' 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 wont 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 dont succeed, try a bigger hammer"

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

Login to add a Bookmark 1 votes for an average rating of 5.00

Related Learning Tools
Maya Related DVDs



UV Mapping for Games
Maya Related Books



Learning Autodesk Maya 2009: The Special Effects Handbook
Global DVDs



Digital Sets 2: Lighting and Texturing

Top Rated Maya Tutorials

1. Random Rotation of Objects using MEL by Prashanth (04/26/06)
2. Conversational MEL Part 3 by James Piechota an (11/30/06)
3. How to make a fun pause screen using mel by TJ Galda (11/15/07)
4. Conversational MEL Part 2 by James Piechota an (11/29/06)
5. How to sample color at any given point in a ramp by TJ Galda (04/08/08)


Related Tips


No Results to Display


More Tutorials by amstos

1. Generating Golden Particles by Prashanth (04/26/06)
2. Random Rotation of Objects using MEL by Prashanth (04/26/06)
3. Page Turn Animation by Prashanth (04/27/06)






Sponsors

Sponsor

Contact Us | Advertise | Post Jobs | Post News | Post Events | Terms