Start a script only once asset is loaded?

I’ve been working on some simple animation scripts. The scripts work. They are run from the script-type rule within the logic UI. The intent is to have the animation start immediately, but it is starting too soon. I can see the console filling up with warnings about API queries failing. Once the asset is fully loaded, the warnings stop, and the animation commences. This indicates that the rule is being executed before the geometry is available. Is there a way to have a script only to execute if the asset is fully loaded?

I did play with approaches like:
await api.getConfigurator();
but the configurator is ready long before the asset.

Hi @jem -

Have you seen the docs on player loading events? There are three events that fire during load - preloaded, loaded, and rendered. In this case, I think the rendered event will be the most appropriate, it fires when the asset is completely loaded & rendered.

player.on('rendered', () => {
  // animation code here
});

Let me know if this is helpful or not.

Hi @Will,
Thanks for reminding me about the events. I made some progress using this technique. I wanted some code that would only execute if the asset was rendered. I also wanted that code to remember if the asset was rendered so that a subsequent call would not just hang. Here is what I came up with:

if (typeof rendered === 'undefined') {
    playerApi.on('rendered', () => {
        console.log('rendered');
        rendered = true;
        animateStuff();
    });
} else {
    animateStuff();
}

This code lives in a rule in an asset, and it works when I view the asset directly. But, I became stuck on the next step. If this same asset is dynamically loaded into a scene, the asset never sees the on-render event, and the code waits forever.

Are there events associated with loading a specific asset into a scene?
Thanks again

These are player events to tell you when the asset you have embedded has hit a certain point, these events do not fire after the player has loaded. Once the player has loaded it’s in the final state.

There are some events for configurations & other interaction events but I am not sure they would be very applicable here if you are not using the configurator. Is this still the same project using addNode methods to insert your models into the scene?