Search code examples
variablesmayamel

Maya MEL variables declaration and initialization


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";

Solution

  • 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