In need of assistance on how to make OBJLoader solely return the object without adding it to the scene. I am encountering a synchronization issue where my code does not properly wait for the XHR request, resulting in errors.
Check out the example below:
var loader = new THREE.OBJLoader();
// create a mesh from an obj file
function createObject( objFile, objName ) {
object3d = loader.load( objFile , function ( object ) {
object.name = objName;
console.log(object);
return object;
})
return object3d;
}
car = createObject('path/to/car.obj', 'abc123');
car.position.y = 10;
The console output looks like this:
1. Uncaught TypeError: Cannot set property 'position' of undefined test.html:171
2. XHR finished loading: GET "path/to/car.obj". Three.js:11377
3. THREE.Object3D {id: 7, uuid: "9E8DC838-E68B-4FCA-A6AD-863D5D06D31F", name: "abc123", parent: undefined, children: Array[1]…}
Error (1) occurs because 'car.position.x' is executed while the XHR request is still ongoing and the object is null. My code lacks the necessary waiting time. However, (2) and (3) indicate that the load request does eventually succeed.
Any thoughts on resolving this issue? How can I modify createObject() so that it waits for the XHR request to complete before proceeding? Or is there a better way to structure this process?