Get DynamicMaterialInstance from Mesh?

Is there a way to get DynamicMaterialInstance from a mesh instead of a regular Material? I need to read one of it’s scalar values i am often setting.

The mesh material is a Dynamic Instance for sure, there is just no obvious way to get or cast it. (cast fails)

Please do not post answers like “save it in a variable”! I care about my RAM performance and would avoid any unnecessary variables.

bumpy bump

Hey :

I found a couple of documentation sources on our docs.unrealengine.com that might be of help. If this isn’t quite what you are looking for, please let me know and I will try my best to assist you. Here are the links to Instanced Materials and Material Parameter Collection (There are also other links on these pages that might might be of better help):

Regards,

it does not answer the question in any way :frowning:

a variable will be the best bet. a single variable really doesnt have that much cost anyway. most games have thousands of variables. you will never have a 100% optimized game, everything is a trade off, eliminating this one variable may be one less thing in memory but it may well cost you on the cpu instead.

You can read it like this:

The mesh material is a Dynamic
Instance for sure, there is just no
obvious way to get or cast it. (cast
fails)

Providing the static mesh has been assigned a MID, the above cast will work just fine. To be perfectly honest, you definitely should store the MID reference and use that instead - it is way more performant than casting…

228668-capture2.png

Please do not post answers like “save
it in a variable”! I care about my RAM
performance and would avoid any
unnecessary variables.

Not sure why you would even say something like this. Perhaps you should look into what a pointer is to understand its memory footprint. If performance is of utmost importance, consider abandoning blueprints altogether and do things directly in C++. For example, the cost of that cast is most likely an order of magnitude larger that storing a pointer.

Anyhow, both are borderline negligible unless you’re firing it in a loop hundred times per frame.

As you see on the screenshot, it fails for my case. Even though i have assigned it a MID (material instance dynamic) at construction time using CreateDynamicMaterialInstance node.

i can argue regards keeping the reference to MID, because reference exists in RAM 24/7, while the operation that demands it only occurs rarely on special events. doesn’t worth it to store a variable for that.

Not sure why you would even say something like this. Perhaps you should look into what a pointer is to understand its memory footprint. If performance is of utmost importance, consider abandoning blueprints altogether and do things directly in C++. For example, the cost of that cast is most likely an order of magnitude larger that storing a pointer.

I come from the C++ world and i can definitely state i understand how pointers work.
a pointer is and implicit int type with 8 bytes length on x64 platforms, means if i have 1000 instances of an actor with this (mostly) idle pointer, i waste 8kb of RAM for practically nothing. should you use it more often and you end up with tens of wasted megabytes that could be used for other more important stuff.

Is the material set to be used with Skeletal Meshes? It’s a tickbox in the material editor.

yes, it is

I think I know why it’s failing for you. You might be creating the dynamic material directly in the object’s construction script. While this was possible in 4.15 and below (maybe even in 4.16), it’s no longer supported.

Instead, wrap the DMI creation in a function or use an event and call that in the construction script.
That should do it.

edit: Nah, scratch that. It’s working as is. Out of the box. I was thinking about widgets, you can no longer construct them there.

In case it still does not work:

I tested the MID in an empty project with a skeletal mesh and it seems to work fine (get scalar from a dynamic material assigned to skeletal mesh). Maybe you can show us how you create and assign the material. Is there anything out of ordinary there?

Is there anything that’s different from this?

This prints 1.25 for me which I set in the material:

228724-capture2.png

wow. i am feeling so dumb. the resolutions are always so obvious…

228780-screenshot-6.png

thanks for your help, it pushed me to recheck it second time.

We’ve all been there, done that. Glad to hear you have it working!

Good luck.

1 Like