PDA

View Full Version : problem with getAttr in a loop


Freshfromthegrave
02-07-2011, 09:48 AM
hey guys, i'm still very new to scripting so this is probably one of those noob questions ;) i'm writing a script that randomizes transforms based on min and max inputs from the user. at the moment i'm struggling to get the objects to do nothing if a checkbox next to the axis in the UI is not checked (so you're using the checkbox to say you want to translate randomly on X but not on Y etc).

so the offending code is:

string $selectedObjects[] = `ls -sl`;
for ($objects in $selectedObjects)
{


//Checkbox queries

int $TranslateXCheckboxOnOff = `checkBox -q -v TranslateXCheckBox`;




if ($TranslateXCheckboxOnOff == 1)
{
float $InputTransXMin = `floatField -q -v TranslateXMin`;
float $InputTransXMax = `floatField -q -v TranslateXMax`;

float $InputTransYMin = `floatField -q -v TranslateYMin`;
float $InputTransYMax = `floatField -q -v TranslateYMax`;

float $InputTransZMin = `floatField -q -v TranslateZMin`;
float $InputTransZMax = `floatField -q -v TranslateZMax`;

float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);
xform -t $TransXRandom $TransYRandom $TransZRandom $objects;
}
else
{
float $InputTransXMin = `getAttr $objects.translateX`;
float $InputTransXMax = `getAttr $objects.translateX`;

float $InputTransYMin = `getAttr $objects.translateY`;
float $InputTransYMax = `getAttr $objects.translateY`;

float $InputTransZMin = `getAttr $objects.translateZ`;
float $InputTransZMax = `getAttr $objects.translateZ`;

float $TransXRandom = rand ($InputTransXMin,$InputTransXMax);
float $TransYRandom = rand ($InputTransYMin,$InputTransYMax);
float $TransZRandom = rand ($InputTransZMin,$InputTransZMax);
xform -t $TransXRandom $TransYRandom $TransZRandom $objects;
}
}

so if i check the checkbox and input my values in the field it works great. if i select one object and don't check the checkbox, it works (objects stays put). but as soon as i select more than one object and don't check the checkbox i get select -r pCube1 pCube2 pCube3 ;
// Error: Cannot convert data of type float[] to type float. //

it seems to be trying to getAttr for all the objects at once but i don't understand why. using the $objects variable elsewhere works as expected and it runs through each object once in the loop. but it doesn't for the getAttr, what am i doing wrong? also if there is a more elegant way to get the object to do nothing when the checkbox is unticked then i'm open to suggestions :D this is the only way i've gotten it to come close to working so far. thanks for any help guys :wavey:

NaughtyNathan
02-07-2011, 10:10 AM
most MEL commands allows you to specify the items to operate on, but if none are given they operate on the selection list. This also means you can do stuff like this:

getAttr .translateX;which will return the .tx value of ALL selected items, i.e. retaurns a float ARRAY. Because you haven't catenated your strings correctly Maya is seeing your `getAttr`s as this (it's essentially ignoring the $objects part of the arg. I don't know why it would do this, I'd class this as a syntax error but.. MEL isn't the most consistant of languages...)

change all your:
float $var = `getAttr $objects.translate`;into:
float $var = `getAttr ($objects+".translate")`;you should always do this "catenation" when joining variables to literal strings. that should fix it.
:nathaN

Freshfromthegrave
02-07-2011, 10:20 AM
ah thanks nathan that works great :applause: i think by the time i finish this script i'm gonna have to give you half the credit ;D thanks for your help man :)

ewerybody
02-08-2011, 03:58 PM
Ahh the good old randomizer :scream:
probably the thing everyone does when starting mel and Maya scripting... (me included)

have you already written a renamer? :D

Freshfromthegrave
02-15-2011, 01:06 PM
hey eric sorry i only saw this now, yup i wrote a renamer too xD i actually had a use for this about a year back and i luckily found a script that saved my butt, but that only did rotation so i decided to make a more comprehensive one :) maybe beginning scripters are all a little unimaginative ;)

ewerybody
02-16-2011, 01:02 AM
maybe beginning scripters are all a little unimaginative ;)Hahaaa great! But nahh! Its all good! Its just obvious! Writing a really good and foolproof renamer is tricky though! The spirit to try it for yourself is what counts! Fresh minds sometimes bring very fresh ideas to the thing. And some spots of Maya can really really need some of that. :D

CGTalk Moderation
02-16-2011, 01:02 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.