Eval - Problem

Hi, I know, that most of you would solve this problem otherwise.
My coding-skills are not very good, but anyway:

[codebox]string $SelObjects[] = ls -sl; // save Names of current Selection to string-Array
int $NumberOfSelObj = size $SelObjects; // save Number of selected Objects to int-Variable

string $FinalEval;

	for ($i=0; $i<$NumberOfSelObj; $i++)		// for every Element do:
	{
		//eval ("`setAttr \"" + $SelObjects[$i] + ".Length\"" +  ($NewValue*$Increment) +"`");
		$FinalEval = ("`setAttr \"" + "TestCurve" + ".Length\" " + 400 +"`");
		//print ("\n" + $FinalEval + "\n");
		eval $FinalEval;
	}[/codebox]

Whenever I try to execute this script I get a "Syntax-Error"-Message, but I don't not why.

If I resolve the string manually, copy it and add the 'eval' it works just fine.

For some reason it does not work within the script, and I don't know what the problem is.
O can't find any reason for a Syntax-Error.

I'd write it like this..

string $SelObjects[] = ls -sl;

for ($o in $SelObjects) {

setAttr ($o+".Length ") 400;

}

yes dont use eval. You almost NEVER need to. So if you can find the 3 only places where you need to then use but not here.

in any case your eval string is missing a assignment for the ticks.

thanks !

this seems much more convenient

string $SelObjects[] = ls -sl;
string $FinalEval="";
for ($i=0; $i<size($SelObjects); $i++)
{
$FinalEval += ("setAttr (\""+$SelObjects[$i]+"\"+\".Length\") 400 ;\n");
}
eval($FinalEval);

I found that I had to use eval for a polyUnite command.
I was trying to send it a list of objects in a string array...

CODE
polyUnite -ch 1 ($Objects)

but that didn't work
the only way I could get it to work was to do this...

CODE
string $List = stringArrayToString($Objects, " ");
eval ("polyunite -ch 1 " + $List);

Sometimes, I find it hard to avoid eval!

QUOTE(JAMIE_YU @ 06/24/09, 06:42 AM) [snapback]309988[/snapback]
CODE
string $SelObjects[] = ls -sl;
string $FinalEval="";    
for ($i=0; $i<size($SelObjects); $i++)
{
    $FinalEval += ("setAttr (\""+$SelObjects[$i]+"\"+\".Length\") 400;\n");
}
eval($FinalEval);

Why would anybody bother with doing that? I mean its shorter to write the proper code and its 2 times faster. See gmasks answer.

QUOTE(andy_davis @ 07/01/09, 03:51 PM) [snapback]310567[/snapback]
I found that I had to use eval for a polyUnite command.
I was trying to send it a list of objects in a string array...

CODE
polyUnite -ch 1 ($Objects)

but that didn't work

Are you REALLY sure? I mean test:
CODE
{
  $ls=ls -sl;
  polyUnite -ch 1 $ls;
}

Works fine, for me, no eval. So if yoru code does indeed not work, then please report the maya version you get this problem with, as sure as hell teh 8.5 im running right now works fine. (will test 2009 when i get to a computer that has it). IF THIS DOES NOT INDEED WORK, then mayas on verge of self collapsing and this is indeed a very serious bug. That need to be reported to autodesk ASAP.

Odds are theres a declaration problem in your code instead.
QUOTE(andy_davis @ 07/01/09, 03:51 PM) [snapback]310567[/snapback]
Sometimes, I find it hard to avoid eval!

Like i said theres only 3 things you need to use eval for one of them looks a bitlike yours but its using a huge setAttr statement, for loong float array. because looping setattr is veryy inneficent. In reality when you see people using eval its often more of sign that they dont know how to THINK about code than anything else. The second is a jump point, they actually DOCUMENT THIS. And the 3 is if you need to use source at code time, see the docs on eval and source. So 90% of time when i see eval i see code thats stupid anyways.

Fine in guis tough since that's a jump point usage. So send me the code where you THINK you need eval an i will re-engineer it to not use eval. (jump points are sane because they are function pointers anyway.)

So mel rules of thumb are

  • If you use eval the likelihood is your doing things wrong, or misunderstood the problem.
  • If your using script jobs then the likelihood is slightly smaller but close to same. Especially if you find you need 30 script jobs

[quote name='Joojaa' date='07/01/09, 09:52 PM' post='310573']
[img]style_emoticons/<#EMO_DIR#>/laugh.gif[/img] Why would anybody bother with doing that? I mean its shorter to write the proper code and its 2 times faster. See gmasks answer.

Of course, I know , I just answer questions Eval - Problem

===============================================================

Your code does work, Mr Jooj.
God only knows why it didn't work when I tried it first time, it being the simpler solution.

Emergency over... Autodesk techs can get back to work now.

QUOTE(JAMIE_YU @ 07/01/09, 06:04 PM) [snapback]310579[/snapback]
Of course, I know , I just answer questions Eval - Problem

Jsut because somebody asks the wrong question doesn't mean you will give the wrong answer.

Thanks, gmask.

Your answer is very simple and exact.