Batch Replace Materials with Instances, Keeping Texture Samples Possible?

Morning all.

I am wondering if it is possible to batch replace a tonne of materials with material instances, keeping the two texture samples that exist in the original materials.

Basically, I am creating level LOD’s which create a material for each landscape level within a large open world using world composition.
That is all working fine. However… I need to apply a material function to every material that is created.

So I figured the most practical way would be some form of script that can do it instead as there are several hundred maps that need LOD’s and related materials.

Is this something that is possible with a python script or something similar?

Thanks in advance everyone!