[Maya/Python] Performance decrease after moving mesh

Hey everyone,

I’ve recently started learning python and began with creating some small tools for maya.

The tool I’m currently working on selects vertices based on the vertex color & tolerance value set by the user.
So far the tool I’ve created works fine but I’m running into a performance issue.

When I run the tool on a plane with 8000 triangles, it takes less than 1 second to cycle through all the vertices and deselect the ones that don’t have the desired vertex color.
After moving the mesh around in maya the same script suddenly takes over 13 seconds to complete.

The issue seems to originate from the following part of my script:

# List with per channel color value of every vertex that's selected
colors = cmds.polyColorPerVertex(query=True, r=True, g=True, b=True, nun=True)

# Array that stores float3 vertex colors
vArray = []

# For loop that collects rgb values for each vertex & adds them to vArray
for i in range(len(verts)):
    cArray = []
    for c in range(3):
        cIndex = i * 3 + c
        if colors[cIndex] > 1.0:
            colors[cIndex] = 1.0
        cArray.append(colors[cIndex])
    vArray.append(cArray)

    # Check if the vertex color is within the range that is declared by the user
    if not toleranceCheck(vArray[i], vColor, tolerance):
        cmds.select(verts[i], d=True)

I’ve tried to debug where the decrease in performance was coming from (using cmds.timer) but the only thing I could find is that the deselect command itself just seems to take longer.

Any help would be really appreciated.

Thanks,
Sander

If the cmds.select truly is the slow point, you’d want to pull that out of your loop and do it in a single call instead.

So probably add another array called deslect and add the verts that fail your tolerance check to that, then do a cmds.select(deselect, d=True) outside of the loop.

Not sure if it would be faster, but you could probably even use min(colors[cIndex], 1) instead of your if check.

1 Like

@bob.w
That fixed it! :slight_smile:
I had already tried deselecting in a single call but must have done something wrong since I couldn’t get it to work earlier. Also replaced my if statement with min(colors[cIndex], 1) as you suggested.

I’m still kind of curious why moving a mesh would have such a big impact on the deselect command, but my script works fine now so I guess it doesn’t really matter.

Thanks for helping!