How animate an Object load with new THREE.ObjectLoader()?

Good evening,

it’s probably a stupid question but it’s been several days since I tried to do something without success …
with that :

var objloader = new THREE.ObjectLoader();

var sun = objloader.load( “obj/sun3.json”, function createSun (sun) {
sun.position.set(0, 0, 0);
scene.add(sun);
} );

I would like to animate this object by a rotation.y + = 0.02. unfortunately I can’t get the data out of the function to use them in the rendering.

Loading is asynchronous - ie. you can’t use model until it is there. It should be enough if you created an external variable and assigned model to it. Then in the render loop, check if the variable is already defined - if it is, do the animating. (See this for example.)

Alternatively you can give your model a name using:

sun.name = 'sun';

And then in the render loop try to retrieve the model using Scene.getObjectByName:

const animate = () => {
  const sun = scene.getObjectByName('sun');

  if (sun) {
    // Do things with the model
  }
}
1 Like

Thank you very much you saved me again :grin: