Home Technical Talk

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

polycounter lvl 10
Offline / Send Message
cromadbomber polycounter lvl 10
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
    Options
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    Try this. Input the name of the mesh as argument.

    import maya.OpenMaya as om
    import maya.cmds as cmds
    
    def getUvShelList(name):
        selList = om.MSelectionList()
        selList.add(name)
        selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
        pathToShape = om.MDagPath()
        selListIter.getDagPath(pathToShape)
        meshNode = pathToShape.fullPathName()
        uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
        allSets = []
        for uvset in uvSets:
            shapeFn = om.MFnMesh(pathToShape)
            shells = om.MScriptUtil()
            shells.createFromInt(0)
            # shellsPtr = shells.asUintPtr()
            nbUvShells = shells.asUintPtr()
    
            uArray = om.MFloatArray()   #array for U coords
            vArray = om.MFloatArray()   #array for V coords
            uvShellIds = om.MIntArray() #The container for the uv shell Ids
    
            shapeFn.getUVs(uArray, vArray)
            shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    
            # shellCount = shells.getUint(shellsPtr)
            shells = {}
            for i, n in enumerate(uvShellIds):
                if n in shells:
                    shells[n].append([uArray[i],vArray[i]])
                else:
                    shells[n] = [[uArray[i],vArray[i]]]
            allSets.append({uvset: shells})
        return allSets
  • cromadbomber
    Options
    Offline / Send Message
    cromadbomber polycounter lvl 10
    @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
    Options
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    Ok, in your case it might be a good idea to modify a function a bit:

    import maya.OpenMaya as om
    import maya.cmds as cmds
    
    def getUvShelList(name):
    	selList = om.MSelectionList()
    	selList.add(name)
    	selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
    	pathToShape = om.MDagPath()
    	selListIter.getDagPath(pathToShape)
    	meshNode = pathToShape.fullPathName()
    	uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
    	allSets = []
    	for uvset in uvSets:
    		shapeFn = om.MFnMesh(pathToShape)
    		shells = om.MScriptUtil()
    		shells.createFromInt(0)
    		# shellsPtr = shells.asUintPtr()
    		nbUvShells = shells.asUintPtr()
    
    		uArray = om.MFloatArray()   #array for U coords
    		vArray = om.MFloatArray()   #array for V coords
    		uvShellIds = om.MIntArray() #The container for the uv shell Ids
    
    		shapeFn.getUVs(uArray, vArray)
    		shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    
    		# shellCount = shells.getUint(shellsPtr)
    		shells = {}
    		for i, n in enumerate(uvShellIds):
    			if n in shells:
    				# shells[n].append([uArray[i],vArray[i]])
    				shells[n].append( '%s.map[%i]' % ( name, i ) )
    			else:
    				# shells[n] = [[uArray[i],vArray[i]]]
    				shells[n] = [ '%s.map[%i]' % ( name, i ) ]
    		allSets.append({uvset: shells})
    	return allSets
    
    getUvShelList( 'pCone1' )
    


  • cromadbomber
    Options
    Offline / Send Message
    cromadbomber polycounter lvl 10
    @pasha_sevez That works perfectly! 
  • pasha_sevez
    Options
    Offline / Send Message
    pasha_sevez polycounter lvl 13
    cromadbomber said:
    @pasha_sevez That works perfectly! 
    No sweat!
  • SoungHun
    Options
    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
    Options
    Offline / Send Message
    n4uj polycounter lvl 5
    Ok, in your case it might be a good idea to modify a function a bit:

    import maya.OpenMaya as om
    import maya.cmds as cmds
    
    def getUvShelList(name):
    	selList = om.MSelectionList()
    	selList.add(name)
    	selListIter = om.MItSelectionList(selList, om.MFn.kMesh)
    	pathToShape = om.MDagPath()
    	selListIter.getDagPath(pathToShape)
    	meshNode = pathToShape.fullPathName()
    	uvSets = cmds.polyUVSet(meshNode, query=True, allUVSets =True)
    	allSets = []
    	for uvset in uvSets:
    		shapeFn = om.MFnMesh(pathToShape)
    		shells = om.MScriptUtil()
    		shells.createFromInt(0)
    		# shellsPtr = shells.asUintPtr()
    		nbUvShells = shells.asUintPtr()
    
    		uArray = om.MFloatArray()   #array for U coords
    		vArray = om.MFloatArray()   #array for V coords
    		uvShellIds = om.MIntArray() #The container for the uv shell Ids
    
    		shapeFn.getUVs(uArray, vArray)
    		shapeFn.getUvShellsIds(uvShellIds, nbUvShells, uvset)
    
    		# shellCount = shells.getUint(shellsPtr)
    		shells = {}
    		for i, n in enumerate(uvShellIds):
    			if n in shells:
    				# shells[n].append([uArray[i],vArray[i]])
    				shells[n].append( '%s.map[%i]' % ( name, i ) )
    			else:
    				# shells[n] = [[uArray[i],vArray[i]]]
    				shells[n] = [ '%s.map[%i]' % ( name, i ) ]
    		allSets.append({uvset: shells})
    	return allSets
    
    getUvShelList( 'pCone1' )
    



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