Keep up-to-date with Free tutorials!!


Sign up to our twice-monthly newsletter today for the latest tutorials, interviews and product information.

Sign me up to receive third-party emails from 3dtotal's partners, too!

- Latest news
- Exclusive Shop Offers
- Preview early content
- Plus much more


Not Ready to take that step? OK, Why not just Subscribe to the RSS Feed

submit tutorial
1 | 2 | 3
Object spreading methods

| Your Rating:
rating star fullrating star fullrating star fullrating star nonerating star none
(Score 3 out of 5 after 1 Votes)
| Comments 0
Date Added: 9th December 2009
Software used:

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.

Result of another similar function to randomize rotations. Is not it more realistic?

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.

To place an object on a surface we are not going to code it, but simply use two constraint nodes (the same you use for rigging). Geometry constraint restricts an object to a surface. And normal constraint aligns an object with the normal vectors of a surface. Let try it by yourself. The combination of those two constraint nodes places an object exactly as we want (and with ease).

Places an object with a combination of a geometry and a normal constraint nodes

To translate this method into melscript, let check the melscript commands F1 doc to learn more about flags and attributes. Study those two functions. Note you can see every actions made by Maya in the upper part of the script editor.

Now we will apply this method on several objects into a loop. That is the way you should work : first search for a method on an object, second apply it to a range of objects thanks to a loop. The script we are going to code is based on the spreadObjects() function. In this case, we are not going to give a random area thanks to a method based on the surface dimensions (by getting its bounding box size). So the duplicated object is randomly placed on the surface area. Then geometry and normal constraint nodes are applied to the object to place it right on the surface. As easily as it's said.

global proc spreadObjectsOnSurface(string $object, string $surface, int $duplicateNb) {

// Get surface size
float $surfaceMinX = `getAttr ($surface+".boundingBoxMinX")`;
float $surfaceMaxX = `getAttr ($surface+".boundingBoxMaxX")`;
float $surfaceMinY = `getAttr ($surface+".boundingBoxMinY")`;
float $surfaceMaxY = `getAttr ($surface+".boundingBoxMaxY")`;
float $surfaceMinZ = `getAttr ($surface+".boundingBoxMinZ")`;
float $surfaceMaxZ = `getAttr ($surface+".boundingBoxMaxZ")`;

for ($n = 0; $n < $duplicateNb; $n++) {

string $duplicatedObject[] = `duplicate $object`;

// Set random value and place the duplicated object
float $randomX = `floor(rand($surfaceMinX, $surfaceMaxX))`;
float $randomY = `floor(rand($surfaceMinY, $surfaceMaxY))`;
float $randomZ = `floor(rand($surfaceMinZ, $surfaceMaxZ))`;
move $randomX $randomY $randomZ $duplicatedObject;

// Could add random rotation and scale task here (apply it to $duplicatedObject)

// Aim the object to the surface
string $geometryConstraintNode[] = `geometryConstraint $surface $duplicatedObject`;
string $normalConstraintNode[] = `normalConstraint -aim 0 1 0 -wut "object" $surface $duplicatedObject`;

// Activate this line if you want to delete the constraints nodes keeping the object pose. So as too manipulate it easier after placing. Note normal constraint node prevent to rotate objects.
// delete $geometryConstraintNode $normalConstraintNode;

Objects right spread on a surface thank to spreadObjectsOnSurface() function

MelScript enables to code particular ways to spread objects. At this point, the procedures we have coded are based on the random function... Let change it by linear maths functions and logic. What is important is to have your algorithm to return the coordinates for each axis and then place the object (Maths, trigo circle, cosinus and sinus to axis coordinates...) Just a good start of computer generated arts... I mean, from a math abstract, you could get amazing results.

// Spread cubes on a virtual spiral

string $object= "pCube1";
int $objectsNb = 30;
float $step = .8;
float $amplitudeFactor = 3;

float $var, $x, $y, $sin, $amplitude;
$var = $x = $y = $sin = $amplitude = 0;

for($n=0; $n < $objectsNb; $n++) {

$cos = `cos($var)`;
$x = $cos*$amplitude;

$sin = `sin($var)`;
$y = $sin*$amplitude;

$z = $var;

$var += $step;
$amplitude = $var/$amplitudeFactor;

string $duplicatedObject[] = `duplicate $object`;
move $x $y $z $duplicatedObject;

Cubes placed from a spiral function

Note you can merge your scripts to make several in one. But I think better to work step by step, and it's easier to code some little scripts with their own actions. Let code your own melscript tools library

< previous page

1 | 2 | 3
Related Tutorials

Fantasy Weapon Texturing Technique

by Miklós Bencz
published on 2012-07-26

Keywords: object, weapon, texturing,

rating star fullrating star fullrating star fullrating star fullrating star half (18)
Comments 3 Views 68245

An In-Depth Look at UVW Mapping an Object

by Waylon Brinck
published on 2009-06-22

Keywords: uvw, mapping, object, max,

rating star fullrating star fullrating star fullrating star fullrating star full (3)
Comments 0 Views 83761

Stylized Character Modeling in Maya 2010

by Adam Dewhirst
published on 2010-12-30

Keywords: stylized, character, modeling,

rating star nonerating star nonerating star nonerating star nonerating star none (0)
Comments 0 Views 66203

Modelling a Car

by Sabino Leerentveld
published on 2009-12-09

Keywords: car, vehicle, modeling, rhino,

rating star fullrating star fullrating star fullrating star fullrating star full (1)
Comments 0 Views 22175
Readers Comments (Newest on Top)
no comments!
No comments yet. Be the first to comment!
Add Your Comment..