“Object spreading methods” by Nicolas d’Haussy
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).
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. Taht 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 spiralstring $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;}
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!
(c) Nicolas d’Haussy , http://ndhaussy.free.fr
Latest Comments