# Loading mesh from .babylon file with referenced Material not on the file

**URL:** <https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193>\
**Category:** Questions\
**Tags:** material\
**Created:** [May 9, 2022, 12:27pm UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193 "2022-05-09T12:27:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hschmiedhofer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hschmiedhofer/32/19691_2.png) [@hschmiedhofer](https://forum.babylonjs.com/u/hschmiedhofer)\
**Post date:** [May 9, 2022, 12:27pm UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/1 "2022-05-09T12:27:25Z")

</div>

I am searching for a way to import a mesh from a .babylon file that has property `materialId` set to some material that’s not present in the .babylon file. The reason for doing so is that i want to load the material from somewhere else as soon as i really need it. In the meantime, the mesh should just hold a reference to it.

However, if the importer cannot find the referenced material, the actual material set on the loaded mesh defaults to some fallback, leaving me unable to know what material should be assigned to my mesh later on.

I’d really appreciate some pointers into the right direction here.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [May 9, 2022, 1:21pm UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/2 "2022-05-09T13:21:14Z")

</div>

Hi,  
It might be odd to say but why don’t you set an id to your mesh? Since the material is not there, obviously the materialID will also not export (i guess). You can next look to all meshes with this ID (not unique ID, just mesh.id) and assign the corresponding material, no?

---

<div class="post-metadata">

**Author:** ![hschmiedhofer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hschmiedhofer/32/19691_2.png) [@hschmiedhofer](https://forum.babylonjs.com/u/hschmiedhofer)\
**Post date:** [May 16, 2022, 9:13am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/3 "2022-05-16T09:13:38Z")

</div>

Thanks for your answer, @mawa. What you are suggesting would require some additional mapping, if I understand correctly. What I want to do is to import the mesh along with information about the missing material, such that it can be loaded from somewhere else, later on.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [May 17, 2022, 8:03am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/4 "2022-05-17T08:03:41Z")

</div>

> [@hschmiedhofer](#):
>
> What you are suggesting would require some additional mapping

Not that much. Not more than setting a materialID.  
Just browse through your meshes (or selection of meshes) and add property:

mymesh.id = “mymeshid”;

Next you can call all from ’ mesh.id == “mymeshid” ’ and assign a material to them.  
Looks pretty straight forward to me.

---

<div class="post-metadata">

**Author:** ![hschmiedhofer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hschmiedhofer/32/19691_2.png) [@hschmiedhofer](https://forum.babylonjs.com/u/hschmiedhofer)\
**Post date:** [May 17, 2022, 11:09am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/5 "2022-05-17T11:09:54Z")

</div>

Sorry, @mawa, I do not fully understand what you’re trying to say.

When importing a mesh from a .babylon file, i need to know the `materialId` of the missing material (i.e. the material that was referenced by the mesh, but wasn’t defined in the .babylon file). Importing these meshes leaves me without clue about what materials they actually need. This information is what i want to add to these meshes at loading time, because i will want to load these materials from somewhere else later on.

---

<div class="post-metadata">

**Author:** ![mawa](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mawa/32/12384_2.png) [@mawa](https://forum.babylonjs.com/u/mawa)\
**Post date:** [May 17, 2022, 11:13am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/6 "2022-05-17T11:13:49Z")

</div>

I was simply suggesting to assign your meshes an id (in babylon). Instead of materialID, use mesh.id on your meshes. Next (outside bjs) you look for all meshes with id and assign the corresponding material. I don’t see any difference in this except you are setting your id to a mesh that exists (in BJS) as opposed to a materialID for a material that does not exist in BJS.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 17, 2022, 11:17am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/7 "2022-05-17T11:17:11Z")

</div>

Are you able to export your scene again? The best way to export custom data in a .babylon file is to use `metadata`: [Node | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/typedoc/classes/babylon.node#metadata)

---

<div class="post-metadata">

**Author:** ![hschmiedhofer](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/hschmiedhofer/32/19691_2.png) [@hschmiedhofer](https://forum.babylonjs.com/u/hschmiedhofer)\
**Post date:** [May 17, 2022, 12:47pm UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/8 "2022-05-17T12:47:49Z")

</div>

@carolhmj you are talking about introducing a second import pass that reads the .babylon file as JSON, and produces a map of missing materials and meshes/mesh-ids that want to use those? If so, that would be possible. However, I’d also need to make this work for GLB files later on, so, maybe I could use a temporary custom file loader for that instead.

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [May 17, 2022, 1:41pm UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/9 "2022-05-17T13:41:18Z")

</div>

Yeah, either this or using metadata, as having materials in separate files isn’t really something that .babylon was thought for anyway 🤔

---

<div class="post-metadata">

**Author:** ![carolhmj](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/carolhmj/32/25918_2.png) [@carolhmj](https://forum.babylonjs.com/u/carolhmj)\
**Post date:** [June 15, 2022, 11:53am UTC](https://forum.babylonjs.com/t/loading-mesh-from-babylon-file-with-referenced-material-not-on-the-file/30193/10 "2022-06-15T11:53:15Z")

</div>

Hi there @hschmiedhofer just checking in, was your question answered?
