Skip to content

Instantly share code, notes, and snippets.

@SEVEZ
Last active September 7, 2017 17:48
Show Gist options
  • Save SEVEZ/93ecdd27dffad0bda2d9 to your computer and use it in GitHub Desktop.
Save SEVEZ/93ecdd27dffad0bda2d9 to your computer and use it in GitHub Desktop.
import pymel.all as pm
def get_selected_uv_shells():
sel = pm.selected(o = True)[0]
selected_uvs = set(uv for uv in pm.selected() if isinstance(uv, pm.MeshUV))
# getUvShellsIds returns a list of shell_ids, and the number of shells
uv_shell_ids, num_shells = sel.getUvShellsIds()
# You still need to actually get the MeshUVs yourself
# Using sets instead of sublists because checking if an item is in a set is much faster
all_shells = [set() for i in xrange(num_shells)]
for uv_index, uv_shell_id in enumerate(uv_shell_ids):
all_shells[uv_shell_id].add(sel.uvs[uv_index])
# If you've not selected any UVs, I'm assuming you want them all.
# Converting your selection to uvs before getting selected_uvs would probably be more accurate
if not selected_uvs:
return all_shells
selected_shells = []
# This goes through and finds shells that contain one of the selected uvs
while selected_uvs:
test_uv = selected_uvs.pop()
for shell in all_shells:
if test_uv in shell:
break
selected_shells.append(shell)
# No need to check against that shell again
all_shells.remove(shell)
# Also we can get rid of all the uvs from that shell
# This way we don't end up iterating through all the UVs
selected_uvs.difference_update(shell)
return selected_shells
get_selected_uv_shells()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment