Make a checkbox command a variable?

Im new to Mel- Im trying to make a checkbox turn on and off the visibility of another button in the GUI- i dont know how to correctly declare the variable-

boolean $fkVis = 1;

string $Rarm = button -label "A" -vis "$fkVis";

string $lHandIK = checkBox -label "IK/FK" -onc ($fkVis-1);

any help appreciated, thanks!

window;
shelfLayout;
string $Rarm = button -label "A" -vis 1;
string $lHandIK = checkBox -v 1 -label "IK/FK" -onc "button -e -vis 1 ($Rarm)" -ofc "button -e -vis 0 ($Rarm)";
showWindow;

thanks for the help- your scipt works perfectly, but when i use that code on mine i get a '$Rarm is an undeclared variable' - here is more of the code, any ideas?

global proc magnus()
{
string $image = (internalVar("-userBitmapsDir") + "magnus/magnus.bmp");

if ( window -exists imageWindow )
deleteUI -window imageWindow;

window -title "James" -width 282 -height 344
-sizeable false imageWindow;

string $form = `formLayout -numberOfDivisions 320`;

  string $Rarm =     `button -label "A" -vis 0 -bgc 1 .728 .732 -w 19 -h 18`;
  string $rHandIK = `checkBox -v 0 -label "IK/FK" -onc "button -e -vis 1 ($Rarm)" -ofc "button -e -vis 0 ($Rarm)"`;

  string $image = `image -image $image`;

  setParent ..;

formLayout -e

-af   $image              "top"     0
-af   $image              "left"    0

-ap   $Rarm               "top"     0   130
-ap   $Rarm               "left"    0   83

-ac   $rHandIK            "top"     20   $Rarm
-ap   $rHandIK            "left"    0   4

  $form;

showWindow;
}
magnus;

declare $Rarm as global ( not too elegant though ) or better:

string $rHandIK = eval( "checkBox -v 0 -label "IK/FK" -onc "button -e -vis 1 " + $Rarm + "" -ofc "button -e -vis 0 " + $Rarm + """ );

Nooo, dont use eval. thre -onc and ofc are strings so why eval somethings thats allready a string (evel is there so you can execute strings, well they arent executed, they are defeered just like eval anyway so no idea in double evaling things)

use

string $Rarm = button -label "A" -vis 0 -bgc 1 .728 .732 -w 19 -h 18;
string $rHandIK = checkBox -v 0 -label "IK/FK" -onc ("button -e -vis 1 "+$Rarm+";") -ofc ("button -e -vis 0 "+$Rarm+";");

Awesome!! that works! thanks alot!

now lastly, how could you append to that 'eval' so that the checkbox could turn off 2 buttons instead of 1.

both

global string $Rarm;
global string $Larm;

thanks.

Well, it's a matter of preffered syntax.

Using string variable name in the string expression which is argument to eval will dereference it into literal ( it's content ). It's exactly the same as using parenthesis to do it. The whole string expression is evaled not just the string variable. Btw. you are evaling when you use

...or without 'eval'

just add semicolon and another button command in the -onc and ofc strings. Like this:

with eval:

string $rHandIK = eval( "checkBox -v 0 -label "IK/FK" -onc "button -e -vis 1 " + $Rarm + "; button -e -vis 1 " + $Larm + "" -ofc "button -e -vis 0 " + $Rarm + "; button -e -vis 0 " + $Larm + """ );

without eval:

string $rHandIK = checkBox -v 0 -label "IK/FK" -onc ("button -e -vis 1 "+$Rarm+"; button -e -vis 1 " + $Larm + ";") -ofc ("button -e -vis 0 "+$Rarm+"; button -e -vis 0 " + $Larm + ";" );

Well eval takes about 2 times as long to execute thets why you WANT to use it without eval.

In a way, you are right. Eval needs to do a bit more work than . But that's why it's capable of executing any kind of code, not just commands. The extra work eval does mainly consists of compilation of source into bytecode, which happens a lot faster than the actual execution of the same code. So it's much less than x2 slower.

Well actualy eval does carry other overheads too but yes not quite 2x... its a runtime compile of the string, normal mel is compiled Once whan run and kept compiled in memory (no maya does not copile line by line), wich means the effcicency will kick in the next time you run the code.

Btw you never need to use the s to return stuff... you can use all maya commands as if they were procedures.

example

print( ls("-sl","-dag"));

Im sure you can think of uses for that the is a ctualy just shorthand notation for this syntax version but note that you could do this also

$flag="-sl";
print(ls($flag,"-dag"));

Also you cnt nestle two s inside eachother but the proc version can! Example note the utility proc for getting the element out of array (no arrays can be passed in this manner). the utility proc shold be ovious.

print(getAttr(getNthItem(ls("-sl","-dag"),1)+".castsShadows"));

Se this code can be dependency analyzed at code compile time, eval cant.