How to automatically color cubes depending on their height (+setting keyframes)?

Hi there,

I’m facing this problem and wanted to ask you, as I wasn’t able to find a solution yet.

I want to create a 3d-bar chart, where each bars continously changes its height over animation time. The chart holds several hundred single bars, so an automatic solution is needed. I wrote a script that imports the data and creates cube objects according to their specification. Keyframes for the z-axis (cube height). So far everything works out pretty well.

Now, I want to gradually color my bars according to their height from red to green. Lowest z-values = dark red color, highest z-values = green color. I attached a picture for the type of color ramp I would like to use. As the height of each bar changes over animation time, also its color should change accordingly.

Do you have any ideas on how to approach this?

Thanks in advance for your help!
best,
Julian


Maybe it could be done with material nodes instead?