edit material properties in c++

Is there any docs on editing a material’s properties in c++?

Specifically I need to link imported file texture nodes.

How can I, for example, plug “texturename.png” into the opacity mask slot of 'Material1" using pure c++?

Both the UTexture2D and the UMaterial exist, and I have found them in my function using Iterators that search the names.

How would I go about doing these steps? I have:

UTexture *Tex = LoadObject<UTexture>(NULL, "texturename.png" );

Material1->OpacityMask = (*Tex);

But the second line won’t accept a UTexture as a parameter.

C++ is not shader. The graph you make in material editor, generates HLSL code which is used by DX or converted for different library shader language if you something else then DX, which later driver compile for your GPU. C++ is run by CPU where shaders are run (more like placed in rendering pipe) by GPU and can’t be mixed, thats why the property you trying to modify seem to be related to shader compiler rether then modify something in real time.

CPU and GPU code can communicate to each other via uniform varables, which in UE4 are implamented via material parameters and materiual instances allowing to change properties of material on the go.

In C++ (and in blueprint in similar way) you operate them in component instead of material it self, first you create and set instance dynamic using this function (or else you gonna create static instance in editor)

And from generated material instance you call function that set those parameters like SetVectorParameterValue or what gonna interest you the most SetTextureParameterValue. I can’t find them in API reference but they are there in UMaterialInstanceDynamic.

Also way you refrenceing texture seems odd, asset are refrence a little diffrent, you can get asset refrence by right clicking it and clicking “Copy Refrence”. Also referencing assets direclyin C++ are little unsafe, after cook if there no asset used in level refrence your asset it won’t be cook, im not sure how to fix that, but you can make code a lot safer if you making UTexture* editable in editor property, where you can set texture and blueprint or level which use C++ object will refrence that asset and you be 100% sure it will be cooked

Thank you! will check through those links. I am actually planning to put this in a plugin, I want it to run in the editor as part of a batch import, NOT at runtime.
Is it possible to edit the materials in editor via c++, without creating a dynamic instance?

I think the question is related to editor-time manipulation of the material asset itself (in the state it is in before being compiled to HLSL), not adjusting a material on-the-fly. Since you can modify a material asset by manipulating the material graph, and the graph editor is coded in C++, it stands to reason you should be able to alter the material asset procedurally.

As for how, I’m afraid I have no idea. It’s conceivable the necessary functionality is buried way down and not exposed through the public interface of UMaterial. I guess the way to find out would be to look through the engine source code of the material graph editor.

Well in thats case OpacityMask is not UTexture it is FScalarMaterialInput

And it accepting float it seems :stuck_out_tongue: or else i missing something

Also antithing referencing UTexture like this “(*Tex)” which with throw new copy of UTexture object which i don’t think you can do with UObjects

aha, thanks! Forgive my idiocy, but…

How can I get a FScalarMaterialInput to pass it? How can a Texture be a float?

After reading up a bit…

I think i need a UMaterialExpression:

UMaterialExpression *MatExpr;

But I can’t figure out how to get an FScalarMaterialInput from it, or to link my UTexture.

FScalarMaterialInput its struct, so you initiate it by declaring it:

FScalarMaterialInput SomeName;

And you fill it with needed data :stuck_out_tongue: there “Constant” varable there that accepts template input type (in this case flot) maybe you need to set it

ok! FScalarMaterialInput is:

struct FScalarMaterialInput : FMaterialInput<float>
	ENGINE_API int32 CompileWithDefault(class FMaterialCompiler* Compiler, EMaterialProperty Property);

And I have a UTexture, that is an imported file. How can I link the two??

Thanks again.