Replace shared materials with material instances for many meshes.

Say I have an actor with 3 attached StaticMeshActors. I want to ‘modify’ the materials for all of them at runtime, in a blueprint, for example changing the opacity.

I could get the attached actors, and RorEach create a new Dynamic Material Instance with a modified opacity. However, if two of the meshes are using the same material, I’m doing unnecessary work. This is fine with 3 meshes and a single duplicate material, but not fine with 1500 meshes using 100 materials.

What I really want to do is:

  • Find all the attached actors.
  • Find the set of materials they use.
  • Create a dynamic instance for each material in the set.
  • Override the material for each attached actor, using a map from original material to the dynamic instance for it.

Is this possible using Blueprint Scripting?