// Assigns a color to each vertex of a mesh.
// 1 Select the objects.
// 2 Execute this command, addVertexColorPrimVar("mycolor", <<0,0,0>>,<<1,1,1>>);
global proc addVertexColorPrimVar(string $shortname, vector $minval, vector $maxval) {
string $sel[] = `ls -sl`;
string $fullname = "rmanvC" + $shortname;
for($current in $sel) {
string $shapes[] = `listRelatives -s $current`;
string $shape = $shapes[0];
if(attributeExists($fullname, $shape) == 0) {
addAttr -ln $fullname -sn $fullname -dt vectorArray -k 1 $shape;
}
int $numVerts[] = `polyEvaluate -vertex $shape`;
string $dataStr = $numVerts[0] + " ";
float $r, $g, $b;
for($n = 0; $n < $numVerts[0]; $n++) {
vector $rgb = rand($minval, $maxval);
float $r = $rgb.x;
float $g = $rgb.y;
float $b = $rgb.z;
$dataStr = $dataStr + $r + " " + $g + " " + $b + " ";
}
eval("setAttr " + $shape + "." + $fullname + " -type vectorArray " + $dataStr);
}
}