Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

providing a subset of rand results from MEL script

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
728 Views, 7 Replies

providing a subset of rand results from MEL script

Hi all,

I'm a huge noob on this, so I'm sorry if this has been asked many times over. I'm a mature student, so didn't do coding etc. when I was at school - so again, apologies if this is obvious to you. I have tried the forum search function, but it seems that rand isn't often used here. I'm using the following code: 

 

proc generateVariedCubes() 
{
int $i;
int $j;
float $randSize;
for ($i=0;$i<5;$i++)
{
for ($j=0;$j<5;$j++)
{
$randSize = rand(2);
polyCube -w $randSize -h $randSize -d $randSize;
move -r (2*$j)0(2*$i);
}
}
}
generateVariedCubes();

And this is working fine. What I'm trying to do is to take these results and remove any cube smaller than 1, so making the spot it was on empty. How can I filter my results, without removing the position completely?

Alternatively/also, I want to just have the cubes set to on or off, rather than sized from 0-2 - so it could function as a quick and dirty random map generator tool. I'm certain that such tools exist - but I'm trying to learn how to do it myself. 

Ideally I would want the finished model in either UE or Unity. Thank you for any help you can give me!

Labels (2)
7 REPLIES 7
Message 2 of 8
Roland.Reyer
in reply to: Anonymous

Hi,

 

if you just want random numbers greater than 1 you could use:

 

$randSize = rand(1) + 1;
Message 3 of 8
Anonymous
in reply to: Roland.Reyer

Hi Roland, 

thanks for the answer. Currently I have something like:

21231
23121
12213
32112
11321

(rounded up). 

 

What I want is

02200
20020
02202
20200
02202

 

I believe that your code would make every cube a minimum of size '1' - is this correct?

Message 4 of 8
Roland.Reyer
in reply to: Anonymous

Your numbers seem to be missing a decimal point?

 

You can offset and "stretch" random numbers.

 

rand(1) = numbers greater 0 and less than 1

rand(1) + 1 = numbers greater 1 and less than 2

rand(1) * 10 + 1 = numbers greater 1 and less than 11

Message 5 of 8
mspeer
in reply to: Anonymous

Hi!

 

I agree with @Roland.Reyer  that it's better to alreay create the right values instead of removing unwanted values.

However if you also want to create a random placement based on a grid as in your example, then a simple if-statement can do this., where you can limit the min or the min and max size of cubes that should be created.

 

 

if ($randSize > 1)
or 
if ($randSize > 1 && $randSize < 10)

 

 

 

 

Example with your MEL script (you can set any value for min and max, in this example a max value of 10 has of course no effect):

 

 

proc generateVariedCubes() 
{
    int $i;
    int $j;
    float $randSize;
    for ($i=0;$i<5;$i++)
    {
        for ($j=0;$j<5;$j++)
        {
            $randSize = rand(2);
            print ($randSize + " ");
            if ($randSize > 1 && $randSize < 10)
            {
                polyCube -w $randSize -h $randSize -d $randSize;
                move -r (2*$j)0(2*$i);
            }
        }
        print "\n";
    }
}
generateVariedCubes();

 

 

 

Message 6 of 8
Anonymous
in reply to: Roland.Reyer

Apologies - I was using numbers to show an example grid.

If you read the 0 as an empty space, and the numbers as a cube. 

Message 7 of 8
Roland.Reyer
in reply to: Anonymous

Well, then @mspeer has the solution with the "if" condition for the creation and position of the cube.

 

Alternatively you can also work with probability for the creation of the cubes, independently of their sizes:

 

// this would be true in ~50% of all cases

if ( rand(1) > 0.5 ) ...

 

BTW: if you play with random values, maybe you want to have a look at my "Randomizer":

https://youtu.be/56leZj0a7-A

 

Message 8 of 8
mspeer
in reply to: Anonymous

Hi!

In my last example I still have some print lines included that I used for testing,

it should have been only this:

proc generateVariedCubes() 
{
    int $i;
    int $j;
    float $randSize;
    for ($i=0;$i<5;$i++)
    {
        for ($j=0;$j<5;$j++)
        {
            $randSize = rand(2);
            if ($randSize > 1 && $randSize < 10)
            {
                polyCube -w $randSize -h $randSize -d $randSize;
                move -r (2*$j)0(2*$i);
            }
        }
    }
}
generateVariedCubes();

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report