Maya Training

Introduction to Hard Surface Modeling

Maya Training

Interiors and Furniture Vol 1 & 2 - Modeling & Cloth

Maya Training

Shading, Lighting and Rendering the Bedroom in MR

Maya Training

Robot volume one - Modeling with animation in mind

Maya Training

Spach-Alspaugh House the complete courseware

Maya Training

Burt The Cartoon Dinosaur Vol 01 - Modeling

New Maya Training

Robot Volume 02 - Hybrid Rigging
You are here > Home > SimplyMaya Community Forums
Loading

Welcome to Simply Maya

Please Sign in or Sign up for an account

Member Login

Lost your password?

Not a member yet? Sign Up!

Old 25-04-2008, 07:07 AM   #1
bendingiscool
Subscriber
 
bendingiscool's Avatar
 
Join Date: Jul 2006
Location: London
Posts: 567
Thanks: 0
Thanked 1 Time in 1 Post
Default geometry grid randomisation

I was going through the 'artists guide to mel' training from dt the otherday and found the really handy little loop that can make grids of geometry.

Heres the script...

int $i;
int $j;

for ($i=0;$i<5;$i++){
for ($j=0;$J<5;$j++){

polyCube;
move -r (2*$j)0(2*$i);
}
}

I was wondering how I would go about randomising their size on creation so that I could have random size cubes etc be produced.

Chris
bendingiscool is offline   Reply With Quote
Old 26-04-2008, 06:57 AM   #2
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here you go Chris, slightly modified code adding in a random number generator and applying it to the polyCube command to set the scale. Commented so people can follow it

Code:
int $i;
int $j;
// holds random value assigned each time a cube is created
float $randSize;

for ($i=0; $i<5; $i++) {
    for ($j=0; $j<5; $j++) {

        // generate a random float number between 0.0 and 2.0
        $randSize = rand(2);
        // use the random number to set the cube dimensions
        polyCube -w $randSize -h $randSize -d $randSize;
        move -r (2*$j)0(2*$i);
    }
}
The only thing to remember though is that $i, $j and $randSize will be global variables which can be bad. The next step would be to change it into a function so that the variables are local.

Code:
// function that creates a 5x5 grid of randomly sized cubes
proc generateVariedCubes() {
    int $i;
    int $j;
    // holds random value assigned each time a cube is created
    float $randSize;

    for ($i=0;$i<5;$i++){
        for ($j=0;$j<5;$j++){

            // generate a random float number between 0.0 and 2.0
            $randSize = rand(2);
            // use the random number to set the cube dimensions
            polyCube -w $randSize -h $randSize -d $randSize;
            move -r (2*$j)0(2*$i);
        }
    }
}

// call the function
generateVariedCubes();
Once you've run the function code then you wont need to rerun it for the current maya session. You can therefore run the generateVariedCubes() call whenever you want or multiple times and it will create the cubes.
As an exercise it'd be good to modify the function so that it generates another set of cubes to the right or left of the previous one. Up to you though

Simon
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote
Old 04-05-2008, 11:48 AM   #3
gster123
Moderator
 
gster123's Avatar
 
Join Date: May 2005
Location: Manchester Uk
Posts: 6,300
Thanks: 1
Thanked 51 Times in 45 Posts
Default

Hey Si

Ive not used ramdom in MEL, is it actually random so to speak (say in delphi you set a random number and unless you set the clock its always the same as its always produced in the same clock cycle)??

Like I say I've never used it so cant comment.
__________________
"No pressure, no diamonds" Thomas Carlyle
gster123 is offline   Reply With Quote
Old 05-05-2008, 04:58 PM   #4
t1ck135
Registered User
 
t1ck135's Avatar
 
Join Date: May 2004
Location: UK
Posts: 1,991
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Steve,

It gives a different result each time so can be called random. If you look into how it is seeded then with some hardcore maths it might be possible to predict the variance but thats way over the head of us mere mortals

Simon
__________________
Examples of bTraffic - a traffic animation tool for Maya
bFlocking - a tool for Maya 8.5+ to generate flocking and swarming behaviours
Jan/Feb Challenge 2007 Entry and W.I.P
May/Jun Challenge 2006 Entry and W.I.P
Mar/Apr Challenge 2006 Entry and W.I.P
Jan/Feb Challenge 2006 Entry and W.I.P
Nov/Dec Challenge 2005 Entry and W.I.P
Sep/Oct Challenge 2005 Entry and W.I.P
Jul/Aug Challenge 2005 Entry
www.flash-fx.net
t1ck135 is offline   Reply With Quote

A little bit about who we are
Links you might find useful
Catch up with SimplyMaya
SimplyMaya specialises in Maya tutorials. We offer over 1,000 individual Maya training videos, ranging from basic Maya tutorials through to intermediate Maya tutorials. Our tutorials are created by instructors with industry experience and are designed to get you up and running in Maya quickly without making it seem like hard work.

Copyright © 1999-2015 SimplyMaya - vBulletin® Copyright © 2000-2015, Jelsoft Enterprises Ltd.