Can MEL be used to perform math equations

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
  10 October 2010
Can MEL be used to perform math equations

Hey 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

  10 October 2010

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


and then you can run your code:

myProc 2 3;
// Result: 1.632993 //
  10 October 2010
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);
  10 October 2010
Thanks a lot, guys! Both methods worked great.
  10 October 2010
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

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 10:08 PM.

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