# doNotSerialize for material plugins

**URL:** <https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228>\
**Category:** Completed Features\
**Tags:** serialization, materialpluginbase\
**Created:** [December 7, 2024, 3:57am UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228 "2024-12-07T03:57:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [December 7, 2024, 3:57am UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/1 "2024-12-07T03:57:05Z")

</div>

Version: git master (d4c5968)  
Engine: WebGL2

## Background:

I’m making a material plugin and do not want it to be serialized, since it is designed to be attached at runtime by code.  
In `Material#_serializePlugins`, which is called every time a material being serialized, it just serialize every plugin in `_plugins`.

> <https://github.com/BabylonJS/Babylon.js/blob/d4c5968cf83512d2c27c0947129d68f217857e44/packages/dev/core/src/Materials/material.ts#L1928>

## Proposal:

Add a public `doNotSerialize` to `MaterialPluginBase`, and skip serialization when it’s true. Existing material plugins would implicitly have this field as they extends `MaterialPluginBase`.

```typescript

    protected _serializePlugins(serializationObject: any) {
        serializationObject.plugins = {};

        if (this.pluginManager) {
            for (const plugin of this.pluginManager._plugins) {
                if (!plugin.doNotSerialize) // the proposed change
                    serializationObject.plugins[plugin.getClassName()] = plugin.serialize();
            }
        }
    }

```

## Risks:

Material plugins that does not extend `MaterialPluginBase` will not have a `doNotSerialize` after this, but when using if to check, it would have the same result as `false`, which should be the default value, so backward compatibility for js projects would not be broken after this.  
In typescript projects, type checking ensures that plugins extending `MaterialPluginBase`.

## Alternatives

Users can iterate the serialized json for material plugin and remove what they do not want, but it’s complex to do that.  
Js users can patch `Material#_serializePlugins` in prototype and make the change without upstreaming it.

---

<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:** [December 7, 2024, 5:20pm UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/2 "2024-12-07T17:20:11Z")

</div>

I think it is an easy change that we should do. Wanna give a shot at a PR?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 7, 2024, 9:47pm UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/3 "2024-12-07T21:47:13Z")

</div>

Why not simply implementing an empty `serialize` method in your plugin?

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [December 9, 2024, 12:40am UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/4 "2024-12-09T00:40:49Z")

</div>

empty `serialize` can not be easily changed at runtime, and would emit a error at `Material._ParsePlugins`

> **[Babylon.js Playground](https://playground.babylonjs.com/#DN6NU9)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 9, 2024, 9:05am UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/5 "2024-12-09T09:05:58Z")

</div>

You should return an object with a name property, not null:

> **[Babylon.js Playground](https://playground.babylonjs.com/#DN6NU9%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL and WebGPU 3D scenes

[…] Ah, my bad, you don’t want the plugin to be serialized so that it’s not recreated at parse time! A new `doNotSerialize` property should do it indeed.

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [December 10, 2024, 12:47am UTC](https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228/6 "2024-12-10T00:47:25Z")

</div>

PR Here:

> <https://github.com/BabylonJS/Babylon.js/pull/15971>
>
> This commit adds a new public property \`doNotSerialize\` to \`MaterialPluginBase\`,… so that a material plugin can be configured to skip serialization like meshes and materials.
> 
> \## Compatibility
> 
> Existing material plugins would implicitly have this field as they extend MaterialPluginBase. Material plugins that does not extend MaterialPluginBase will not have a doNotSerialize after this, but when using if to check, it would have the same result as false, which should be the default value, so backward compatibility for js projects would not be broken after this.
> In typescript projects, type checking ensures that plugins extending MaterialPluginBase.
> 
> ## References
> 
> Forum post: \<https://forum.babylonjs.com/t/donotserialize-for-material-plugins/55228\>
