Friday, January 27, 2012

Looping And Setting Attributes

i'm currently working on some skinning solutions where I would be holding weights on lots of joints, so i wrote a quick function to help me with that. It uses mel's settAttr and will skip things if it needs to letting us know what it skipped.

/////make attribute have the specified value
//ex to hold weight on joint2: naSetAttribute("liw",{"joint2"},1);
needs arguments
string attribute
string array objects
float attribute value
//supports joints, polys, nurbs, curves ...
//can be used for holding weights "liw"
//turning on off display
global proc
naSetAttribute(string $attribute, string $objects[], float $value )
string $objectArray[] = $objects;
//loop objects
for($i=0; $i < size($objectArray); $i++ )
string $object = $objectArray[$i];
//update attribue if object and attribute exists
if(`objExists $object`)
string $attr = $attribute; //hold weight attribute
int $attrValue = attributeExists($attr,$object);
if($attrValue == 1)
eval("setAttr "+$object+"."+$attr+" "+$value);
//setAttr ($object).liw 1;
else{ print("skipping "+$object+ " no attribute "+$attr+"\n"); }
else{ print("skipping "+$object+ " does not exist"+"\n"); }