Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Forked from SEVEZ/new_gist_file_0
Created May 15, 2019 16:23
Show Gist options
  • Save JokerMartini/844c860d700e4eacefdeebc999f58ac4 to your computer and use it in GitHub Desktop.
Save JokerMartini/844c860d700e4eacefdeebc999f58ac4 to your computer and use it in GitHub Desktop.
'''
mayaScene.uv
namespace for uv manipulation and queries
'''
import maya.cmds as cmds
def get_names(obj):
'''
returns a name:index dictionary of the uvsets on the supplied object
'''
uvindices = cmds.polyUVSet(obj, q=True, uvn=True)
uvnames = cmds.polyUVSet(obj, q=True, auv=True)
return dict(zip(uvnames, uvindices))
def get_indices(obj):
'''
returns am index:name dictionary of the uvsets on the supplied object
'''
uvindices = cmds.polyUVSet(obj, q=True, uvn=True) or []
uvnames = cmds.polyUVSet(obj, q=True, auv=True) or []
return dict(zip(uvindices, uvnames))
def collapse(obj, map=None):
'''
Deletes all UV channes except the first. If the optional map parameter is supplied, that map will be copied into
the first map channe;
Returns a string list of the deleted channel names
'''
indices = get_indices(obj)
if map:
cmds.polyUVSet(obj, cp=True, uvs=map, nuv=indices[0])
else:
cur_map = cmds.polyUVSet(obj, q=True, cuv=True)[0]
if cur_map != indices[0]:
cmds.polyUVSet(obj, cp=True, uvs=cur_map, nuv=indices[0])
index_list = [k for k in indices]
index_list.sort()
index_list.reverse()
index_list.pop()
delenda = []
for item in index_list:
cmds.polyUVSet(obj, delete=True, uvs=indices[item])
delenda.append(indices[item])
return delenda
def delete_empty_uvs(obj):
'''
Deletes all empty UV channels on the supplied object
@note: the first UV channel is not touched, even it if is empty, since it cannot be deleted.
'''
indices = get_indices(obj)
index_list = [k for k in indices]
index_list.sort()
index_list.reverse()
index_list.pop()
delenda = []
for item in index_list:
ct = cmds.polyEvaluate(obj, uv=True, uvs=indices[item])
if ct == 0:
cmds.polyUVSet(obj, delete=True, uvs=indices[item])
delenda.append(indices[item])
return delenda
def find_multiple_uv_sets():
'''
Returns a list of all the meshes in the scene with multiple UV sets
'''
lvi = lambda uv_obj: len(cmds.polyUVSet(uv_obj, q=True, auv=True)) > 1
meshes = cmds.ls(type='mesh', ni=True)
return filter(lvi, meshes)
def force_map_1(obj, map=None):
'''
collapse UV sets and make sure that the surviving UV channel is named 'map1' for finicky programs that care about
UV set names
'''
delete_empty_uvs(obj)
collapse(obj, map)
cmds.polyUVSet(obj, rename=True, newUVSet="map_rename_temp")
cmds.polyUVSet(obj, rename=True, newUVSet="map1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment