eval command weirdness

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
Old 02 February 2005   #1
eval command weirdness

eval "int $i = 3 + 2;";
print $i;

results in:

// Error: print $i; //
// Error: "$i" is an undeclared variable. //

What the heck am I doing wrong here?
Old 02 February 2005   #2
$i is local to the anonymous procedure represented by the code in the string. Use global declaration to make $i global, therefore existent in both scopes.
global int $i; eval( "int $i = 2 + 3;" ); print $i;

Last edited by failure : 02 February 2005 at 08:49 AM.
Old 02 February 2005   #3
Hmmm... okay. I find it very odd that the Maya help doesn't say ANYTHING about the eval command having it's own little scope.

Now if what you say is true, then why does this happen:

1. Run this script:

eval "int $i = 3 + 2;";

Okay, no errors here. NOW, try to run this simple command:

print $i;

It actually returns "5" as if "$i" was a global variable!? Why is this? If $i was a global variable, then the script listed in my first post would have worked fine...

If you could shed some light on this, I'd appreciate it.
Old 02 February 2005   #4
I believe the variable just has to be dcalred before you run the print command on it.

If you run

int $c;
eval "int $c = 3 + 2;";

print $c;

it will work. This might be the reason having the brackets around the eval command works. It forces Maya to evaluate the variable, and then the print command can read it.

Strangely though, having the eval command within the brackets should not work, since they should make any variables local to them.
Old 02 February 2005   #5
Originally Posted by Jakobud
eval "int $i = 3 + 2;";
print $i;

results in:

// Error: print $i; //
// Error: "$i" is an undeclared variable. //

What the heck am I doing wrong here?

A test, referring to your original code: If I run the first line - the eval statement - I get a result. If I then run the print statement, I get the correct result. If I hilite them both and run them "at the same time" as one block, I get the same error as you.
If I run each line once independantly (so the variable has already been declared and is in memory), then I hilite them both and run both statements at the same time, I do not get the error.

If I create a little proc in the script editor and try and "source" it, I get the same errors:
global proc printValue() { eval "int $b= 3 + 8;"; print $b; }

As previously mentioned, it probably has to do with scope, and IMHO I would consider it bad programming practise to declare your variable inside your eval statement. Better to declare it previously and only execute the assignment inside the eval statement.

Old 02 February 2005   #6
Okay, I gave this problem a second tought. Although it seems like a scope thing it really isn't. The variable is global in both cases. So my previuos claim is not entirely true.

The problem is related to the way scripts are run ( not just mel, but any scripting language infact ).
When you execute a piece of code it goes in two stages; first the interpreter takes the source and "compiles" it to some kind of bytecode ( checking for compile-time errors ), and then, as phase two, it executes the bytecode. Take a look at:

eval "int $i = 3 + 2;"; print $i;

Here we actually need to compile and execute two scripts; "outer" script - having above source, and "inner" script which is one passed as a string argument to eval.
Now, in the "outer" script there is no explicite declaration of $i since at the "outer" script compile-time the "inner" script is not compiled/run yet and it exists only as a source code inside string literal. So when "outter" gets compiled it reports the undeclared variable - which is a complie-time error.

When you execute the two statements separately, the "inner" and "outer" scripts of eval statement are compiled and executed without errros. This establishes the $i as a valid global name. So when you run the print statement afterwards, $i is recognized without problems

Last edited by failure : 02 February 2005 at 11:52 PM.
Old 02 February 2006   #7
Thread automatically closed

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.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 12:26 AM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.