View Full Version : Can MEL be used to perform math equations

 thegarland10 October 2010, 08:02 PMHey guys, I have a new to perform certain equations numerous times. They're relatively simple equations, but it's a little tedious to work them out on paper over and over. So I'm trying to see if I can write a MEL script that will calculate the equation for me when I plug in certain variables. Kind of like a java applet. But I don't know anything about java programming, so I'm wondering if I can do it in MEL. These are the two equations: T = 2 * sqrt[ D/A ] where I get the value of T once I define D and A T = ((D - (A * t^2)) / (A * t)) + (2*t) where I get the value of T once I define D, A, and t Thanks.
mduvekot
10 October 2010, 09:04 PM
sure..

first you'd declare a procedure, like this:

proc float myProc (float \$D, float \$A) {
if (\$A == 0) { error "Cannot divide by zero\n";}
return 2 * sqrt(\$D/\$A);
}

If you save the procedure to one of the directories in the MAYA_SCRIPT_PATH, under the same name as the procedure, you can use it without having to declare it first, as Maya does it for you. In this example, I would save the code to myProc.mel in one of the script folders that I can find with

getenv MAYA_SCRIPT_PATH;

and then you can run your code:

myProc 2 3;
// Result: 1.632993 //

NaughtyNathan
10 October 2010, 09:07 PM
of course you can do this kind of thing, it's practically the base functionality of all programming/scripting languages! :)

it's also very easy, if fact it's almost exactly the same in python as you've written it out:

# python
from math import sqrt
D = 0.5
A = 3.14
T = 2 * sqrt( D/A )and here's how it would look in MEL seeing as how that's what you asked for:

// MEL:
float \$D = 0.5;
float \$A = 3.14;
float \$T = 2 * sqrt(\$D/\$A);
:nathaN

thegarland
10 October 2010, 10:15 PM
Thanks a lot, guys! Both methods worked great.

CGTalk Moderation
10 October 2010, 10:15 PM
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.

1