My goal is to retrieve the child object sole
from a scene. While I can successfully access the variable sole
inside the loader function using obj
, I am struggling to do so from outside the loader function.
To view the functioning code, click here and the JSON model file can be found here.
I have managed to access the object within the loader:
var loader = new THREE.ObjectLoader();
loader.load("models/shoe4.json", function (obj) {
scene.add(obj);
scene.rotation.y = Math.PI / 1;
scene.position.y = -5;
scene.position.z = -24;
var sole = obj.getObjectByName("sole", true);
sole.position.y = -5;
});
However, my challenge lies in accessing it outside the loader like this:
var loader = new THREE.ObjectLoader();
loader.load("models/shoe4.json", function (obj) {
scene.add(obj);
scene.rotation.y = Math.PI / 1;
scene.position.y = -5;
scene.position.z = -24;
});
var sole = obj.getObjectByName("sole", true);
sole.position.y = -5;
This necessitates creating its own function for future reference.
I attempted:
var sole = scene.getObjectByName("sole", true);
sole.position.y = -5;
But received the error :
Uncaught TypeError: Cannot read property '
getObjectByName
' of undefined
How can I effectively access the sole
object from any part of the code? A simple declaration like var obj;
doesn't seem to work.