Custom File Importer EXTENDING Default Importer?

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?

Hello!
RegisterPlugin indeed “overwrites” the existing default plugin: Babylon.js/sceneLoader.ts at master · BabylonJS/Babylon.js (github.com)

You could use onPluginActivatedObservable instead: Babylon.js/sceneLoader.ts at master · BabylonJS/Babylon.js (github.com)

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 :thinking:

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?

 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.');
    });

Thank you for the clarification :smiley: 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? :thinking:

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) so the mesh keeps this _waitingMaterialId property.

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 :slight_smile:

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.

1 Like

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

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?

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

Great, that helps a lot. Thank you very much :slight_smile:

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

Sorry for the delay :slight_smile: