# Make a checkbox command a variable?

**URL:** https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739
**Category:** Maya
**Tags:** mel
**Created:** [May 31, 2005, 7:38pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739 "2005-05-31T19:38:22Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![desaintdesign](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@desaintdesign](https://forum.highend3d.com/u/desaintdesign)
#### Post date: [May 31, 2005, 7:38pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/1 "2005-05-31T19:38:22Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![failure](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@failure](https://forum.highend3d.com/u/failure)
#### Post date: [May 31, 2005, 8:19pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/2 "2005-05-31T20:19:59Z")

</div>

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;

---

<div class="post-metadata">

### Author: ![desaintdesign](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@desaintdesign](https://forum.highend3d.com/u/desaintdesign)
#### Post date: [May 31, 2005, 9:54pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/3 "2005-05-31T21:54:29Z")

</div>

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;

---

<div class="post-metadata">

### Author: ![failure](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@failure](https://forum.highend3d.com/u/failure)
#### Post date: [May 31, 2005, 10:26pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/4 "2005-05-31T22:26:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![joojaa](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@joojaa](https://forum.highend3d.com/u/joojaa)
#### Post date: [May 31, 2005, 10:59pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/5 "2005-05-31T22:59:13Z")

</div>

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+";")`;

---

<div class="post-metadata">

### Author: ![desaintdesign](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@desaintdesign](https://forum.highend3d.com/u/desaintdesign)
#### Post date: [May 31, 2005, 11:05pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/6 "2005-05-31T23:05:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![failure](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@failure](https://forum.highend3d.com/u/failure)
#### Post date: [May 31, 2005, 11:12pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/7 "2005-05-31T23:12:11Z")

</div>

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 ``

---

<div class="post-metadata">

### Author: ![desaintdesign](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/d/848f3c/32.png) [@desaintdesign](https://forum.highend3d.com/u/desaintdesign)
#### Post date: [May 31, 2005, 11:12pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/8 "2005-05-31T23:12:15Z")

</div>

...or without 'eval'

---

<div class="post-metadata">

### Author: ![failure](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@failure](https://forum.highend3d.com/u/failure)
#### Post date: [May 31, 2005, 11:19pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/9 "2005-05-31T23:19:40Z")

</div>

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 + ";" )`;

---

<div class="post-metadata">

### Author: ![joojaa](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@joojaa](https://forum.highend3d.com/u/joojaa)
#### Post date: [June 1, 2005, 9:37am UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/10 "2005-06-01T09:37:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![failure](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/f/f08c70/32.png) [@failure](https://forum.highend3d.com/u/failure)
#### Post date: [June 1, 2005, 6:03pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/11 "2005-06-01T18:03:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![joojaa](https://forum.highend3d.com/letter_avatar_proxy/v4/letter/j/5daacb/32.png) [@joojaa](https://forum.highend3d.com/u/joojaa)
#### Post date: [June 1, 2005, 7:53pm UTC](https://forum.highend3d.com/t/make-a-checkbox-command-a-variable/3739/12 "2005-06-01T19:53:30Z")

</div>

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.
