PDA

View Full Version : checkbox and eval?


cgbeige
03-31-2011, 05:36 PM
I need to figure out a way to get checkboxes not to stop my script from loading if the attributes they connect to don't exist yet. I'm guessing this is done with eval but how does that get factored into the checkbox code?:

menu -label "RT" -tearOff false;
menuItem
-label "Displacement"
-postMenuCommand "getAttr vraySettings.rt_displacement"
-ann "Toggle Displacement for RT"
-c "setAttr vraySettings.rt_displacement (!(`getAttr vraySettings.rt_displacement`))"
-checkBox (`getAttr vraySettings.rt_displacement`);
menuItem
-label "Subdivision"
-postMenuCommand "getAttr vraySettings.rt_subdivision"
-ann "Toggle Subdivision Rendering for RT"
-c "setAttr vraySettings.rt_subdivision (!(`getAttr vraySettings.rt_subdivision`))"
-checkBox (`getAttr vraySettings.rt_subdivision`);
menuItem
-label "Proxies"
-postMenuCommand "getAttr vraySettings.rt_vrayProxyObjects"
-ann "Toggle Proxy Rendering for RT"
-c "setAttr vraySettings.rt_vrayProxyObjects (!(`getAttr vraySettings.rt_vrayProxyObjects`))"
-checkBox (`getAttr vraySettings.rt_vrayProxyObjects`);

menuItem -sm true
-postMenuCommand "getAttr vraySettings.rt_engineType"
-label "RT Mode";
menuItem -label "GPU"
-c "setAttr vraySettings.rt_engineType (!(`getAttr vraySettings.rt_engineType`))"
-checkBox (`getAttr vraySettings.rt_engineType`);

Robert Bateman
03-31-2011, 07:02 PM
add in some objExists/attributeExists calls to your menu commands.

cgbeige
03-31-2011, 07:40 PM
can you have if (objExists ) stuff in menu commands? Could you post some example code?

NaughtyNathan
03-31-2011, 09:59 PM
a menu command is really nothing more than a block of MEL code, so you can do literally anything inside it. However, menuItems with checkboxes or radio buttons are slightly more complicated than this because once a menuItem is created it is static, and does not update (automatically!). The -cb and -rb flags are only significant on initial creation of the menuItem. This means that when you create the menuItem the checkbox will accurately reflect the attribute status, but it will never change/update unless you change it manually. If, for example, the user changes the attribute manually on the node your menuItem will be out of sync.
The best way around this is to use the menu -pmc flag when you create the menu. This defers the building of the menuItems to a global proc, which is called every time the menu is about to be opened. In this proc you can put as much `objExists` and attribute checking as you like.

global proc buildTestMenu()
{
menu -e -dai testMenuName; // EMPTY the menu!
setParent -m testMenuName;

int $isPersp = `objExists persp`;
int $isCam1 = `objExists camera1`;

int $pVis = ($isPersp) ? `getAttr persp.visibility` : 0 ;
int $cVis = ($isCam1) ? `getAttr camera1.visibility` : 0 ;

menuItem -en $isPersp -l "persp visible?" -cb $pVis;
menuItem -en $isCam1 -l "camera1 visible?" -cb $cVis;
}

{
global string $gMainWindow;
setParent $gMainWindow;

menu -l "test Menu" -p $gMainWindow -pmc "buildTestMenu" testMenuName;
setParent -m ..;
}


:nathaN

cgbeige
04-01-2011, 01:39 AM
awesome - thanks again, Nathan.

cgbeige
04-06-2011, 04:36 PM
hey Nathan,

I'm just getting into trying to incorporate this into my script and I'm wondering if you can explain the syntax here:

int $pVis = ($isPersp) ? `getAttr persp.visibility` : 0 ;
int $cVis = ($isCam1) ? `getAttr camera1.visibility` : 0 ;

I'm not clear on the ? and : usage.

NaughtyNathan
04-06-2011, 05:01 PM
ah, sure.. this is just a natty shortcut trick that allows you to do an if..else condition as a single line, rather than several.

this should explain it better than I could:

http://download.autodesk.com/us/maya/2010help/files/Controlling_the_flow_of_a_script_operator.htm

:nathaN

cgbeige
04-06-2011, 06:25 PM
ah okay thx

CGTalk Moderation
04-06-2011, 06:25 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.