Home Technical Talk
The BRAWL² Tournament Challenge has been announced!

It starts May 12, and ends Sept 12. Let's see what you got!

https://polycount.com/discussion/237047/the-brawl²-tournament

Maya - Python - Get a list of all UV shells in a selected object?

polycounter lvl 11
Offline / Send Message
cromadbomber polycounter lvl 11
Looking for a function which returns a list of all UV shells from a selected object. Most of the stuff I found on the internet is a bit hacky and doesn't end up working well. I read through the official docs and I couldn't find anything like this so it makes me wonder does it even exist. Basically I would need a list of those UV shells and to be able to select faces/verts/edges inside of it.

MEL solution is fine aswel.

Using Maya 2018 by the way.

Replies

  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    Try this. Input the name of the mesh as argument.

    1. import maya.OpenMaya as om
    2. import maya.cmds as cmds
    3.  
    4. def getUvShelList(name):
    5. selList = om.MSelectionList()
    6. selList.add(name)
    7. selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
    8. pathToShape = om.MDagPath()
    9. selListIter.getDagPath(pathToShape)
    10. meshNode = pathToShape.fullPathName()
    11. uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
    12. allSets = []
    13. for uvset in uvSets:
    14. shapeFn = om.MFnMesh(pathToShape)
    15. shells = om.MScriptUtil()
    16. shells.createFromInt(0)
    17. # shellsPtr = shells.asUintPtr()
    18. nbUvShells = shells.asUintPtr()
    19.  
    20. uArray = om.MFloatArray() #array for U coords
    21. vArray = om.MFloatArray() #array for V coords
    22. uvShellIds = om.MIntArray() #The container for the uv shell Ids
    23.  
    24. shapeFn.getUVs(uArray, vArray)
    25. shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    26.  
    27. # shellCount = shells.getUint(shellsPtr)
    28. shells = {}
    29. for i, n in enumerate(uvShellIds):
    30. if n in shells:
    31. shells[n].append([uArray[i],vArray[i]])
    32. else:
    33. shells[n] = [[uArray[i],vArray[i]]]
    34. allSets.append({uvset: shells})
    35. return allSets
  • cromadbomber
    Offline / Send Message
    cromadbomber polycounter lvl 11
    @pasha_sevez Alright. That seemed to work. It pretty much returned this on a 4 point plane with UV shell spreading across entire 0,1 space.

    [{u'map1': {0: [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [1.0, 1.0]]}}]

    0: - Representing first shell
    [0.0, 0.0] -1st point
    [1.0, 0.0] -2nd
     [0.0, 1.0] -3rd
     [1.0, 1.0] -4th
    Now I managed to iterate through this and obtain each UV shell and its respective UVs.
    Now the problem is using this data to obtain UV selection again. This needs to be put in a way that cmds.select() can understand.
  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    Ok, in your case it might be a good idea to modify a function a bit:

    1. import maya.OpenMaya as om
    2. import maya.cmds as cmds
    3.  
    4. def getUvShelList(name):
    5. selList = om.MSelectionList()
    6. selList.add(name)
    7. selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
    8. pathToShape = om.MDagPath()
    9. selListIter.getDagPath(pathToShape)
    10. meshNode = pathToShape.fullPathName()
    11. uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
    12. allSets = []
    13. for uvset in uvSets:
    14. shapeFn = om.MFnMesh(pathToShape)
    15. shells = om.MScriptUtil()
    16. shells.createFromInt(0)
    17. # shellsPtr = shells.asUintPtr()
    18. nbUvShells = shells.asUintPtr()
    19.  
    20. uArray = om.MFloatArray() #array for U coords
    21. vArray = om.MFloatArray() #array for V coords
    22. uvShellIds = om.MIntArray() #The container for the uv shell Ids
    23.  
    24. shapeFn.getUVs(uArray, vArray)
    25. shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    26.  
    27. # shellCount = shells.getUint(shellsPtr)
    28. shells = {}
    29. for i, n in enumerate(uvShellIds):
    30. if n in shells:
    31. # shells[n].append([uArray[i],vArray[i]])
    32. shells[n].append( '%s.map[%i]' % ( name, i ) )
    33. else:
    34. # shells[n] = [[uArray[i],vArray[i]]]
    35. shells[n] = [ '%s.map[%i]' % ( name, i ) ]
    36. allSets.append({uvset: shells})
    37. return allSets
    38.  
    39. getUvShelList( 'pCone1' )


  • cromadbomber
    Offline / Send Message
    cromadbomber polycounter lvl 11
    @pasha_sevez That works perfectly! 
  • pasha_sevez
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    cromadbomber said:
    @pasha_sevez That works perfectly! 
    No sweat!
  • SoungHun
    Offline / Send Message
    SoungHun null
    @pasha_sevez

    I want to get the mesh data of the FBX file object in view from MAYA. We also want to compare the mesh data before and after editing the object. Please
  • n4uj
    Offline / Send Message
    n4uj polycounter lvl 5
    Ok, in your case it might be a good idea to modify a function a bit:

    1. import maya.OpenMaya as om
    2. import maya.cmds as cmds
    3.  
    4. def getUvShelList(name):
    5. selList = om.MSelectionList()
    6. selList.add(name)
    7. selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
    8. pathToShape = om.MDagPath()
    9. selListIter.getDagPath(pathToShape)
    10. meshNode = pathToShape.fullPathName()
    11. uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
    12. allSets = []
    13. for uvset in uvSets:
    14. shapeFn = om.MFnMesh(pathToShape)
    15. shells = om.MScriptUtil()
    16. shells.createFromInt(0)
    17. # shellsPtr = shells.asUintPtr()
    18. nbUvShells = shells.asUintPtr()
    19.  
    20. uArray = om.MFloatArray() #array for U coords
    21. vArray = om.MFloatArray() #array for V coords
    22. uvShellIds = om.MIntArray() #The container for the uv shell Ids
    23.  
    24. shapeFn.getUVs(uArray, vArray)
    25. shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    26.  
    27. # shellCount = shells.getUint(shellsPtr)
    28. shells = {}
    29. for i, n in enumerate(uvShellIds):
    30. if n in shells:
    31. # shells[n].append([uArray[i],vArray[i]])
    32. shells[n].append( '%s.map[%i]' % ( name, i ) )
    33. else:
    34. # shells[n] = [[uArray[i],vArray[i]]]
    35. shells[n] = [ '%s.map[%i]' % ( name, i ) ]
    36. allSets.append({uvset: shells})
    37. return allSets
    38.  
    39. getUvShelList( 'pCone1' )



    Cheers man for that ;)
Sign In or Register to comment.