/* .oO mapSpread by Nicolas d'Haussy Oo. This melScript spread randomly objects in 2D function to a texture map (has a photo taken from a plane). Design the texture the way you want to spread your object. For instance : a forest crossed by a river. .oO mapSpread parameters Oo. objectToBeDuplicated : name of the object you want to spread (by giving the quotes "myTree"). objectsNb : number of object you want to spread. map : texture map name (by giving the quotes "myTexture"). threshold : a value between 0 and 0.99 (Determines the contrast). placingFactor : just so as to scale placement (let tweak). .oO Notes Oo. Example of use : type it in the command line "mapSpread("pSphere1", 250, "file1", 0.9, 30)" (after having sourcing this script). Does not support layered texture directly. Convert it to file texture. For Maya 4.5 and higher (reason : colorAtPoint function required). USE OR MODIFY THIS SCRIPT AT YOUR OWN RISK - FOR NON-COMMERCIAL USE ONLY. */ global proc mapSpread ( string $objectToBeDuplicated, int $objectsNb, string $map, float $threshold, float $placingFactor ) { // Start message if (`exists colorAtPoint` != 1) { error "Cannot process because \"colorAtPoint\" MEL function does not exist.\n";} if ($threshold > 1) { $threshold = 0.99; print("Threshold corrected to 0.99 (To prevent error).\n");} print("\n\nProcessing started from the " + nodeType($map) + " texture called : \"" + $map + "\".\n"); waitCursor -state on; int $objectsCount = 0; while($objectsCount < $objectsNb) { // Get random values for placement float $randomU = `rand 0 1`; float $randomV = `rand 0 1`; // Analyse the map color at this point float $alphaLevelAtPoint[] = `colorAtPoint -o A -u $randomU -v $randomV $map`; // Check if the object can exist if($alphaLevelAtPoint[0] >= $threshold) { // An object can be created here. So place it. float $x = $randomU*$placingFactor; float $z = $randomV*$placingFactor; string $duplicatedObject[] = `duplicate $objectToBeDuplicated`; move $x 0 $z $duplicatedObject; // Count one more object $objectsCount++; // print ("\t" + $objectsCount + " . " + $objectToBeDuplicated + " duplicated to (" + $x + ", " + $z + ").\n"); } } // End message waitCursor -state off; print("Process successful : " + $objectsCount + " objects placed.\n\n"); }