Adding Create Dynamic Material Instance node and setter node to actor blueprint trough C++ ?

Hey. I’m trying to create a C++ function that adds a Create Dynamic Material instance node and the return value of that node as a set node in a given blueprint.

The function could look something like this:

417nwWn

But could have other parameters and return value as well if that is more reasonable.

What the function should generate in the given blueprints graph is something like this:

As you can see it adds the create dynamic material instance node, sets the parent node to M_Test (the name of the material it is creating a material instance of), and promotes the return value to a variable trough a setter node. The resulting dynamic material instance then becomes a variable in the blueprint.

I’ve tried to create this for a few days now in several different ways. I get as far as creating the nodes and connecting them, but having trouble actually getting the dynamic material instance variable to be added to the blueprint and be blueprint editable.

To not burden you with my failed code I’d figure I’d just ask how you guys would create such a function from scratch. Anyone have any ideas on how to do this? All help is greatly appreciated!

Ps. If you think seeing my broken code is helpfull I can post it. But it doesn’t really work anyway.

Hi @Putemonsteret, how are you? Hope you’re doing great!

From what I understand, you’re trying to create a node (from C++) where you pass a material as input, and then internally (in C++) the class creates a Dynamic Material Instance from that input material, and then stores it (for example in a TArray of Dynamic Material Instances), and later exposes that array to Blueprints (with the TArray marked as UProperty(EditAnywhere)) so that you can access and edit the material instances from Blueprint.

Is that correct? If that is what you want to achieve, you can store all the new dynamic material instances inside a exposed property, something like this:


// On the .h
UPROPERTY(BlueprintReadWrite)
TArray<UMaterialInstanceDynamic*> MID;

// On the .cpp
void AddDynamicMaterialInstanceNodesetupToActor(class UMaterialInterface* Parent, FName OptionalName, EMIDCreationFlags CreationFlags)
{
    if (UMaterialInstanceDynamic* NewDynamicMaterial = 
    UKismetMaterialLibrary::CreateDynamicMaterialInstance(this, Parent, OptionalName, CreationFlags))
{
    MID.Add(NewDynamicMaterial );
}
}

Oh thanks for the suggestion, I’ll look in to that when I get back to it tomorrow. :smiley:

Essentially what I want to do is just make a C++ function that sets up a working blueprint for another person that is going to continue to code in blueprints and not C++. In the larger scope it goes trough the blueprints of a lot of actors in the project and creates the node setup in the blueprints automatically. So it’s not nesesarily C++ code that needs to do stuff at runtime but rather a function that runs trough a plugin button in the editor that sets up the blueprints for you.

For example:
The user might have an unreal engine project with 6 blueprint actors. The plugin would then find those 6 blueprint actors and set up the above variable and node setup in the graphs of all those 6 actors. The plugin already has a lot of this functionality. It’s just setting up these specific nodes and variable that I’m struggling with.

Thanks for the reply!