Animating obj models

Hi, I’m trying to animate an obj model. I simply want the model to be going up and down for infinity.
I am getting the result I want with the code below, but I’m getting this error message:

Uncaught TypeError: Cannot read property ‘position’ of undefined
at render (d3c.js:1249
at animate (d3c.js:1316)

How do I get rid of it?

Thank you for your help in advance.

Creating the object:

var mtlLoader = new THREE.MTLLoader();
mtlLoader.load('models/LILLY.mtl', function(materials){
materials.preload();
    var objLoader = new THREE.OBJLoader();
        objLoader.setMaterials(materials);
        objLoader.load('models/LILLY.obj', function(object) {
        object.rotateY( THREE.Math.degToRad(90));
        object.rotateX( THREE.Math.degToRad(13));
        object.scale.set(35,35,35);
        object.position.set(-1055, -165, -225);
        lilly1 = object;
        scene.add(lilly1);
    });
});

Code animating it in the render() loop:

var up = true;
   /* Animating Lilly1 */
if(up){
   lilly1.position.y +=0.5; 
}
if(lilly1.position.y == -61){
    up = false;
}
if( !up){
    lilly1.position.y -=0.5; 
 }
 if(lilly1.position.y == -365){
     up = true;
 }
 /* ---------------------- */

It seems you try to access the variable lilly1 before a value was assigned to it. Try to change your animation code to:

if ( lilly1 ) {
	var up = true;
		/* Animating Lilly1 */
	if(up){
		lilly1.position.y +=0.5; 
	}
	if(lilly1.position.y == -61){
		up = false;
	}
	if( !up){
		lilly1.position.y -=0.5; 
	}
	if(lilly1.position.y == -365){
		up = true;
	}
}

Thank you so much, this actually makes a lot of sense. It worked.

1 Like