Download

Simple changing of base color for static mesh/material from C++

Ok I have tried everything I can think of and everything I have found on the web for changing the base color of a material for a static mesh from C++ and cannot get it to work.

Basic creation code is normal, create the static mesh component, load ‘/Engine/BasicShapes/Cube.Cube’ mesh and assign it to the component. That works fine.

I get the material 0 for the mesh, create UMaterialInstanceDynamic from it and assign the mesh back to the static mesh component.

The thing is no matter what change I make to the UMaterialInstanceDynamic, it never has any effect on what is displayed, it still just displays the original mesh and material intact.

Can anyone point to a simple (working) example of just changing the base color of a static mesh ? I just want to highlight my box when something happens.

Thanks,
Jason

Hello Jason!

Which version of UE4 are you using? Also, are you attempting this from Blueprint or in C++?
UMaterialInstanceDynamic has always been a bit moody, especially before 4.7.

Some things to watch out for though is to create the material dynamic instance in begin play and then using that precreated instance. Also make sure that you are setting parameters on the MID that actually exist in the material.

Best regards,
Temaran

Static Mesh -> Set Mobility to Movable!

It is a frequently encountered issue with Static mesh components (especially when spawned in code) that you absolutely cannot change them at all visually (changing mesh or materials) unless you specifically make them movable!

The reason this comes up so much is cause the default for spawned components / static mesh actors is static,**** so nothing gonna happen till you set it to be movable**** :slight_smile:



//Mobility
GetStaticMeshComponent()->SetMobility(EComponentMobility::Movable);


THEN you can change the materials and the mesh as often as you please!

Please note as Temaran mentioned, that you must have already spawned the DynamicMaterialInstance so it is ready to be used on the static mesh component AFTER setting mobility to movable.



GetStaticMeshComponent()->SetMaterial(0,YourDynamicMaterialInstance);


:slight_smile:

Rama

Great, thanks guys I will look into that.

I got something working a different way although it probably ended up being the same in the end.

Instead of loading the static cube mesh then getting the material and trying to change it, I loaded a material separately from BasicShapeMaterial. Then I noticed “GetAllVectorParameterNames” was suddenly returning a ‘Color’ value so I set that color value, set the material and it worked. The material that was I was getting from “GetMaterial” from the loaded static mesh was not returning any values for ‘GetAllVectorParameterNames’. I don’t know what that means but interesting none the less.

I will definitely remember the mobility trick, thanks a bunch.

I just tried the Mobility trick using the material (GetMaterial(0)) that comes with the loaded Cube static mesh and was still not able to change the material for the static mesh component. My feeling is that the material that is coming with the Cube static mesh may just not be changeable for some reason.

I am on 4.7.2 by the way.

Thanks again guys.

Further research showed that the Cube basic shape uses the ‘WorldGridMaterial’ which has no parameters in the material editor but the BasicShapeMaterial has ‘Color’ and ‘Roughness’ in the editor which also shows up from inside the code.

So I guess if I want control over any aspect of a material I need to one that has ‘param’ boxes in the designer to make it available via code.

Just in case someone else runs into this :slight_smile:

Jason