マテリアルインスタンスのペアレントマテリアルをPythonから変更するとUEがクラッシュするときがある

クラッシュ再現方法1

①NewMaterial1、NewMaterialInstanceConstantを/Gameに作成する

②アクターとしてCubeを生成し、NewMaterialInstanceConstantをアサインする

③ペアレントマテリアルを変更するために、下記コードを実行すると、UEがクラッシュします。

import unreal mat1 = unreal.load_asset("/Script/Engine.Material'/Game/NewMaterial1.NewMaterial1'") mi = unreal.load_asset("/Script/Engine.MaterialInstanceConstant'/Game/NewMaterialInstanceConstant.NewMaterialInstanceConstant'") mi.set_editor_property("parent", mat1)クラッシュ再現方法2

①NewMaterial1、NewMaterial2、NewMaterialInstanceConstantを/Gameに作成する

②アクターとしてCubeを生成し、NewMaterialInstanceConstantをアサインする

③NewMaterial1をNewMaterialInstanceConstantのペアレントとする

④下記のコード1を実行する(NewMaterial2をペアレントとします。クラッシュしないこともあります)

mat2 = unreal.load_asset("/Script/Engine.Material'/Game/NewMaterial2.NewMaterial2'") mi = unreal.load_asset("/Script/Engine.MaterialInstanceConstant'/Game/NewMaterialInstanceConstant.NewMaterialInstanceConstant'") mi.set_editor_property("parent", mat2)⑤クラッシュしない場合、下記のコード2を実行する(NewMaterial1をペアレントに戻します)

mat1 = unreal.load_asset("/Script/Engine.Material'/Game/NewMaterial1.NewMaterial1'") mi = unreal.load_asset("/Script/Engine.MaterialInstanceConstant'/Game/NewMaterialInstanceConstant.NewMaterialInstanceConstant'") mi.set_editor_property("parent", mat1)

再現手順
やりたいことは、Pythonによるマテリアルインスタンスのペアレントの変更です。

そのマテリアルインスタンスを含むスタティックメッシュアクターがレベルに存在する場合、UEがクラッシュしてしまいます。

このクラッシュの回避方法を教えていただきたいです。

Hi, [30 [Content removed]

Sorry about the delay. I was able to reproduce this crash here. Note that there is a specialized library function for changing the parent of a material instance. Unfortunately, though, using that function also results in a crash. For your information, the function is:

// C++ UMaterialEditingLibrary::SetMaterialInstanceParent(UMaterialInstanceConstant* Instance, UMaterialInterface* NewParent)``# Python unreal.MaterialEditingLibrary.set_material_instance_parent(cls, instance: Optional[MaterialInstanceConstant], new_parent: Optional[MaterialInterface]) -> NoneI have filed an internal bug report for this issue, and I will get back to you with the public bug tracking number as soon as I have it.

While this is not fixed, you can work around the crash by implementing your own function in a C++ BlueprintFunctionLibrary as follows:

`#include “MyEditorLibrary.h”
include “MaterialEditor/MaterialEditorInstanceConstant.h”

void UMyEditorLibrary::SetMaterialInstanceParent(UMaterialInstanceConstant* Instance, UMaterialInterface* NewParent)
{
if (!Instance)
return;

UMaterialEditorInstanceConstant* MaterialEditorInstance = NewObject(GetTransientPackage(), NAME_None, RF_Transactional);
MaterialEditorInstance->SetSourceInstance(Instance);

FProperty* ParentProperty = MaterialEditorInstance->GetClass()->FindPropertyByName(TEXT(“Parent”));
if (ParentProperty)
{
MaterialEditorInstance->Parent = NewParent;

FPropertyChangedEvent ParentChangedEvent = FPropertyChangedEvent(ParentProperty);
MaterialEditorInstance->PostEditChangeProperty(ParentChangedEvent);
}
}`I hope this is helpful. Please let me know if this solution works for you, and do not hesitate to ask if you have any further questions.

Best regards,

Vitor

Hi Vitor, thank you and sorry for the late reply. I’ll try out the code you provided—it might take a little time.

Here’s the bug tracker link: UE-269068. It should become available once the devs mark it as public.

Hi! Do you still need assistance with this?