Hi,
I’m trying today to link the material of a mesh inside my C++ to manipulate its properties from here. Unfortunately I can’t get to make it work.
I tried in my header :
class UMaterialInstanceDynamic* MI; //-> works
class UMaterialInstance* MI; //-> Soft crash the editor, crash the game
Then, in my function I tried :
float t = 0;
MI = NewObject(); //compile without errors
MI->Create( Mesh->GetMaterial(0), this ); //no effect
MI->Parent = Mesh->GetMaterial(0); //no effect
MI->GetScalarParameterValue(TEXT("blend_mask"), t);
elog(t);
So, the GetScalar function only work (I get my correct value) if I use a Material Instance (but it crash the editor), and if I use a Material Instance Dynamic the GetScalar return me 0.
I’m a bit lost here, while one would works but crash and the other one wouldn’t work at all ?
By the way, why can I edit directly the “Parent” variable and not use the SetParentInternal() function (since it is protected) ? That seems strange, shouldn’t be the inverse ?
Since I stuck on this, I tried to see if I could setup my idea with Blueprints. Unfortunately I’m hitting the same problem. How could I convert the MaterialInterface to a MaterialInstanceDynamic ? It seems there is no function that would allow me to cast this or convert it.
You want to use the factory function UMaterialInstanceDynamic::Create and provide the material you’d like to use as the parent. In Blueprint you’d use a function like CreateMIDFromMaterial or CreateMIDForElement.
The Parent member being a public variable is legacy from UE3 and I’d like to clean that up. It must be changed by calling SetParentInternal and that function is intentionally non-public as it must only be called in-Editor on MICs and may not be called after creation on MIDs.
1 Like
I see, thanks. However can you clarify me if the following is the correct way to reference a material ? Because I tried it and it doesn’t return any value from the original material (it stays at 0 while I’m expecting 0.353 as I typed it in the editor).
MI = NewObject();
MI->Create( Mesh->GetMaterial(0), this );
The function is static, so what you actually want is something like this:
UMaterialInstanceDynamic* MI = UMaterialInstanceDynamic::Create(Mesh->GetMaterial(0),this);
Why C++ allows calling static member functions as if they were methods is beyond me 
Thanks a lot, I got it working properly now ! 