Introduction to Maya - Modeling Fundamentals Vol 1
This course will look at the fundamentals of modeling in Maya with an emphasis on creating good topology. We'll look at what makes a good model in Maya and why objects are modeled in the way they are.
# 1 23-05-2014 , 10:22 AM
Registered User
Join Date: May 2014
Location: Seoul, Korea
Posts: 3

about Eval

Anyone here familiar with 'Complete Maya Programming' book by David Gould? Well I'm studying with that book now and I'm kinda stuck with sth I don't understand. I was hoping you guys could help me understand.

I'm and the Eval Command part where the sample code reads as follows;


global proc calc (string $statement)
{
float $res = eval("float + $_dummy = " + $statement);
print ("\n" +$statement +"=" +$res);
}

calc("1+2");
calc("10*2/5");


results shows '1+2=3' and '10 *2/5 = 4'

what I don't get here is how these results show up.
I get the basic fact the eval command allows you to execute things in string form.
what I really don't get is this part ;

eval("float + $_dummy = " + $statement);
print ("\n" +$statement +"=" +$res);


I have no idea how this works to show both the equation and its result.
can any of you help me?

# 2 23-05-2014 , 10:49 AM
EduSciVis-er
Join Date: Dec 2005
Location: Toronto
Posts: 3,374
When you call calc, the equation gets passed into the function as statement. That gets evaluated which is what the eval command does, and stores the result as res. Then it prints the statement and the result. Hope that helps. I haven't used eval much before so I'm not too clear on the dummy variable but I guess it needs to be there to be syntactically complete. Let me know if you need further clarification.

# 3 23-05-2014 , 11:03 AM
Registered User
Join Date: May 2014
Location: Seoul, Korea
Posts: 3
I still don't get it,
so when calc is called the ("1+2"); for example is sent into the $statement function
but what does the rest of the script like "float + $_dummy = " part have to do with showing
1+3=4 result?

# 4 23-05-2014 , 04:36 PM
NextDesign's Avatar
Technical Director
Join Date: Feb 2004
Posts: 2,988
Trace it out. (By the way, you have a syntax error in your eval statement. There is no + after float)

You first call calc with an argument:
Code:
calc("1+2");
This turns calc into the code below. Notice how statement is now "1+2".
Code:
global proc calc ("1+2")
{
    float $res = eval("float $_dummy = " + "1 + 2");
    print ("\n" +$statement +"=" +$res);
}
MEL then takes the two strings in eval, and makes them into one.
Code:
global proc calc ("1+2")
{
    float $res = eval("float $_dummy = 1 + 2");
    print ("\n" +$statement +"=" +$res);
}
The eval statement is then evaluated. This essentially does the following in the background. res holds the resultant value of the eval command. In this case, since you created a variable, it returns the value of the variable.
Code:
global proc calc ("1+2")
{
    float $_dummy = 1 + 2;
    float $res = $_dummy;
    print ("\n" +$statement +"=" +$res);
}
So when it gets to the print command, it prints out the statement that was given, "1+2", then an equal sign, then the result of the eval command, which is 3.


Imagination is more important than knowledge.
# 5 25-05-2014 , 10:26 AM
Registered User
Join Date: May 2014
Location: Seoul, Korea
Posts: 3
I think I get it now, thanks for the reply user added image

Posting Rules Forum Rules
You may not post new threads | You may not post replies | You may not post attachments | You may not edit your posts | BB code is On | Smilies are On | [IMG] code is On | HTML code is Off