Steps to Reproduce
I’m developing a plugin that parses customized material files and creates material assets. I’ve prepared a parent material asset with layers, a material layer asset with 4 textures as inputs (color, metalness, normal, roughness), a material blend asset with one mask texture as input. So for each newly-created MaterialInstanceConstant (MIC),I tried to add several layers in layer parameter, and assign the material layer asset and blend asset for each layer, then assign the corresponding textures that are loaded in advance.
[Image Removed]
The material instance from editor is what i’m trying to create in C++.
`FString MaterialLayerPath = TEXT(“/Game/NewMaterialLayer.NewMaterialLayer”);
FString MaterialLayerBlendPath = TEXT(“/Game/NewMaterialLayerBlend.NewMaterialLayerBlend”);
FString LayeredMaterialParent = TEXT(“/Game/NewMaterial.NewMaterial”);
UMaterial* MaterialFather = LoadObject(nullptr, LayeredMaterialParent);
UPackage Package = CreatePackage(MaterialInstancePackageName);
UMaterialInstanceConstant NewMIC = NewObject(Package,
UMaterialInstanceConstant::StaticClass(), FName(MaterialInstanceBaseName),
RF_Public | RF_Standalone);
NewMIC->PreEditChange(nullptr);
if (NewMIC != nullptr)
{
NewMIC->Parent = MaterialFather;
FMaterialLayersFunctions LayersValue;
auto& LayersRuntime = LayersValue.GetRuntime();
UMaterialFunctionInterface* BaseLayer = LoadObject(nullptr, MaterialLayerPath);
UMaterialFunctionInterface BlendLayer = LoadObject(nullptr, *MaterialLayerBlendPath);
LayersRuntime.Blends.Add(BlendLayer);
LayersRuntime.Layers.Add(BaseLayer);
LayersValue.AddDefaultBackgroundLayer();
LayersValue.AppendBlendedLayer();
NewMIC->SetMaterialLayers(LayersValue);
bool bDirty = NewMIC->MarkPackageDirty();
NewMIC->PostEditChange();
FAssetRegistryModule::AssetCreated(NewMIC);
bool bSaveSuccess = UEditorAssetLibrary::SaveAsset(MaterialInstancePackageName, true);
}
StaticMesh->SetMaterial(Index, NewMIC);
}`The first problem is I couldn’t create the MIC by failing some validation during SetMaterialLayer. There must be some data requirement for FMaterialLayersFunction I didn’t realize.
The second problem is I don’t know how to assgin the material layer object and material blend object to the layer parameter, and how to assgin textures to those.