Working with more than 10k cubes

Hey I am trying to build something like this.

The problem I am having is performance issues when adding a lot of cubes, blender slows down if you have too many objects.

What I have tried:

  • Using the Array modifier. Works fine until its a single object but when I try, bpy.ops.mesh.separate(type='LOOSE') it is super slow.
  • Creating only 1 cube and copying that and changing the location of that copy and linking it back to the scene. This works only upto 10k cubes but anything after that is very slow.

It seems like I need to keep them all as one single object if I want performance to work with more cubes. But how can I apply different materials to different cubes, like the scene above.

you need to use instances, either manually (with collection instances), or with geometry nodes/particle systems