Creating a material graph programmatically

I am writing a plugin to import a custom format.
I am trying to create the material graphs programmatically based on the custom structures from my file, the materials are then applied to the loaded meshes.
Can anyone supply sample code to create a material graph programmatically in c++?
material node + texture node connected to the material base color would be enough to get me started.


I’m not sure exactly but I tried to do this as well some time ago and back then came to the conclusion that this is in fact not possible.

The reason being that materials (as opposed to most of the other blueprints) are not boiled down to generated C++ code but to shader language. There is no direct C++ material representation.

You can however, access material instances, copy and modify them. Just not “code a graph” per se.


You actually can build material node networks from code. Take a look at the speedtree import factory, especially the CreateSpeedTreeMaterial function, that builds a fairly complete material when importing a speed tree model.

Hmm, you could dig down into it, but from what I think thats pretty hard… Why don’t you pre-create a master material which has every feature you want, and put in Material parameters for every this and that you want? That way, you just need to create a dynamic material instance from that master material, and set the parameters…

It would work in the editor probably as an extension, but not in a packaged game.

You have to create a custom Editor plugin (C++), implement a custom asset factory (to add your file extension and import the data using editor) and from there generate the “object” which is read by the material editor.
Lots of work

I created a plugin for the editor to import my custom file and crate instances from template graphs.

thank you all for the replies.