Help! Unreal Engine 5.1.1 programmatically create a UMaterial: deprecated code

Hi, Thank you very much, i was checking the code too and found the same class type.
However i tried to use it like this:

UMaterialEditorOnlyData& Data = *InMaterialToGenerate->GetEditorOnlyData();
...
Data.BaseColor.Expression = TextureRTMaskExpression;

but unreal crash with this error::

Assertion failed: TextureReferenceIndex != INDEX_NONE [File:D:\build++UE5\Sync\Engine\Source\Runtime\Engine\Private\Materials\HLSLMaterialTranslator.cpp] [Line: 6769]
Material expression called Compiler->Texture() without implementing UMaterialExpression::GetReferencedTexture properly

then i tried adding this lines:

UMaterialEditorOnlyData& Data = *InMaterialToGenerate->GetEditorOnlyData();


//Added these two
FMaterialExpressionCollection Expressions = Data.ExpressionCollection;
Expressions.AddExpression(TextureRTMaskExpression);

Data.BaseColor.Expression = TextureRTMaskExpression;

But crash with same error… my question now is, do i need to implement this method ( GetReferencedTexture() ) from UMaterialExpression but how? i don’t think that is needed to create a subclass of UMaterialExpressionTextureSample that implement and override that method… i’m so confused.

The texture isn’t null, and its created in the content browser correctly because i can see it, so the texture is fine, but assigning it to the material expression that i want.

PS. I alredy have this line of code after all the assignment:

FAssetRegistryModule::AssetCreated(InMaterialToGenerate);
Package->FullyLoad();
Package->SetDirtyFlag(true);

// Aggiornamento Materiale
InMaterialToGenerate->PreEditChange(NULL);
InMaterialToGenerate->PostEditChange();
	
FGlobalComponentReregisterContext RecreateComponents;

(I tried to remove this line of code but unreal crash with the same error)

i’ll continue to search in the code or on internet, if i make some progress i’ll post here, in the meanwhile i hope you or someone other know the solution to this problem.

thank you again!