Home Technical Talk

Mel script help with UV editor query if statement.

polycount sponsor
Offline / Send Message
malcolm polycount sponsor
I'm writing a simple script for my wife, when I click this button it checks to see if one of the UV sets is named lightmap and should producer an error and stop. If it doesn't find light map it should run the second part of the script. For some reason even when light map exists as a UV set it runs the error, but then also runs the else statement. What am I doing wrong? It's like it finds both statements to be true.

$danaUVs = `polyUVSet -q -auv`;

for ($danaSet in $danaUVs)
{
    if ($danaSet == "lightmap")
    {
        updateUvSetEditor;
        error "lightmap UVs already exist";
       
    }
     
    else
    {
        polyCopyUV -uvSetNameInput "map1" -uvSetName "lightmap" -createNewMap 1 -ch 1;
        toggleSelMode;
        toggleSelMode;
        selectMode -object;
        updateUvSetEditor;
        print "Lightmap UVs created";
    }
}   

Replies

  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    ok the problem is that you're applying the if statement to each part of the array of UV's. 

    This is a modified version that only does something after you've checked each uv set

    global proc uvsetTest()
    {
        string $danaUVs[] = `polyUVSet -q -auv`;
        int $lightMapExists = 0;
        
        //this bit checks if lightmaps exists
        for ($danaSet in $danaUVs)
        {
            if ((`gmatch $danaSet "lightmap*"`) == 1)
            {
                //updateUvSetEditor;
                error "lightmap UVs already exist";
                $lightMapExists = 1; //when a lightmap is found the variable is set
            }
        }    
        
        //then if they don't exist this does something
        if ( $lightMapExists == 0){
           
            polyCopyUV -uvSetNameInput "map1" -uvSetName "lightmap" -createNewMap 1 -ch 1;
            toggleSelMode;
            toggleSelMode;
            selectMode -object;
            print "Lightmap UVs created";
        }
    }<br>

  • malcolm
    Options
    Offline / Send Message
    malcolm polycount sponsor
    Thanks that definitely works, I'm not familiar with gmatch yet though, I'll need to research that to see what it's doing.
  • sprunghunt
    Options
    Offline / Send Message
    sprunghunt polycounter
    malcolm said:
    Thanks that definitely works, I'm not familiar with gmatch yet though, I'll need to research that to see what it's doing.
    gmatch allows you to use wildcards to do a partial string match - like you would do in maya's 'select by name' function

    http://download.autodesk.com/us/maya/2009help/Commands/gmatch.html

    So the way I've used gmatch it matches with lightmap1 or  lightmap2 or  lightmap365


  • malcolm
    Options
    Offline / Send Message
    malcolm polycount sponsor
    Very cool, need to start using that.
Sign In or Register to comment.