Maya MEL variables declaration and initialization

2.6k views Asked by At

Is it obligatory in MEL to initialize variables and especially strings when you declare them?

I know that it is not necessary to initialize string arrays:

string $buffer[];
$buffer[0] = "abc";

But what about strings and other variable types? Is it acceptably:

string $str;
$str = "abc";

Or should always use double quotes to initialize it?

string $str = "";
$str = "abc";
2

There are 2 answers

0
theodox On BEST ANSWER

You don't need to populate the variable, initializing it with a type declaration sets it to a default value (0 for ints, 0.0 for floats, and "" for strings). In general it's good practice to assign in place when the initial variable is meaningful:

string $topCamera  = "|top|topShape";

but it's fine to declare an empty variable when you need to placeholder but don't have the value yet;

int $cameraCount;
// make some cameras here;
$cameraCount = size(`ls -type camera`);

It doesn't hurt to declare $cameraCount as 0 in that example but it's just extra typing.

Insert obligatory warning to learn python instead here

0
SAF On

As a general rule, it is best to explicitly declare the type of a variable when it is defined. This ensures that you aren't depending on Maya to automatically determine the variable type for you.

from Complete Maya Programming 1

use string $str;