Reputation: 15
I import an obj file with the OBJLoader. After then i want to change the position of a child from the imported model. I try to get the position of the child with the following code:
var vector = new THREE.Vector3();
vector.setFromMatrixPosition( child.matrixWorld );
console.log('vector', vector);
But it always return
THREE.Vector3 {x: 0, y: 0, z: 0}
How can i get the current world position of the child and then change it?
Upvotes: 1
Views: 1850
Reputation: 2101
You can render the scene once before you get the position.
Use this line of code before you get the position :
renderer.render(scene,camera);
(here I assume the name of your scene and camera are scene and camera)
Here is an example of loading an object and getting the position :
var objLoader = new THREE.OBJLoader();
objLoader.setMaterials( materials );
objLoader.setPath( 'models/' );
objLoader.load( objFile, function ( child ) {
child.position.z = 20;
scene.add( child );
renderer.render(scene,camera);
var vector = new THREE.Vector3();
vector.setFromMatrixPosition( child.matrixWorld );
console.log('vector', vector);
}, onProgress, onError );
Here if you comment "renderer.render(scene,camera);", you will get :
Vector3 {x: 0, y: 0, z: 0}
and if you uncomment it you will get :
Vector3 {x: 0, y: 0, z: 20}
Upvotes: 1