# Custom File Importer EXTENDING Default Importer?

**URL:** <https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408>\
**Category:** Questions\
**Created:** [May 16, 2022, 9:10am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408 "2022-05-16T09:10:09Z")\
**Posts on this page:** 11\
**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 16, 2022, 9:10am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/1 "2022-05-16T09:10:09Z")

</div>

I want to extend the default .babylon file importer with changing something on an imported mesh during the import.

As an example, in addition to importing my mesh, `SceneLoader.ImportMesh()` should add a suffix to its name based on information inside of the .babylon file.

I was able to register my own plugin by `SceneLoader.RegisterPlugin()`, however, my plugin is now called **instead of** the default loader.

How can I combine the default behaviour with my custom behaviour?

---

<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 16, 2022, 1:36pm UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/2 "2022-05-16T13:36:36Z")

</div>

Hello!  
RegisterPlugin indeed “overwrites” the existing default plugin: [Babylon.js/sceneLoader.ts at master · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Loading/sceneLoader.ts#L672)

You could use `onPluginActivatedObservable` instead: [Babylon.js/sceneLoader.ts at master · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Loading/sceneLoader.ts#L521)

But depending on where this information is, that might even not be necessary. Is your information on the `metadata` attribute? If so, then metadata is preserved when loading, so you could do this renaming in the ImportMesh callback instead 🤔

---

<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, 7:33am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/3 "2022-05-17T07:33:09Z")

</div>

Dear @carolhmj, thanks a lot for your answer. Here’s some more specific information:

What I have: a _.babylon_ file containing meshes referencing missing materials. For instance, a mesh defined on the file has a `materialId` property “someMaterial”, but that material is missing on the file.

What I want: Since importing such mesh kills any information about its `materialId`, I would like to write the `materialId` to some other property of the mesh, such that I can load and create the missing material from somewhere else later on.

What I need: access to the loading process in a way that the mesh is created by default means, after which I want to add the materialId-information to it.

Regarding your suggestion: From what I understand, an observer triggered by `SceneLoader.OnPluginActivatedObservable` would be called when a specific loader plugin is about to be used, but before anything was imported. The observer receives the _eventData_, being the actual loader, and an _eventState_. Since my observer does not get any information on that - how would I be able to interact with a mesh about to be imported at this point?

```nohighlight
 SceneLoader.OnPluginActivatedObservable.add(function (theLoader, eventState) {
      //? what would i do in here?
      console.log('HI from the observer.');
      console.log(`Loader name: ${theLoader.name}`);
      console.log(theLoader.extensions);
      if (theLoader.name === 'myCustomLoader') {
        // if custom loader was called ...
      }
      console.log('BYE from the observer.');
    });

```

---

<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:10am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/4 "2022-05-17T11:10:42Z")

</div>

Thank you for the clarification 😃 It puzzles me a bit why the .babylon file has missing materials, how did you get this file? Was it from blender or through Babylon itself? 🤔

And yeah, the pluginActivatedObservable won’t work in this case since it triggers before the data itself is even read. Since the .babylon file is just a JSON in the end, a workaround could be just using JSON.parse. Another, if you can’t afford loading twice, could be to comment out this line in the babylonFileLoader: [Babylon.js/babylonFileLoader.ts at master · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/blob/master/packages/dev/core/src/Loading/Plugins/babylonFileLoader.ts#L467) so the mesh keeps this `_waitingMaterialId` property.

---

<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:50am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/5 "2022-05-17T11:50:27Z")

</div>

The materials are extracted on purpose beforehand, in order to load them from a server at a later point. Thus, i need to know what mesh needs what material(-Id), even the materials themselves are not on the file.

Regarding commenting-out of parts of the babylonFileLoader: If I understand correctly, you are suggesting to copy the entire default loader code for my custom file loader minus that one line? Of course I could do that, but I’d really like to stay away from repetitive code usage. That’s why I’m searching for a way to extend the default loader code in the first place 🙂

---

<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:08pm UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/6 "2022-05-17T13:08:00Z")

</div>

I was actually thinking of building your own version of Babylon with the line commented out, since as far I’m aware, there’s really no way to extend the default loader for this case (@sebavan is this right?). But, as I’ve commented in your other topic, I think using `metadata` would be much better in this case, since this attribute is saved as it is on export/import.

---

<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, 1:41pm UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/7 "2022-05-17T13:41:53Z")

</div>

Dear @carolhmj, thanks a lot for your help so far 🙂 !

Since I have no control over the creation of the .babylon files I need to process in the described way, there’s no way for me to ensure that the contained meshes have respective `metadata` set. All I have is meshes that are referencing missing materials via `mesh.material` in the file. `Meshes.metadata` would of course be the weapon of choice for me to get info about which material was about to be used but was missing onto my meshes, but following our conversation I see no other option than introducing a pre-pass that reads the .babylon file as JSON and extracts all information about missing materials, in order to apply that info to the metadata of respective meshes after they have been loaded.

What do you think?

---

<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:45pm UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/8 "2022-05-17T13:45:04Z")

</div>

It’s my pleasure! 🙂 Yeah with all the requirements, I think the separate JSON parse step would be the best solution 🤔

---

<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, 2:15pm UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/9 "2022-05-17T14:15:17Z")

</div>

Great, that helps a lot. Thank you very much 🙂

---

<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:54am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/10 "2022-06-15T11:54:19Z")

</div>

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

---

<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:** [June 28, 2022, 8:25am UTC](https://forum.babylonjs.com/t/custom-file-importer-extending-default-importer/30408/11 "2022-06-28T08:25:39Z")

</div>

Sorry for the delay 🙂
