Search code examples
pythonmaya

Assign random color to a vertex inside grey scale range


I am trying to assign a random greyscale color value to a vertex of a mesh.

By using cmds.polyColorPerVertex(rgb=()) I can assign any specific color to a vertex but I can't seem to figure out how to randomize that color value yet lock it into a grayscale range.

In the end a need each time for a vert to be of a different greyscale value.

Thank you


Solution

  • cmds.polyColorPerVertex() takes an rgb tuple in the form (r, g, b), where each element should be a float in the range [0, 1].

    "Greyscale", in this context, just means that r == g == b.

    Accordingly, we can just use Python's random number generators to achieve this:

    brightness = np.random.rand() # generates a random float in the range [0, 1]
    rgb = (brightness, brightness, brightness)
    
    cmds.polyColorPerVertex(rgb=rgb)