Assign panel material range by "Z" Value

Hi, I’m trying to assign a material to a curtain panel based on it’s height on the building. The goal is to create a random gradient of panels from one material at the base to another at the top.

Any help would be much appreciated.

I am going to assume this, or at least something similar, has already been done many times but I haven’t been able to find examples so maybe I am overthinking it.

My steps:
0. Create 5 materials.
1a. Discover/assign the “Z” point value from the panels to determine it’s height
1b. Sort/ filter the panels by value into quarters of the overall height
*(this is where I get lost, and the script gets messy)
2. Take each “quarter” of panels and assign each materials to a specific percentage of those panels. 40% material 1, 20% material 2, 10%, material 3 etc.

This is where I am lost

Here is the *.dyn
Curtain Panels_ColourByZ.dyn (88.8 KB)