# Get DynamicMaterialInstance from Mesh?

**URL:** <https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196>\
**Category:** Asset Creation\
**Tags:** casting, get, unreal-engine, question\
**Created:** [January 22, 2018, 12:24am UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196 "2018-01-22T00:24:18Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 22, 2018, 12:24am UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/1 "2018-01-22T00:24:18Z")

</div>

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.**

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 26, 2018, 12:39pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/2 "2018-01-26T12:39:28Z")

</div>

bumpy bump

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_d33fe9f7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_d33fe9f7](https://forums.unrealengine.com/u/anonymous_user_d33fe9f7)\
**Post date:** [January 26, 2018, 8:46pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/3 "2018-01-26T20:46:20Z")

</div>

Hey :

I found a couple of documentation sources on our [docs.unrealengine.com](http://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):

> **[Material Parameter Collections](https://docs.unrealengine.com/5.1/en-US/using-material-parameter-collections-in-unreal-engine/)**
>
> Assets that store an arbitrary set of Scalar and Vector Parameters which can be referenced in any Material.

> **[Instanced Materials](https://docs.unrealengine.com/5.1/en-US/instanced-materials-in-unreal-engine/)**
>
> Hierarchical Material relationships that you to alter behavior and appearance of child Materials without shader recompilation.

Regards,

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 27, 2018, 8:50pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/4 "2018-01-27T20:50:28Z")

</div>

it does not answer the question in any way ☹

---

<div class="post-metadata">

**Author:** ![ThompsonN13](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@ThompsonN13](https://forums.unrealengine.com/u/ThompsonN13)\
**Post date:** [January 27, 2018, 9:09pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/5 "2018-01-27T21:09:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [January 28, 2018, 8:37am UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/6 "2018-01-28T08:37:20Z")

</div>

You can read it like this:

 ![228667-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/9/f/79fb1e03bbf41b4b9299e1e858bf48abcbcb4a50.png)

> 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](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/1/1/5110160559ff0f68185f230b580609e0aa6d0451.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.

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 28, 2018, 12:35pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/7 "2018-01-28T12:35:25Z")

</div>

![228679-screenshot-5.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/4/6/446ceefd8a17b89b6c76ca6b564fd8d8428b379a.png)

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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [January 28, 2018, 3:26pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/8 "2018-01-28T15:26:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 28, 2018, 4:40pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/9 "2018-01-28T16:40:03Z")

</div>

yes, it is

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [January 28, 2018, 5:39pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/10 "2018-01-28T17:39:31Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [January 28, 2018, 5:55pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/11 "2018-01-28T17:55:13Z")

</div>

Is there anything that’s different from this?

 ![228721-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/6/c/c6c3067beec897c1f4f69cf879647488d5bce8d1.png)

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

![228724-capture2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/8/8/e881797e9be4996e49713951bdb359a2bed81a8a.png)

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 29, 2018, 11:47am UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/12 "2018-01-29T11:47:08Z")

</div>

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

![228780-screenshot-6.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/e/a/9eaccd5a478feecb462f29b36053bf9997794ff2.png)

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

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [January 29, 2018, 2:03pm UTC](https://forums.unrealengine.com/t/get-dynamicmaterialinstance-from-mesh/415196/13 "2018-01-29T14:03:16Z")

</div>

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

Good luck.
