Setting SamplerType crashes when creating new material from code

Hi,

I’d like to make custom material from editor, everything works perfectly fine except the one thing - Setting SamplerType crashes editor with assertion:

Assertion failed: TextureReferenceIndex != INDEX_NONE [File:D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\Materials/HLSLMaterialTranslator.h] [Line: 3992] Material expression called Compiler->TextureParameter() without implementing UMaterialExpression::GetReferencedTexture properly

Full stacktrace: SamplerType_Stacktrace - Pastebin.com

Short reproduced code:

void ABugCoder::CreateMaterial()
{
	UPackage* Package = CreatePackage(NULL, TEXT("/Game/SomeFolder/M_MyMaterial"));
	UPackage* OutermostPkg = Package->GetOutermost();
	UMaterialFactoryNew* Factory = NewObject<UMaterialFactoryNew>();
	UMaterial* Mat = Cast<UMaterial>(Factory->FactoryCreateNew(UMaterial::StaticClass(), OutermostPkg, TEXT("M_MyMaterial"), RF_Standalone | RF_Public, NULL, GWarn));
	FAssetRegistryModule::AssetCreated(Mat);
	Mat->MarkPackageDirty();
	Package->SetDirtyFlag(true);

	UMaterialExpressionTextureSampleParameter2D* NormalTexture = NewObject<UMaterialExpressionTextureSampleParameter2D>(Mat);
	NormalTexture->SetParameterName(TEXT("NormalParam"));
	NormalTexture->Texture = LoadObject<UTexture2D>(NULL, TEXT("/Engine/EngineMaterials/FlatNormal"), NULL, LOAD_None, NULL);
	NormalTexture->SamplerType = EMaterialSamplerType::SAMPLERTYPE_Normal; // Line that will affect in crash in PostEditChange(); 
    // it works without this line, but need to set SamplerType manually (from material editor) in created material
	Mat->Normal.Connect(0, NormalTexture);

	Mat->PostEditChange();
}

Hello,

We’ve recently made a switch to a new bug reporting method using a more structured form. Please visit the link below for more details and report the issue using the new Bug Submission Form. Feel free to continue to use this thread for community discussion around the issue.

https://epicsupport.force.com/unrealengine/s/

Thanks

I;ve submited it. Got an reply “it’s not a bug”, but no further answer.
It’s still an issue and I’ve completely no idea how to solve it