How to mFnMesh.setFaceColors in python?

49 views
Skip to first unread message

johancc

unread,
Sep 7, 2022, 12:31:01 PM9/7/22
to Python Programming for Autodesk Maya
Hey all. I have the code below(part of it) to set different vertex colors per uv island, but I was told that setting vertex colors with cmds is quite slow, and mFnMesh.setFaceColors would be faster. With my current code how can I apply the vertex colors to the selection of faces using the new method? Thank you

       for shell in chunk:
       
            selection_shell = shells.get(shell)
           
            cmds.select(selection_shell)
            facesSel = cmds.polyListComponentConversion(fromUV=True, toFace=True)
            cmds.select(facesSel)
            r = [random.random() for i in range(3)]
            cmds.polyColorPerVertex(facesSel,rgb=(r[0], r[1], r[2]), cdo=1 )
           
            cmds.select(deselect=1)

johancc

unread,
Sep 7, 2022, 6:11:01 PM9/7/22
to Python Programming for Autodesk Maya
So I was able to get vertex colors, the problem I have now is that I need to apply to sets of uv shells, how can I manage to do this? Thank you.

https://pastebin.com/KiRYC5kC

def setColors():

    shells = getUvShelList( cmds.ls(sl=1)[0] )

    print ( shells )

    """

    #3 similar objects combined into one

    {0: ['pCube4.map[0]', 'pCube4.map[1]', 'pCube4.map[2]', 'pCube4.map[3]', 'pCube4.map[4]', 'pCube4.map[5]', 'pCube4.map[6]', 'pCube4.map[7]', 'pCube4.map[8]', 'pCube4.map[9]', 'pCube4.map[10]', 'pCube4.map[11]', 'pCube4.map[12]', 'pCube4.map[13]'],
    1: ['pCube4.map[14]', 'pCube4.map[15]', 'pCube4.map[16]', 'pCube4.map[17]', 'pCube4.map[18]', 'pCube4.map[19]', 'pCube4.map[20]', 'pCube4.map[21]', 'pCube4.map[22]', 'pCube4.map[23]', 'pCube4.map[24]', 'pCube4.map[25]', 'pCube4.map[26]', 'pCube4.map[27]'],
    2: ['pCube4.map[28]', 'pCube4.map[29]', 'pCube4.map[30]', 'pCube4.map[31]', 'pCube4.map[32]', 'pCube4.map[33]', 'pCube4.map[34]', 'pCube4.map[35]', 'pCube4.map[36]', 'pCube4.map[37]', 'pCube4.map[38]', 'pCube4.map[39]', 'pCube4.map[40]', 'pCube4.map[41]']}

    """
   
    selList2 = om2.MGlobal.getActiveSelectionList()
    dagPath = selList2.getDagPath(0)
    selMesh = om2.MFnMesh(dagPath)
   
    vertList = list(set(selMesh.getVertices()[1]))
    lenVertList = len(vertList)
   
    for shell in shells:
       
   
        selection_shell = shells.get(shell)


        r = [random.random() for i in range(3)]
        tempColor = om2.MColor([r[0],r[1],r[2]])
        vertexColorList = om2.MColorArray(lenVertList, tempColor)
       
        selMesh.setVertexColors(vertexColorList, vertList)
   

setColors()
Reply all
Reply to author
Forward
0 new messages