Artyu
Artyu

Reputation: 19

How to access a material instance variable from a blueprint object in Unreal Engine?

I have a texture material that changes between 2 textures. This change can be controller by a variable. I've made a material instance from this material as well as a blueprint that has an object in it, to which this MI texture is applied to. I was hoping to update the materials variable parameters through the blueprints event tick but I am having difficulties figuring out how to access this parameter.

Would anyone know what has to be done in blueprint to access parameters and change them?

Upvotes: 0

Views: 16038

Answers (3)

alexpanter
alexpanter

Reputation: 1578

How to get it working with landscape materials

The answer by @Fritz is the correct path, unless you are working with a landscape material.

Here, at least in UE5, the base material must use a Material Parameter Collection. Then, after creating a material instance from this base material, you would call SetScalarParameterValue (e.g.) on the parameter collection instead. For some reason, dynamic material instance changes does not affect the landscape.

Also, the landscape actor has a field "Use Dynamic Material Instance", which I have found having no effect. But maybe there's a secret "correct way" of using it.

How to use parameter collection (pc.)

  1. Create an actor in the Content Browser somewhere (type: Material Parameter Collection)
  2. Open it and add named fields corresponding to what a blueprint graph should be able to modify
  3. Add references to this pc. inside the base material

You can add a "Static Switch Parameter" that says whether to use the pc., or to use normal parameters. This enables you to test stuff with a material instance as well during development. But this switch must then be set to use pc., otherwise blueprint changes to the material will have no effect.

Further design notes

It's messy to use direct references to actual scene actors inside a blueprint graph. If the blueprint is used in other maps where those actors are not in place, or the actors are changed/modified, bad things may happen.

So using a parameter collection makes a lot of sense, since it decouples blueprint logic from actors in the current world. At least in my book.

Upvotes: 0

lernie
lernie

Reputation: 21

Materials can be accessed in Blueprint by creating a Material Instance and then storing a reference into a variable (if you plan to use it int eh future).

I think it should added, that within a BP, a Material is accessed first by the Get Material node, then iterate each returned Material using For Each Loop. For each element, Create Dynamic Material Instance.

Upvotes: 0

Fritz
Fritz

Reputation: 10045

Materials can be accessed in Blueprint by creating a Material Instance and then storing a reference into a variable (if you plan to use it int eh future).

Once you create a Material and define a parameter by giving it a name, create a Material Instance based on that Base Material. This is an asset that allows for quick updates by caching part of the underlying pipeline.

Once you've done so, you'll need to use a CreateDynamicMaterialInstance, store the ootput, set it in the corresponding material slot for the Mesh and then use the SetParameter nodes (Vector, Scalar, etc.).

Once you've done that, you're good to go.

You can also get an Instance from a slot directly if the Base Material is not going to change.

Upvotes: 1

Related Questions