CGTalk > Software Specific Forums > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 02-04-2005, 07:13 AM   #1
Jakobud
Veteran
Jake Wilson
Software Engineer
USA
 
Join Date: Feb 2003
Posts: 55
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?
__________________
Jakobud
http://www.jakobud.com
 
Old 02-04-2005, 08:47 AM   #2
failure
X
Nenad Jalsovec
Croatia
 
Join Date: Aug 2004
Posts: 410

$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.
Code:
global int $i; eval( "int $i = 2 + 3;" ); print $i;

Last edited by failure : 02-04-2005 at 08:49 AM.
 
Old 02-04-2005, 06:36 PM   #3
Jakobud
Veteran
Jake Wilson
Software Engineer
USA
 
Join Date: Feb 2003
Posts: 55
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.
__________________
Jakobud
http://www.jakobud.com
 
Old 02-04-2005, 07:12 PM   #4
ajk48n
Frequenter
 
ajk48n's Avatar
portfolio
Adam Katz
Freelance 3d Animator/Motion Graphics Designer
Sydney, Australia
 
Join Date: Nov 2002
Posts: 256
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-04-2005, 10:19 PM   #5
markbones
Frequenter
 
markbones's Avatar
Canada
 
Join Date: Mar 2003
Posts: 251
Quote:
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:
Code:
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.

Cheers!
__________________
-Mark-
 
Old 02-04-2005, 11:50 PM   #6
failure
X
Nenad Jalsovec
Croatia
 
Join Date: Aug 2004
Posts: 410

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:

Code:
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-04-2005 at 11:52 PM.
 
Old 02-05-2006, 12:00 AM   #7
CGTalk Moderation
Lord of the posts
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,481
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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
CGSociety
Society of Digital Artists
www.cgsociety.org

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

All times are GMT. The time now is 12:16 PM.


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