Free 3d tutorials: from 3d tips & tricks to advanced 3D software tutorials in 4 seconds.

Objects spreading methods

“Object spreading methods” by Nicolas d’Haussy
Melscripting issue.
Here is maybe one of the more interesting way to spread. You need simple melscripting knowledges (or other c-like scripting languages) . You must know some basics of melscripting… and especially the random function, duplicate function, transformations (move, rotate, scale) and the loop. Read the comments to understand the scripts steps.
Lets start code a simple object spreader. Simply duplicate several times the chosen object and place it randomly. The loop repeats several times this same action : returns random value for each axis, duplicate the object, place it with the random values. That is all!

global proc spreadObjects(    string $object,    int $duplicateNb,    float $Xmin,    float $Xmax,    float $Ymin,    float $Ymax,    float $Zmin,    float $Zmax    ){    for($n = 0; $n < $duplicateNb; $n++) {        // Get location random values        float $randomX = `rand $Xmin $Xmax`;        float $randomY = `rand $Ymin $Ymax`;        float $randomZ = `rand $Zmin $Zmax`;        // Duplicate and place it randomly        string $duplicatedObject[] = `duplicate $object`;        move $randomX $randomY $randomZ $duplicatedObject;    }    print($object + ” duplicated and randomly placed ” + $duplicateNb + ” times.\n”);}
To use this procedure/function, first execute it in the script editor or source the melscript file, then call the function from the command line. For instance, you can use this function by typing spreadObjects(“myObject”, 30, -10, 10, -10, 10, -10, 10). This tool we’ve just coded is very useful and easy to use.

Next, you might want to randomize the scale of this range of objects just created. Lets use a such as melScript one more time. This script applys a loop on selected objects to random their scales.

global proc randomizeSelectedObjectsScale(float $minScale, float $maxScale) {    // Build the selected objects list    string $objectsList[] = `ls -sl`;    // Random scale of each object of the list    for ($object in $objectsList) {        // Get random value and scale object        float $randomScale = `rand $minScale $maxScale`;        scale $randomScale $randomScale $randomScale $object;    }    print(“Random scale on ” + size($objectsList) + ” selected objects done.\n”);}
For instance, type randomizeSelectedObjectsScale(1, 5)

You should be able to code a such script to random rotation (replace scale by rotate), move or even other object attributes! Programming note : Do not create a procedure that do all in one. Work step by step, such as with passes/filters.

The spreading method of the spreadObject() function is not based on a surface, but on objects location. Lets code a script to spread objects on a surface, that is really more interesting (for instance, spread trees on a mountain). First, we will search for a method in Maya to have an object to be right placed on a surface. Second we will “translate” this method into melscript. Then we will merge this method in a such as loop in spreadObjects() function.
 
     

Pages: 1 2 3 4

Leave a Reply