How to Create material using C++ and apply it to static mesh?

Ok to answer to each one of your questions.

  1. How to Create a material in C++.
    I Assume you mean instantiate a material here. Generally you will want to do it when constructing you object with:

    UMaterial* ExampleMaterial = nullptr;

    ConstructorHelpers::FObjectFinderMaterialAsset(TEXT(“Material/MATERIAL PATH’”));
    ExampleMaterial = MaterialAsset.Object;

  2. UMaterialInterface vs UMaterial

You should not need to convert from UMaterial* to UMaterialinstance* since UMaterial inherits from MaterialInstance. If for some reason the code is complaintning simply cast to it Cast(YourMaterialPointer).

Material interface is an interface that allows you to pass any material type into functions that use materials as arguments (like in your case), since there are several material types (UMaterialInstance, UMaterialConstantDynamic, UMaterialInstanceDynamic, etc).

The function takes Material Interface as a value for the reason stated above (so it can receive any material type).

Hope this information helps you. If this is the information you needed please remember to mark this answer as the correct one so other people can find it. Make it a great day!

Hello Everyone,

I want to apply material to my Static mesh using C++, now i know that there is “StaticMesh->SetMaterial()” function, but the problem is this function is taking UMaterialInterface as a parameter and i could not find any way to convert my UMaterial UnrealMaterial* to UMaterialInterface ?

Another problem is, what is the difference between UMaterialInterface and UMaterial ?? And why instead of taking UMaterial in StaticMesh->SetMaterial() it is taking UMaterialInterface ?

So anyone know how to do it ?

Thanks in advance.

Hey @Bariudol ,

Thanks for the answer i can set the material using StaticMesh->SetMaterial(),
do have any idea on how to set the color for that material which i can set to red or something which will update in editor?

In that case you need to create a MaterialInstanceDynamic based on a material that has the color as a paramter. It would go something like this:

UMaterialInstanceDynamic* ExampleMID = UMaterialInstanceDynamic::Create(ExampleMaterial);  
ExampleStaticMesh->SetMaterial(ExampleMID, 0);  
FLinearColor Red(FLineaerColor::Red);  
ExampleMID->SetVectorParameter("ParameterName", Red);  

This is just a guideline for what you want to do. Look into the methods available for the MaterialInstanceDynamic class here http://api.unrealengine.com/INT/API/Runtime/Engine/Materials/UMaterialInstanceDynamic/