Back to the Shapespark home page

API change material for another

Hello

Is it possible to change one material for another in an object, like the picker does but from the API with JavaScript?

Say, you would like to replace a material on meshes of type ‘wall’, to be ‘brick’.

First, before the scene is loaded, mark all materials and nodes that you need to use via API as editable:

var viewer = WALK.getViewer();
viewer.setNodeTypeEditable('wall');
viewer.setMaterialEditable('brick');

Then, at any time, but after the scene is loaded, find the required material and nodes and apply the change:

var brickMaterial = viewer.findMaterial('brick');
var nodes = viewer.findNodesOfType('wall');
for (var i = 0; i < nodes.length; ++i) {
    viewer.setMaterialForMesh(brickMaterial, nodes[i].mesh);
}

Hi @jan , thanks for your reply.

I already integrated the code in my test project, it enters the for loop, but visually I don’t see any change.
Is there anything else to add?

I attach reference images.

I found the detail, it was first put the material and then the mesh.

viewer.setMaterialForMesh(brickMaterial, nodes[i].mesh);

Now it works, thanks again @jan

1 Like

Sorry about this, I’m adding this correction to my original post.