Procedurally creating and assigning dynamic material instances to meshes with multiple material IDs

I’m not super experienced with blueprints and I’ve been fiddling with this issue for a little while without finding the solution I’m looking for, so hopefully someone here can help.

What I’m trying to do is modify a material parameter across all the props in a level based on an object’s relative distance from the ground. The ground isn’t one continuous mesh - due to the construction of the level, it has to be made up of separate assets. Those ground assets are also located at different elevations depending on their position in the world. The props are made up of many modular kit pieces grouped together in blueprints.

So what I did first was create a scene component that sets global material parameters based on a world position parameter, but obviously that means that the position will always start from the same spot in the world rather than from the elevation where the ground starts locally, so it’ll work for one area of the level but not in another.

So I moved on to using dynamic material instances, and that’s where I’m getting stuck. I’ve currently got a blueprint with a collection of static meshes that have two materials each. The two materials are shared, so the total materials in the blueprint is 2. I was able to create a dynamic material instance of each material and apply them to the static meshes in the blueprint by manually entering the material slot names and assigning them individually, but the issue is that when creating blueprints that have many static mesh components with many materials each, it’s going to be tedious and time consuming to set them up and maintain them, so I’m looking for a more efficient solution to the problem.

So now I’m trying to procedurally gather the materials contained within the blueprint, create dynamic instances of each, and then assign them back to the meshes in the correct slots. I’ve gotten as far as gathering arrays of the meshes, materials, and material slot names contained within the blueprint, but now I’m stuck at actually creating an array of dynamic material instances and assigning them back to the appropriate slots without messing up the ID’s.

Like I said I don’t have a lot of blueprint experience and am sort of bumbling my way through this, so I could be going about this all wrong. If anyone has any ideas for how to do this or a better solution that I haven’t thought of yet I’m all ears!