Cannot set property material of [object Object] which has only a getter

I use AssetContainer.instantiateModelsToScene() clone the model.I need to clone the material.An error was throw.

https://playground.babylonjs.com/#QHKHQE#1

It crashes because it tries to set the material on an instanced mesh, which is not possible as an instanced mesh inherits the material from its master mesh (there’s no setter for material in the InstancedMesh class).

Will be fixed by:

does the same concept apply to skeletons too? I am trying to create an asset pool and am getting the same error only difference that it cannot get skeletons

This PR should fix the problem for skeletons:

1 Like

Thanks. Will check it out

Hi this worked but the animations don’t seem to be working? Is that a problem with babylon or am I doing something wrong. The animation group length is 0 in game but looks fine in sandbox

You should create another issue (with a repro PG if possible) as this is another problem.

2 Likes

Hi, I got the same error in my react-typescript project and i don’t know how to fixed it, i tried to set materials for imported meshes, it worked in the scene but shown this error, and all gui disappeared. here’s my code

    const result = await SceneLoader.ImportMeshAsync(
      "",
      "/assets/test/",
      "myModel.gltf",
      scene
    );

    result.transformNodes.forEach(node => {
      generateMaterial(node);
    })

    const generateMaterial = (node: TransformNode) => {
      if (node.name === "wall_01") {
        let glass_wall = new PBRMaterial("glass_wall_wall", scene);
        glass_wall.indexOfRefraction = 0.52;
        glass_wall.alpha = 0.5;
        glass_wall.directIntensity = 0.0;
        glass_wall.environmentIntensity = 0.7;
        glass_wall.cameraExposure = 0.66;
        glass_wall.cameraContrast = 1.66;
        glass_wall.microSurface = 1;
        glass_wall.reflectivityColor = new Color3(0.2, 0.2, 0.2);
        glass_wall.albedoColor = new Color3(0.95, 0.95, 0.95);
        let children = node.getChildMeshes();
        for (let child of children) {
          // error:cannot set property material of [object object] which has only a getter
          child.material = glass_wall;
        }
      }

    }

my package.json:

    "@babylonjs/core": "^5.46.0",
    "@babylonjs/gui": "^5.46.0",
    "@babylonjs/inspector": "^5.46.0",
    "@babylonjs/loaders": "^5.46.0",
    "@babylonjs/materials": "^5.49.1",

Make sure that child is a mesh and not an instance mesh before setting the material: You can test child.getClassName() === "Mesh".

2 Likes

thank you! I think I have to take a look at InstanceMesh.

1 Like