Keep up-to-date with Free tutorials!!

 

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

 

- 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

By Nicolas d'Haussy
Web: Open Site
| Your Rating:
rating star nonerating star nonerating star nonerating star nonerating star none
(0 Votes)
| 18266 Views
| 0 Comments
| Comments 0
Date Added: 9th December 2009
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.

735_tid_image09.jpg
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).

735_tid_image10.jpg
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;
}
}

735_tid_image01.jpg
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;
}

735_tid_image11.jpg
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: fingers, hands, maya, feet, tutorial, modeling,

Go to tutorial
rating star fullrating star fullrating star fullrating star fullrating star full (8)
Comments 0 Views 34146

Tutorial

Gallery Image.



Keywords: object, watch, wristwatch,

Go to galleries 1
rating star fullrating star fullrating star fullrating star fullrating star full
Comments 0 Views 42362

Tutorial

Gallery Image.



Keywords: object, weapon, gun,

Go to galleries 1
rating star fullrating star fullrating star fullrating star fullrating star full
Comments 1 Views 42310

Tutorial

Gallery Image.



Keywords: Making of, ZBrush, Maya, Photoshop, Fantasy, Alice, V-Ray, Mudbox, NUKE

Go to galleries 1
rating star fullrating star fullrating star fullrating star fullrating star half
Comments 1 Views 22043
Readers Comments (Newest on Top)
no comments!
No comments yet. Be the first to comment!
Add Your Comment