PBRMetallicRoughnessMaterial throws JSON circular structure error?

Per Introduction to Physically Based Rendering | Babylon.js Documentation if I apply a simple PBRMetallicRoughnessMaterial to all the meshes in my model I get this weird error:

                const mat = new PBRMetallicRoughnessMaterial( `${worktop.sku}_stainless`, scene );
                worktopModel.getChildMeshes( true ).forEach( mesh => mesh.material = mat );
                mat.baseColor = Color3.Red();
                mat.metallic = 1;
                mat.roughness = 1;
onSceneReady TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Mesh'
    |     property '_parentContainer' -> object with constructor 'AssetContainer'
    |     property 'meshes' -> object with constructor 'Array'
    --- index 0 closes the circle

Funny thing is, the error stops if I simply dont assign this particular material to the mesh:

                const mat = new PBRMetallicRoughnessMaterial( `${worktop.sku}_stainless`, scene );
                worktopModel.getChildMeshes( true ).forEach( mesh => mesh.material = null );//mat );
                mat.baseColor = Color3.Red();
                mat.metallic = 1;
                mat.roughness = 1;

… even though Ive defined the material exactly as the linked tutorial dictates.

It also throws no error if I instead use PBRMaterial:

                const mat = new PBRMaterial( `${Math.random()}`, scene );
                //const mat = new PBRMetallicRoughnessMaterial( `${worktop.sku}_stainless`, scene );
                worktopModel.getChildMeshes( true ).forEach( mesh => mesh.material = mat );
                //mat.baseColor = Color3.Red();
                mat.metallic = 1;
                mat.roughness = 1;

Why does PBRMetallicRoughnessMaterial hate me? Im just trying to get a simple stainless-steel-looking material, but even the simplest PBRMetallicRoughnessMaterial tutorial blows up on me.

This should work, you are the first one I have seen with this error (not sure if lucky or unlucky here :slight_smile: ) Could you share a repro in the playground?

What are you doing in onSceneReady ? it looks like you are trying to serialize smthg ?

1 Like

Doh.

Moral of the story - there are apparently legit circular references in the PBRMetallicRoughnessMaterial, which do not occur in PBRMaterial.

And I had a JSON.stringify() call elsewhere, trying to help me understand the materials imported from the GLB, when an unknown material type appeared.

And I was still handling PBRMRM as an unknown type, since this is my first attempt with it.

My fault, nothing to do with Babylon. And better to use util.inspect() than JSON.stringify() for simple debug output. (Webpack 5 dropped default nodejs polyfills, so now it takes an extra step to install ‘utils’ as an import fallback.)

1 Like