And as a script that ensures unique colors:
fn shuffle arr =
for counter = arr.count to 1 by -1 collect
(
local swapIndex = random 1 counter
swap arr[random 1 counter] arr[counter]
)
fn incrementCounters &r &g &b step =
if (b += step) > 256 do
(
b = 1
if (g += step) > 256 do
(
g = 1
if (r += step) > 256 do r = 1
)
)
fn getElements mesh =
(
local vertCount = mesh.numVerts
local faces = mesh.faces as bitArray
local facesByVert = for v = 1 to vertCount collect #()
for face in faces do
(
local faceVerts = getFace mesh face
append facesByVert[faceVerts[1]] face
append facesByVert[faceVerts[2]] face
append facesByVert[faceVerts[3]] face
)
for face in faces collect
(
local i = 0, element = #(face), elementVerts = #()
while (local f = element[i += 1]) != undefined do if faces[f] do
(
faces[f] = false
local faceVerts = getFace mesh f
for v = 1 to 3 do facesByVert[faceVerts[v]] =
for vertFace in facesByVert[faceVerts[v]] where faces[vertFace] collect
(
append element vertFace
append elementVerts (int faceVerts[v])
dontCollect
)
)
elementVerts as bitarray
)
)
fn assignUniqueElementColor obj op shuffleElements:off =
(
local elements = getElements (snapshotAsMesh obj)
local stepCount = elements.count^(double 1/3) + 1
local step = 255./stepCount
local redArr = shuffle (#(0) + #{1..255})
local greenArr = shuffle (#(0) + #{1..255})
local blueArr = shuffle (#(0) + #{1..255})
local r = 1, g = 1, b = 1
if shuffleElements do
elements = shuffle elements
for element in elements do
(
op.setVertColor obj 0 element [redArr[int r], greenArr[int g], blueArr[int b]]
incrementCounters &r &g &b step
)
)
if isKindOf $ Editable_Mesh then assignUniqueElementColor $ meshop shuffleElements:on
else if isKindOf $ Editable_Poly do assignUniqueElementColor $ polyop shuffleElements:on
If you instead want to bind it to a shortcut, you can save it as a .ms file and use the macroscript creator to make a macro for it.