Mel Script - variables and data types

 

 

 

Everything related to programming will always come back to the concept of data storage. Without any data to act upon, the processes defined via our scripts are somewhat pointless. Mel has 4 main data storage methods

 

variables
external ascii or binary files
The Maya Dependency Graph
optionVar for preferences and settings

 

This page will only deal with variables, the other types are dealt with in later sections.

There are 3 basic types available within maya, namely int, float, and string. The general form of a variable declaration looks like that shown below. The typename (ie, int, float, string) describes the type of data that this variable will hold. The value assigned to the variable should hold a data type compatible the typename.

The variable_name is used to uniquely define the variable in conjunction with a $ symbol. For example, if the <variable_name> was counter, then we would need to use $counter to get and set it's value

 

 

 


				<type_name> $<variable_name> = <value>;

 

 

Example 1 : Variable Declaration and Assignment

Declare 3 variables of different types, assign some values to them and then print.

 

 


		
		{
			// declare an integer variable $i
			int $i = 100;
			
			// declare a string. The text string should be enclosed between
			// quotes "". 
			string $str = "hello!";
			
			// declare a floating point number
			float $f = 0.123;
			
			// assign some new values to the variables
			$f = 1000.02837563;
			$str = "Yippeee!";
			$i = 12234567;

			// print the variables out. The print function writes output
			// to the results window of Maya. All of the variables are 
			// automatically converted to a string when they are passed
			// as arguments to print.
			print( $i + "\n" );
			print( $str + "\n" );
			print( $f + "\n" );
	}
	

 

 

 

Example 2 : Local Variable Scoping

It is a GOOD idea to scope all variables unless you NEED to have global variables. The code below demonstrates this priniciple. $scopedVar only exists within the compound statement ( ie, between the brackets {} ). This is preferable since allowing all variables to be declared global means primarily that you waste memory, and secondly since variables cannot change their type once declared, you may get problems if another mel script uses a global of the same name.

So, scope your scripts within compound statements to prevent them producing excessive global variables.

Once a global variable has been declared, there is NO WAY to delete it or re-assign it's type unless you re-start Maya.

 

 

 



		// this variable will be global
		int $globalVar = 100;

		{
			// this variable only exists within the brackets {}
			int $scopedVar = 100;
		}

		// $scopedVar no longer exists. $globalVar does.


 

 

 

Example 3 : Automatic Variable Typing

When you declare variables, you can optionally decide to leave out the type of the variable. For example, the following is exactly the same as the last except this time we omit the types, Maya will simply guess the type of the variable from the type of value assigned to it.

The only real downside to automatic typing is that sometimes your variables may be declared a different type to what you expect.

 

 


		
		{
			// declare an integer variable $i
			$j = 100;
			
			// declare a string. The text string should be enclosed between
			// quotes "". 
			$str2 = "hello!";
			
			// declare a floating point number
			$fl = 0.123;
			
			// assign some new values to the variables
			$fl = 1000.02837563;
			$str2 = "Yippeee!";
			$j = 12234567;

			// print the variables out. The print function writes output
			// to the results window of Maya. All of the variables are 
			// automatically converted to a string when they are passed
			// as arguments to print.
			print( $j + "\n" );
			print( $str2 + "\n" );
			print( $fl + "\n" );
		}
		

 

 

Example 4 : String Operations

strings in mel are a lot more flexible than strings in languages such as C. There are many pre-built mel function to help manipulate string data, this is an example of a few of the most common.

 

 


	{
		// create a couple of strings to play with
		$hello = "hello";
		$world = "world";
		
		// concatenate the strings together
		$output = $hello + " " + $world;

		// read a word from the file and print
		print( $output + "\n" );
	

		// integers and floats can also be added into strings
		$output += (" " + 0.1234);

		// read a word from the file and print
		print( $output + "\n" );


		// to compare two strings simply just test for their equality
		// - you wouldn't be able to do this in C!!
		if( $hello == "hello" ) {
			print( "strings are equal\n");
		}


		// print the sting length by using the size() function
		print( "length= " + size($output) + "\n" );
	}