Sharing Dynamic Material Instances

I have an actor with many attached StaticMeshActors. I want to modify the materials for all of them at runtime. I could get all the attached actors, and ForEach create a new Dynamic Material Instance, and modify that instance as desired. However, if I have 1500 meshes sharing 100 materials, I really only need to create 100 Dynamic Material Instances, not 1500.

I’d appreciate any advice on how to construct a Blueprint script that:

  • Creates a map/hash/association that can associate dynamic instances to materials.
  • ForEach of the attached actors, finds the material used.
    • If there is not already a dynamic instance associated with the material, it
      • creates a dynamic instance,
      • customizes it,
      • associates it in the map/hash,
      • and sets the actor to use it
    • If there is already a dynamic instance, sets the actor to use it

Alternatively (less optimal, but maybe easier to author?) it could be:

  • Find all the attached actors (1500 of them)
  • Find the unique set of materials they use (100 of them)
  • ForEach of the materials, create a Dynamic Material Instance with my own customizations.
    • Store these in a map/hash from original material to dynamic instance.
  • ForEach of the attached actors, replace the original material with the associated dynamic instance.

when you create the dynamic material instance, right click the bluepin on the right and promote it to a variable. Then set that variable in the foreach loop for all of them instead of creating a new one everytime. Any change you make the parameters in that variable will affect all of them.

Thanks for your reply. Can you be more explicit? Imagine that I have three meshes, A, B, and C. Meshes A and C use material M1, mesh B uses material M2. In my ForEach loop, when I get to C, how do I find out that there’s already a dynamic instance based on M1 and apply that material instance to C, instead of creating a new dynamic instance and variable?

What I understand you to suggest is the following, which (AFAICT) makes a new value for the variable each loop. How do I associate the dynamic instance with the original material?