python driver - change color in multiple object, not random

hi
i’m looking for a method to change the color of the elements in array with one material, but i think is impossible (for me and with the default array modifier).
but i find this workaround:

  • the material use the node “object info - object index” for choose the color in a color ramp;
  • the pass index of the object is controlled by a driver with a python expression;
  • the expression read the name of the object (in the example read the name of the parent but is the same thing), bring the last three characters (the name must be like ‘name.000’) and convert it in number, and put it in the pass index;
  • and instead of the array i use the clone (alt+d)

and here the expression (im not a real coder/programmer so be careful)

import bpy

def driver_mat(s):
    scn = bpy.context.scene

    obName=s.parent.name
    
    #the part mof the name after the '.'
    numStr=obName.split(".")[1]
    numInt=int(numStr)
    #return numInt
    #here the static part
    #in material the object index must be divided
    #for the greater pass index
    #(all the value must be from 0 to 1, for the color ramp)

    #from here the dinamic part
    d=(1/35) #delta (35 object)
    c=75 #frames of the loop
    f=scn.frame_current #the current frame

    v=numInt*d
    vfi=v/(1/c)
    vf=(1/c)*((f+vfi)%c);
    return vf*c

# Register it to driver namespace under name 'driver_mat':
bpy.app.driver_namespace['driver_mat'] = driver_mat

and here a video:

i hope this will inspire someone
ciao