Maya python api - deform mesh to UV layout

hi…

i’m new to the site & using the maya python api…

i’m trying to deform a mesh using the api for speed, my goal is to unwrap a mesh to match the uv layout of the current uv set.
i’ve been searching online and have gotten so far… the code below works ok on a mesh which only has a single uv shell.

but i’ll have multiple shells, which i split the verts on the uv borders.

my problem is with the setPoints… take a cube for example, i get a vert in every position to match the default uv layout (when the verts have been split)
but the polys are twisted… i’m guessing the issue is with vert indices aren’t the same as when i iterated over the point array ?

any help and advice would be much appreciated.

thanks


def deformMeshToUVSetLayout():
    # get the active selection
    selection = OpenMaya.MSelectionList()
    OpenMaya.MGlobal.getActiveSelectionList( selection )
    iterSel = OpenMaya.MItSelectionList(selection, OpenMaya.MFn.kMesh)

    # go through selection
    while not iterSel.isDone():

        # get dagPath
        dagPath = OpenMaya.MDagPath()
        iterSel.getDagPath( dagPath )

        # create empty point array & float arrays
        mesh_MPointArray = OpenMaya.MPointArray()
        U_MFloatArray = OpenMaya.MFloatArray()
        V_MFloatArray = OpenMaya.MFloatArray()

        # create function set, get points in world space & UVs
        meshFn = OpenMaya.MFnMesh(dagPath)
        meshFn.getPoints(mesh_MPointArray, OpenMaya.MSpace.kWorld)
        meshFn.getUVs(U_MFloatArray, V_MFloatArray)

        # write UV postions to mesh_MPointArray
        for i in range( mesh_MPointArray.length() ):
            mesh_MPointArray[i].x = U_MFloatArray[i]
            mesh_MPointArray[i].y = V_MFloatArray[i]
            mesh_MPointArray[i].z = 0

        # apply new point positions to mesh
        meshFn.setPoints(mesh_MPointArray, OpenMaya.MSpace.kWorld)
        return

got there in the end (with a little help!!)

just needed to collect a little more info with a vertex and uvId list.

Cool :slight_smile: I’ve been trying to do something similar myself, it would be awesome if you were willing to share it :slight_smile:

here you go…


def deformMeshToUVSetLayout():
    # get the active selection
    selection = OpenMaya.MSelectionList()
    OpenMaya.MGlobal.getActiveSelectionList( selection )
    #iterSel = OpenMaya.MItSelectionList(selection, OpenMaya.MFn.kMesh)
    iterSel = OpenMaya.MItSelectionList(selection, OpenMaya.MFn.kGeometric)

    # go through selection
    while not iterSel.isDone():
        # get dagPath
        dagPath = OpenMaya.MDagPath()
        iterSel.getDagPath( dagPath )

        # create empty point array & float arrays
        inMeshMPointArray = OpenMaya.MPointArray()
        U_MFloatArray = OpenMaya.MFloatArray()
        V_MFloatArray = OpenMaya.MFloatArray()
        vertexCountMIntArray = OpenMaya.MIntArray()
        vertexListMIntArray = OpenMaya.MIntArray()
        uvCountsMIntArray = OpenMaya.MIntArray()
        uvIdsMIntArray = OpenMaya.MIntArray()

        # create function set, get points in world space & UVs
        meshFn = OpenMaya.MFnMesh(dagPath)
        meshFn.getPoints(inMeshMPointArray, OpenMaya.MSpace.kWorld)
        meshFn.getUVs(U_MFloatArray, V_MFloatArray)
        meshFn.getVertices(vertexCountMIntArray, vertexListMIntArray)
        meshFn.getAssignedUVs (uvCountsMIntArray, uvIdsMIntArray)

        # write UV postions to inMeshMPointArray
        for i in range( vertexListMIntArray.length() ):
            inMeshMPointArray[vertexListMIntArray[i]].x = U_MFloatArray[uvIdsMIntArray[i]]
            inMeshMPointArray[vertexListMIntArray[i]].y = V_MFloatArray[uvIdsMIntArray[i]]
            inMeshMPointArray[vertexListMIntArray[i]].z = 0

        # apply new point positions to mesh
        meshFn.setPoints(inMeshMPointArray, OpenMaya.MSpace.kObject)

        iterSel.next()

Thanks a lot man! I really appreciate it :nod: