Creating material nodes from c++ shows no changes on preview sphere.

On existing empty material, creating UE MaterialEditor nodes from c++ wont show any changes on material preview even if nodes are properly created and connected.

But if existing material had some connected nodes the update works properly.

Below is my code, but I tried a lot of other things like RebuildGraph(), link, refresh… So much in fact that even AI gave up.

            SelectedMaterial->Modify();
            SelectedMaterial->PreEditChange(nullptr);
            
            // create nodes
            UMaterialExpression* a = UMaterialEditingLibrary::CreateMaterialExpression(SelectedMaterial,UMaterialExpressionTextureSample::StaticClass());
            UMaterialExpressionTextureSample* TextureSample =Cast<UMaterialExpressionTextureSample>(a);
            SelectedMaterial->AddExpressionParameter(TextureSample,SelectedMaterial->EditorParameters);
            TextureSample->Texture = TextureGroups[SelectedMaterialName][0].Texture;

            // connect
            SelectedMaterial->GetEditorOnlyData()->BaseColor.Connect(0,TextureSample);
            
            SelectedMaterial->PostEditChange();
            UMaterialEditingLibrary::RecompileMaterial(SelectedMaterial);
1 Like

Did you ever figure this out? I found that flicking a property with human touch lets say Two-Sided in the material graph then pressing save works. I also am able to move any node a few pixels and hit save and it updates. I’m having this exact issue except the material is generated through Python. I have tried using a Blueprint Editor Utility to flick a property, recompile, and save, No luck. It seems doing something inside the actual Material Graph triggers some kind of internal state that forces the editor to re-evaluate which does not happen when done via automation.

From C++ you might be able to do:
Material->Modify();
Material->PostEditChange();
Material->MarkPackageDirty();

In my case I am stuck with Python and am going crazy :melting_face: