Home Technical Talk

MEL script help getting my toggle boolean statement to work in a global proc?

polycount sponsor
Offline / Send Message
malcolm polycount sponsor
Hi, so I created this code which executes fine in the script edtior, it toggles the checker map on/off each time you run the script in the UV editor.

//This works fine
int $uvToggleChecker = !$uvToggleChecker;
if ($uvToggleChecker == 1)
{
print ("UV checker map turned on.");
}

else if ($uvToggleChecker == 0)
{
print ("UV checker map turned off.");
}

Buuuuut, when I run this exact same script in my MEL GUI it always returns true and won't toggle? What gives, I must be missing some secret formatting or something?

//This always returns true which is the exact same code in a window
global proc button20UVmapper ()
{
int $uvToggleChecker = !$uvToggleChecker;
if ($uvToggleChecker == 1)
{
print ("UV checker map turned on.");
}

else if ($uvToggleChecker == 0)
{
print ("UV checker map turned off.");
}
}

if (`window -exists TestWindow`)
{
deleteUI TestWindow;
};
window -title "" -toolbox true -sizeable true -resizeToFitChildren true TestWindow;
columnLayout -adjustableColumn true;
button -bgc 0.51 0.51 0.51 -width 60 -label "checker toggle" -command "button20UVmapper";

showWindow;

Replies

  • bitinn
    Offline / Send Message
    bitinn polycounter lvl 6
    you need to understand the scope of a variable. in a proc the variable is local, thus you always initialized it with the same value. you need a global variable to remember the state.

    also good to understand variable declaration, which answers your problem with previous thread, you were declaring it twice.
  • sprunghunt
    Offline / Send Message
    sprunghunt polycounter
    a better way to do this would be to check if the UV checker is turned on or off and toggle it based off that - and not use a boolean at all. 

    you can read the state of the checker map by querying the UI like this:

    int $checkerState = ` textureWindow -q -displayCheckered polyTexturePlacementPanel1`;

    then you can use the variable $checkerState to switch your if statements

    https://help.autodesk.com/cloudhelp/2016/CHS/Maya-Tech-Docs/Commands/textureWindow.html

    like this: 

    global proc button20UVmapper ()
    {
        int $checkerState = ` textureWindow -q -displayCheckered polyTexturePlacementPanel1`;
    
        if ($checkerState == 1)
        {
            textureWindow -e -displayCheckered 0 polyTexturePlacementPanel1;
        }
        
        else if ($checkerState == 0)
        {
            textureWindow -e -displayCheckered 1 polyTexturePlacementPanel1;
        }
        
    };
    
    <br>
  • malcolm
    Offline / Send Message
    malcolm polycount sponsor
    Thanks guys, I'm just learning MEL so I don't really understand the rules yet and I'm not a programmer this is my first programming language. Global variable was the problem, and I'll also check out if I can query the panels as suggested as that would be a bit cleaner, I'll post back if I can't get the panels working on my own.
    Here is the code that works with the global variable in a generic sense.

    global proc button20UVmapper ()
    {
        global int $uvToggleChecker;
        $uvToggleChecker = !$uvToggleChecker;
        if ($uvToggleChecker == 1)
            {
                print ("UV checker map turned on.");
            }

    else if ($uvToggleChecker == 0)
        {
            print ("UV checker map turned off.");
        }
    }

    if (`window -exists TestWindow`)
    {
    deleteUI TestWindow;
    };
    window -title "" -toolbox true -sizeable true -resizeToFitChildren true TestWindow;
    columnLayout -adjustableColumn true;
    button -bgc 0.51 0.51 0.51 -width 60 -label "checker toggle" -command "button20UVmapper";

    showWindow;
Sign In or Register to comment.