PDA

View Full Version : Does a variable exist ?


th0rse
10-23-2009, 12:48 PM
Hello,

Is there a way to check if a variable exists? I want to check if it exists before acting on that variable.

As far as I know the command `exists` checks if there is a node or command with that name only.

I am also wondering if there is a way to destroy a variable if it's not gonna be used anymore.


Thanks

czemiello
10-23-2009, 02:22 PM
Hello ! In python there is a useful method try and except:

try:
print str(variable)+':Variable exist'
except:
print 'not exist'


Pozdro

GiantG
10-23-2009, 08:55 PM
same in MEL
http://download.autodesk.com/us/maya/2010help/Commands/catch.html

~NoMaD~
10-26-2009, 05:10 AM
I am also wondering if there is a way to destroy a variable if it's not gonna be used anymore.



Restarting Maya is about the only way to clear variables, unless you include them in a procedure... while testing scripts try putting {} around the script and Maya wont keep the vairables in memory after it runs.

eg.

{
string $test[] = `ls -mat`;
print $test;
}

Run the above and it will print the materials...

print $test;

but this line will return an error.

Jacobborsting
10-26-2009, 09:13 AM
In python you can also do:

if 'myVar' in locals():
print 'the variable myVar exist'

shibumenon
11-03-2009, 11:05 AM
the mel command
whatIs "$theVariable"
would return "Unknown" if $theVariable doesnot exist

th0rse
11-03-2009, 11:25 AM
Thanks for your replies

For now, I will do what Shibu said.
One of these days I have to start learning Python.

:-)

CGTalk Moderation
11-03-2009, 11:25 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.