LC #42 Pipers Alley

View Full Version : query type of a variable

06-26-2007, 11:56 PM
I need to query the type of a variables i read in from optionVars.
How do i know i deal with an integer or string?
This must be easy but i dont get it.

Less important: is there a way to show a progress bar in mel?

06-27-2007, 09:24 PM
Suppose to have declared those variables:

int $tmp = 100;
float $tmp2 = 200;
vector $tmp3 = <<300, 300, 300>>;

If you whant to know the type of those variables after their creation, you can use the command "whatIs".

whatIs "$tmp";
// Result: int variable //

whatIs "$tmp2";
// Result: float variable //

whatIs "$tmp3";
// Result: vector variable //

For the variables inside the optionVar, you can do as follow;

Build your 'command' as a "string" and query the optionVar variable you need:

string $cmd = "optionVar -q \"CEPrecision\"";

then, 'eval' your string in this way:

$cmdEval = eval($cmd);

and finally use `whatIs` to know the type of the variable coming from the `eval` command:

whatIs "$cmdEval";
// Result: int variable //

06-27-2007, 09:25 PM
thanks so much, i was just about to give up on this.

06-28-2007, 08:40 AM
That's completely cool with int or strings
but how do i find out if its array or a single value?
Its even hard to query an optionvar without knowing.
There is the arraysize flag but it also works for single vlaue vars.

06-28-2007, 10:07 AM
It is not a problem at all.

If a single value, it will return:

// Result: int variable //

If an Array:

// Result: int[] variable //

Try this example code for an Array of type int[]:

optionVar -iv tmpArray 10 -iva tmpArray 20 -iva tmpArray 30 -iva tmpArray 40 -iva tmpArray 50 -iva tmpArray 60 -iva tmpArray 70 -iva tmpArray 80 -iva tmpArray 90 -iva tmpArray 100;

string $cmdArray = "optionVar -q tmpArray;";

$cmdArrayEval = eval($cmdArray);

whatIs "$cmdArrayEval";
// Result: int[] variable //

and this one for an Array of type string[]:

optionVar -sv stringArray "uno" -sva stringArray "due" -sva stringArray "tre" -sva stringArray "quattro" -sva stringArray "cinque" -sva stringArray "sei" -sva stringArray "sette" -sva stringArray "otto" -sva stringArray "nove" -sva stringArray "dieci";

string $cmdString = "optionVar -q stringArray;";

$cmdStringEval = eval($cmdString);

whatIs "$cmdStringEval";
// Result: string[] variable //

06-28-2007, 10:26 AM
i tried to do it in a for loop yesterday and what
seems to happen is it seems to remember the data type of $cmdArrayEval
had in the previous iteration. So if the first value is an int there is a type error
if the next value is a string.... Maybe i am wrong.
I am doing an OptionVar to Xml script (and back), so i dont know the data types of th eoptionVars created by the user.

06-28-2007, 10:41 AM
If you tried my example code in the ScriptEditor without enclose it
within the braces, obviously it will remember the value as $cmdArrayEval
will become a Global variable and will exist until you d' nt close and re-open Maya.

Start a fresh Maya session and enclose the code within the braces.


optionVar -iv intArray 10 -iva intArray 20 -iva intArray 30 -iva intArray 40 -iva intArray 50 -iva intArray 60 -iva intArray 70 -iva intArray 80 -iva intArray 90 -iva intArray 100;

string $cmdInt = "optionVar -q intArray;";

$cmdIntEval = eval($cmdInt);

whatIs "$cmdIntEval";


06-28-2007, 10:49 AM
Quite stupid of me, many thanks.

06-28-2007, 06:57 PM
I must be badly retarded somehow i still dont get it.

proc string myProc(string $OptVarname)
string $type;
if (`optionVar -exists $OptVarname` && `optionVar -arraySize $OptVarname` > 0 )
string $cmdInt = "optionVar -q " + $OptVarname;
$cmdEval = eval($cmdInt);
string $cmd = "whatIs \"$cmdEval\"";
$type = eval($cmd);

print ("$type: "+"\n");
print $type;
print ("\n");
print "doesnt exist";
return $type;

string $type1 = myProc("tmpArray");
print ($type1 + "\n");

string $type2 = myProc("stringArray");
print ($type2 + "\n");

gives me the output:

// Error: file: D:\maya\mel\workflow_WORK\eample.mel line 10: Cannot convert data of type string[] to type int[]. //

For now i program it with a name convention to tell the optionVar type.
Not elegant really. BTW you seem to have been in realflow a long time?

06-29-2007, 01:26 AM
I was testing more and I got those strange result as well...

I will look into it and if I' ll find out why it does not work, I'll post in here.

Also, it seem it can not pass the string until the return type point or, it does but it not reinitialize the old variable returning this error:

// Error: line 11: Cannot convert data of type string[] to type int[]. //

...and seem also that the `eval` command does not its job least inside the Proc..

BTW you seem to have been in realflow a long time?

How you can tell?

06-29-2007, 08:21 AM
just googled what seemed to be an old web page of you.
Just me beeing curious, nevermind thanks for your help.

07-02-2007, 08:02 AM
The script is up on Highend ks_optVarToXML.mel (no auto-detection unfortunately)
Thanks for your help.

07-02-2007, 12:26 PM
my pleasure.

I'm glad to know you complete your script!
Thanks for let us know.

CGTalk Moderation
07-02-2007, 12:26 PM
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.