Simple Calculator in MEL

Hi folks,

I want to do simple calculator function in mel, i’m beginner to mel scripts so can anybody guide me to how to do this. I also want make simple UI for this in mel. What i’m thinking is like this.

I want to enter the inputs in ( Left side )and result should be displaced (Right side).


not really a reason to do this as its easier to do with the channelbox or script editor but if its for learning purposes then you could do it like that
if you make the 3 textfields all writeable, then you allow the user to give the result and a part of the calculation and it should figure out the other part.
you can also add another textfield in which you can input “+”, “-”, “/” or “*” to give your calculator more functionality

there is a lot of functionality to a calculator if you want to build it, allthough as i said not necessary as it is way easier to directly input it in the script editor

Thanks for your reply peerke. I’m now in learning stage so its enough.

I created a small mel which its doing add functionality, its working perfect. But what i want is the user can enter input values in UI itself and he will get result there only. I don’t want user to come to scripteditor and assign the values.

This my script.
$A = 10;
$B = 2;
$C = $A + $B;
print $C;

Here is UI i created for it, still procedure function need to add.

if (`window -ex "cal"`)
         {
         deleteUI cal;
         windowPref -remove cal;
         }
        window -widthHeight 100 100  -menuBar true -resizeToFitChildren true cal;
		rowLayout  -numberOfColumns 5 -columnWidth3 50 50 50;		
		string $First = `textField`;
		text -label "+";
		string $Second = `textField`;	
		button -label "=";	
		string $Third = `textField`;		
		showWindow cal;
		
		 

Not sure this is quite a rigging thing, but anyways, to help, few things.

First off, there is another UI command that will work better for what you’re trying to do than textField. You’ll want to run floatField instead, because it will only accept floating point numbers (numbers with decimals) thereby negating potential errors or misbehavior from letters being inserted in the fields.

the third field won’t need to be editable, and there’s a handy little flag -editable(-ed for short) where it will make the last one greyed out but not editable by the user.

Then to get the values and run the operation, you’ll want to create a separate function to read the fields and add the values and have the button run that as the command. Here it is in pseudo-code.

proc readFields()
{
    
    //take the value of First and use the floatField command with the -query flag to get the value. Save that to a variable
    
    //take the value of Second and use the floatField command with the -query flag to get the value. Save that to a variable
    
    //add the two together to make a third variable
    
    //take the value of Third and use the floatField command with the -edit flag to set the value. This could be combined with the line above if you want
    
}

Now question is, how do you get the names of the fields in to the process? To be honest, I haven’t used MEL in a long time, so I learned more of the elegant solutions to that in python. But a few ways you could do it could be one of these (but I’m sure there are more elegant solutions if you do some digging)
-to make those variables for the float fields global variables
-find a way to specify arguments for the process in the command flag on the button (one way is by turning it in to a string)
-make unique names for the fields, and just specify those in the function rather than use variables.

Once you get that working, if you’re feeling extra fancy, you can use the -changeCommand flag on the float fields, to have it actual run that process, thereby having it auto-update as you enter values and letting you ditch that “=” button. Then when you’re feeling mega fancy, make other buttons that allow you to change the operation to things like subtraction, multiplication, division, and exponents.

Hope that helps.

I wrote this a while ago, mostly because I was bored:
http://www.creativecrash.com/maya/script/nightshade-calculator

Feel free to take pieces of the code if you want to

@ nalzay - Thanks for pseudo-code, it helped me a lot.

@Nightshade - Thanks Nightshade your life saver…your script was awsome…

[QUOTE=Nightshade;23880]I wrote this a while ago, mostly because I was bored:
http://www.creativecrash.com/maya/script/nightshade-calculator

Feel free to take pieces of the code if you want to[/QUOTE]

I tried your script but images are not displaying on the buttons , can you suggest what should i do?

You need to copy the image files form icon folder and go to your doucuments–>maya—>pref—>icon and paste inside. Now if u run the script u’ll get the all images.

sabarishwar: You are welcome!

By the way, I was reading some intro stuff to Qt yesterday and found that you can easily make an LCD-display in Qt Designer. I’m not sure how well that Qt widget translates to Maya’s UI, but it should work out just fine (the dial widget turns out great for example). I thought I should mention this because I thought about my calculator when I read about that.

Hey guys, I’m just curious. How do you go about about using the Script Editor to do simple equations anyway? Kind of a noob question, I know but obviously if I type in “4-2=”, for example, I get a syntax error.
Thanks.
-B

in mel:
print (4-2);

in python:
4-2

[QUOTE=Theodox;24169]in mel:
print (4-2);

in python:
4-2[/QUOTE]

Thanks, Theodox! (*smacks own face)