Script impr. for Assigning UV Sets

Script impr. for Assigning UV Sets

The-Digital-Shaman
Advocate Advocate
671 Views
2 Replies
Message 1 of 3

Script impr. for Assigning UV Sets

The-Digital-Shaman
Advocate
Advocate

Hi there,

 

I am looking for improvements of the script I've assembled, in a way that would allow me to apply it on multiple selected objects without side effects.

 

Result I am after is having two uv channels containing the same layout and named regardless of initial condition this way:

UVMap

UVLightmap

 

Script thus far has 3 parts:

 

//Rename UV Set 1 to 'UVMap'

print("Renamed UV Set 1 to 'UVMap'");

 

string $eachElement;

string $sel[] =`ls -sl`;

for ($eachElement in $sel)

polyUVSet -rename -currentUVSet -newUVSet ("UVMap");

 

There is this one issue with that part:

//// Error: line 7: Cannot rename uv set to an existing uv set name.

 

Is one of selected object already has UVMap channel, it won't rename others.

 

Then I have creating UVLightmap and copying content from UVMap to UVLightmap.

 

//print("Created 'UVLightmap'");

{string $objects[] = `uvSetMakeSelectCmd`;polyUVSet -create -uvSet "UVLightmap" $objects;};

//{string $objects[] = `uvTkCreateUVSetTargets`;polyUVSet -create -uvSet "UVLightmap" $objects;};

 

print("Created 'UVLightmap' and copied content from 'UVMap' to 'UVLightmap'");

polyCopyUV -uvSetNameInput "UVMap" -uvSetName "UVLightmap" -ch 1;

 

 

So that causes situations of creating UVLightmap1...2.. if UVLightmap already exists.

 

Atm Part 1 and Part 2+3 I have split under different hotkeys.

 

 

Gif shows some of the situations. Generally it works, but requires to go through individual cases manually.
I am after selecting 50 assets and get the same end result.

 

Therefore also part 4 is needed, where would be performed cleanup from other UV sets than 'UVMap' and 'UVLightmap'.

 

Many thanks,

DS

0 Likes
672 Views
2 Replies
Replies (2)
Message 2 of 3

mumpay
Community Visitor
Community Visitor

Did you ever end up solving this? I need literally this exact same thing and can't figure it out.

Message 3 of 3

mumpay
Community Visitor
Community Visitor

Actually I think I sorted it out. I wanted "put all the uvs I'm looking at, regardless of what set it's in, exactly these uvs I can see on this selected geo, into map1 and delete all the other sets".

 

General cleanup for Maya. Not quite what you wanted but the checks seem to be working so you could likely modify.

 

global proc
UvSetCleanup()
{
//Ensure All selected objects have a map1 (make a new one if map1 doesn't already exist)
$sel =`ls -sl`;
for ($obj in $sel){
    string $uvs[] = `polyUVSet -query -allUVSets $obj`;
    int $map1 = 0;
    for ($set in $uvs) {
        if ($set=="map1") { $map1 = 1; }
    }
    if ($map1==0) {
        polyUVSet -create -uvSet "map1" $obj;
        print ("Geo "+$obj+" didn't have a map1. It's now been created, and is empty, and is not default.\n");
        
    }
}

//Set map1 to be the default first index UV set
$sel =`ls -sl`;
for ($obj in $sel){
    int $uv_id[] = `polyUVSet -query -allUVSetsIndices $obj`;
    for ($i in $uv_id) {
        if($i == 0) {
            string $defaultset = `getAttr ($obj+".uvSet["+$i+"].uvSetName")`;
            if ($defaultset != "map1") {
                polyUVSet -reorder -uvSet $defaultset -newUVSet "map1" $obj;
                print ("Geo "+$obj+"'s default set is "+$defaultset+". It will be re-ordered with map1 so that map1 is the new default.\n");
             }    
                else { print ("Geo "+$obj+" Default set is map1, don't do anything.\n");}
        }
    }
}

   


//COPY The current visible uv sets of every selected object to map1
$sel =`ls -sl`;
for ($obj in $sel){
    string $uvs[] = `polyUVSet -query -currentUVSet $obj`;
    for ($set in $uvs) {
        if ($set!="map1") {
            polyUVSet -copy -uvSet $set -newUVSet "map1" $obj;
            print ("Geo "+$obj+": Copy "+$obj+"'s set "+$set+" to map1\n");
 
        }
        else { print ("Geo "+$obj+"is set to map1, don't do anything.\n");}
    }
}



//Set all the objects to map1 to check everything
{string $objects[] = `uvSetMakeSelectCmd`;
polyUVSet -currentUVSet -uvSet "map1" $objects;};



// delete all UV Sets except default uv map1 set for all selected objects
$sel =`ls -sl`;
for ($obj in $sel){
        int $uv_id[] = `polyUVSet -q -allUVSetsIndices $obj`;
        for ($i in $uv_id) {
            string $cuvname = `getAttr ($obj+".uvSet["+$i+"].uvSetName")`;
            if ($i == 0) {
                if ($cuvname!="map1") { print ("Geo "+$obj+"'s default uv set is not map1, it's "+$cuvname=".\n"); break; }
            }
            else if($i != 0) {
                if ($cuvname=="map1") { print ("Geo "+$obj+"'s map1 set is not the default right now.\n"); break; }
                else {  
                   polyUVSet -delete -uvSet $cuvname $obj;
                   print ("Geo "+$obj+"'s "+$cuvname+" set has been deleted.\n");
                 }
            }
        }
}

}

UvSetCleanup();
0 Likes