Maya MEL check if object has an uv set with a specific name


Basically I need to copy UV sets from set called UVChannel_1 to map1 from all selected objects.
After that I delete the UVChannel_1 set.

The line that I am struggling with is in bold.

Any alternative much smarter script would also be appreciated.
For example
 1 Copying all UV set to UVs to map1 from all merged meshes, objects and selected objects if map 1 is empty.
 2 Set map1 as default
 3 Delete other maps

Thank you very much.

string $selectedObjects[] = ls -sl;
string $sObject;
for ($sObject in $selectedObjects){

if( // Object has at least one UV set and object has UV set named UVChannel_1//){

polyCopyUV -uvSetNameInput “UVChannel_1” -uvSetName “map1” $sObject;
polyUVSet -delete -uvSet “UVChannel_1” $sObject;


I really need such script.

Current problem:
I merged multiple objects and all of them are on different UV sets, so I have lets say 10 different sets and default merged objects is not the same. Therefore, some objects will be rendered black, until those faces’ uv sets are manually moved to Map1.

(Problem is not related to normals, missing textures or anything like that)

I believe that if primary UV sets are named the same, on merging objects, problem does not exists.

Perhaps script could rename primary uv sets and the rest would work normally?


The script in this thread (scroll down a bit) might help you:

You would need to separate your object first, and of course make sure “Merge UV Sets: merge by name” is selected when re-combining.


Thank you spire2 ! I can see it works now. Further work will show if no more issues remains.

PS 1:
I learned something again. black textures appear when there are no UVs in the set and it is copied by the script anyway. Therefore, check if UVs exists is required before copying.

Apparently, when merging after the scrip was applied, some surfaces becomes black again, furthermore only Map1 uv set remains, therefore UVs are not recoverable.
So I played around and solution to that is to name all uv sets to Map1 before merging. So ultimate script should be the script that spire2 refereed + renaming of uv sets to the same name (as map1) before merging.


So, regarding the rename of the UV sets to the same names. I am not a coder, but to rename all uv sets in selected objects, inspired by previous I created this:

// get selection
string $sel[] = `ls -sl`;
//figure out which UV window to query for UV sets
string $texWinName[] = `getPanel -sty polyTexturePlacementPanel`;
//Rename uv sets to the same name so it can be merged and no black textures will appear.
for ( $obj in $sel ) {
     select $obj;
    //query all UV sets on selection
    $UVs = `polyUVSet -q -auv`;
    for ($set in $UVs) {
        string $mapName = $set;
        if ($mapName != "map1"){
            //$mapName = "map1"; 
            // rename from any name to map1
            polyUVSet -rename -uvSet "" -newUVSet "map1";

So my workflow to avoid black surfaces on merging models will be:
1 Applying Script referred before - to merge UV sets to one map
2 Applying my script - to rename merged UV set to map1
3 Merging objects as usual

So far no issues with this approach.