MEL How-To #76

Back · Previous · Next Maya

How do I undeclare a variable used in the Script Editor so I can use it as a different type?

Technically, you can't.

For example:

int $value = 3;
// Result: 3 //

string $value = "foo";
// Error: string $value = "foo"; //
// Error: Line 1.15: Invalid redeclaration of variable "$value" as a different type. //

A variable must go out of scope before it can be declared as a different type. If you declare a variable within the Script Editor (outside of a proc), then that variable must always be used as that type within the Script Editor (outside of a proc) for the duration of your Maya session.

To "redeclare" the variable, place your test code within braces:

int $value = 3;
{
  string $value = "foo";
  print ( "String $value = " + $value + "\n" );
}
print ( "Int $value = " + $value + "\n" );

The output:

String $value = foo
Int $value = 3

Alternately, write your test code as a procedure:

global proc test()
{
  string $value = "foo";
  print ( "$value = " + $value + "\n" );
}

test;

The output:

$value = foo

Then immediately:

global proc test()
{
  int $value = 3;
  print ( "$value = " + $value + "\n" );
}

test;

The output:

$value = 3

Wednesday, October 10, 2001