Viewer API: How to set materials before model viewer displays model


I have a scenario where I have a bunch of models that I would like to set with a “Default” material. However I don’t seem to be able to change materials until after the “viewerready” event. This results in a null/white model for a few seconds as the model is “zooming in”.

Does anyone have any pointers for setting the materials at an earlier part of the lifecycle?

Hi there,

By definition, you won’t be able to perform actions on the viewer until it’s ready (the “viewerready” event).

You can prevent the “zooming in” initial camera animation by setting camera: 0 in your initialization options, so that might help a little bit.

Another option would be to set the “Default” material directly in 3D Settings so that the model already has it set.

You could also hide the viewer and/or create your own “loading” screen so that the viewer is not visible until it’s ready and you’ve set your material:

Thank you for the suggestions and clarification about the “viewerready” event.

I was hoping to do everything programmatically, but I think I will probably just set the material directly in the 3D settings. I don’t suppose there’s a way to “copy and paste” material settings between models in 3D settings (json import or something similar)?

The setMaterial method has a callback. You could use that to keep track of all your material changes. However, it’s not waterproof. There always seem to be some sort of extra delay in material changes. Adding a timeout somewhere, as James shows, does help.
There’s no copy/paste for materials in the editor :slightly_frowning_face:

There’s no copy/paste for materials in the editor

We want to add a “Presets” feature in the future, to allow you to quickly save and copy settings between models and materials.

However, the ability to copy the JSON of an existing material for use by the Viewer API could be a good addition to the new developer editor logs! That said, it could get complicated when dealing with textures which then need to be fetched and assigned… something to think about…

Or you could just bring back that PATCH call in the API to be able to edit materials. That would be awesome!