Keep up-to-date with Free tutorials!!

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

1 | 2 | 3

By Nicolas d'Haussy
Web: Open Site
| 38265 Views
| 0
Software used:
Maya

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;
}
}

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

Tutorial

Gallery Image.

Keywords: Maya, ZBrush, Modeling, Character

(14)
2 27317

Tutorial

Gallery Image.

Keywords: Arch-viz, 3ds Max, snow, modeling

1 26568

Tutorial

Gallery Image.

Keywords: object, chair, eduardo souza,

4 136786

Tutorial

Gallery Image.

Keywords: Zeno Pelgrims, Maya, ZBrush, tutorial

2 8242