# GLTF Serializer - is there a way to remove unused dynamic textures on export

**URL:** <https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706>\
**Category:** Questions\
**Created:** [August 26, 2020, 5:37pm UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706 "2020-08-26T17:37:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffb](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/jeffb/32/6374_2.png) [@jeffb](https://forum.babylonjs.com/u/jeffb)\
**Post date:** [August 26, 2020, 5:37pm UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/1 "2020-08-26T17:37:00Z")

</div>

When we export a scene via GLTF2Export.GLTFAsync, dynamic textures that are not in use by any exported nodes show up in the exported files (referenced in the GLTF and listed as png’s). Note: they might have been referenced by nodes that were filtered out using the shouldExportNode options filter.

Is there a way to filter unneeded dynamic textures out of the exported GLTF?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [August 26, 2020, 5:39pm UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/2 "2020-08-26T17:39:12Z")

</div>

Assigned @Drigax on August 26, 2020, 5:39pm UTC



---

<div class="post-metadata">

**Author:** ![Drigax](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/drigax/32/16585_2.png) [@Drigax](https://forum.babylonjs.com/u/Drigax)\
**Post date:** [August 27, 2020, 10:25pm UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/3 "2020-08-27T22:25:35Z")

</div>

Hi @jeffb, welcome to the Forum!

Currently, the exporter exports all materials in scene, pulling all referenced textures with them, then links the material to each exported mesh, but it should be fairly straightforward to add a method that allows the user to exclude materials from the scene similar to: [IExportOptions - Babylon.js Documentation](https://doc.babylonjs.com/api/interfaces/babylon.iexportoptions#shouldexportnode) where we export the scene contents by default, and let the user manually define which meshes should be ignored.

Otherwise the alternative is to dispose of the materials with textures that you don’t need exported, which isn’t super ideal if you want to use them later.

Would this solution work for you?

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [August 28, 2020, 12:28am UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/4 "2020-08-28T00:28:03Z")

</div>

It would be awesome if it also accepted an array of meshes, if you don’t want the entire scene,

> [myMeshA, myMeshB]

Or if you want all meshes, but not lights, cameras, etc

> scene.meshes

And then only serialize child tree and connected materials, textures etc

---

<div class="post-metadata">

**Author:** ![Drigax](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/drigax/32/16585_2.png) [@Drigax](https://forum.babylonjs.com/u/Drigax)\
**Post date:** [August 28, 2020, 1:46am UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/5 "2020-08-28T01:46:50Z")

</div>

> It would be awesome if it also accepted an array of meshes, if you don’t want the entire scene,  
> [myMeshA, myMeshB]

That sounds already pretty doable with the current function pointer based implementation:

```auto
let meshesToExport = [myMeshA, myMeshB]
BABYLON.GLTF2Export.GLBAsync(scene, assetName, {shouldExportNode: (node) =>{
     return meshesToExport.indexOf(node) != -1;
});

```

> Or if you want all meshes, but not lights, cameras, etc

Ah! now thats something to consider. Perhaps adding a collection of `shouldExportX` functions for each type of element in scene?

> And then only serialize child tree and connected materials, textures etc

I’m a bit on the fence about this one as well, one can modify the sample to do something like this:

```auto
let meshesToExport = [myMeshA, myMeshB]
BABYLON.GLTF2Export.GLBAsync(scene, assetName, {shouldExportNode: (node) =>{
     return meshesToExport.some( (mesh) => { mesh === node || node.isDescendantOf(mesh)}});
});

```

But that’s not super efficient…

---

<div class="post-metadata">

**Author:** ![aWeirdo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/aweirdo/32/52_2.png) [@aWeirdo](https://forum.babylonjs.com/u/aWeirdo)\
**Post date:** [August 28, 2020, 2:10am UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/6 "2020-08-28T02:10:43Z")

</div>

Having a need of exporting select meshes, i found myself hacking away as i’ve had no time to look at the source yet.

final result was this process;  
Export to babylon data string  
Create new empty scene & import babylon data string  
Export new scene to gltf or glb 😂

so for me at least, the ability to export an array of meshes & their connected materials & textures would be great 😅  
Either way, it’s no problem if you guys don’t feel it’s worth including in source, i’ll get around to a local fix eventually  
Sorry for hi-jacking the thread 🙂

---

<div class="post-metadata">

**Author:** ![Drigax](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/drigax/32/16585_2.png) [@Drigax](https://forum.babylonjs.com/u/Drigax)\
**Post date:** [August 28, 2020, 2:18am UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/7 "2020-08-28T02:18:18Z")

</div>

those snippets should definitely give you what you need regarding mesh selection at export, no need to go through the scene serializer if you can avoid it. The materals/texture removal will need some additional feature work, but I don’t quite have the time to take that on right now.

But I definitely agree a couple switches like “IncludeChildren” and “IgnoreUnused” to add to the export options would be really useful

---

<div class="post-metadata">

**Author:** ![Drigax](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/drigax/32/16585_2.png) [@Drigax](https://forum.babylonjs.com/u/Drigax)\
**Post date:** [January 26, 2021, 4:19pm UTC](https://forum.babylonjs.com/t/gltf-serializer-is-there-a-way-to-remove-unused-dynamic-textures-on-export/13706/8 "2021-01-26T16:19:49Z")

</div>

Unassigned @Drigax on January 26, 2021, 4:19pm UTC


