2

I'm just beginner with Three.js. I would like to push my 'palmtree' object to an array. My code should be fine, I think..

I have my var objects = [];.

And inside the init:

var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("objects/palmtree.mtl", function(materials) {
    materials.preload();
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.load("objects/palmtree.obj", function(palmtree) {
        palmtree.position.x = 205.12577753354344;
        palmtree.position.y = 2;
        palmtree.position.z = -600.0613083252872;
        palmtree.scale.x = 64;
        palmtree.scale.y = 64;
        palmtree.scale.z = 64;
        scene.add(palmtree);
        objects.push( palmtree );
    });
});

The result looks fine. I have my palmtree in the scene. But when I try objects.length I get 0.. I don't understand what is wrong.

How can I push my palmtree to the objects array? Many thanks.

4
  • Do you see the palmtree with all the adjustments you did (scaling and positioning)? Commented Jan 17, 2017 at 11:28
  • Yes, I see the palmtree correctly Commented Jan 17, 2017 at 12:36
  • did you check the length after the model was loaded? Commented Jan 17, 2017 at 12:46
  • Yes. I checked the length outside the objLoader.load (see answer of Wilt). If I check the length inside the objLoader.load then I get 1 as result. I'm now trying to figure out how I can check the length outside the objLoader.load with the palmtree in it. Commented Jan 17, 2017 at 13:00

1 Answer 1

2

I think your are missing something fundamental here. Object loading is an asynchronous process so if you call objects.length somewhere outside your callback method it will most likely return 0 for the array length since your palm tree has not been loaded yet.

But try once to check objects.length in the callback for example like this:

var mtlLoader = new THREE.MTLLoader();
mtlLoader.load("objects/palmtree.mtl", function(materials) {
    materials.preload();
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.load("objects/palmtree.obj", function(palmtree) {
        palmtree.position.x = 205.12577753354344;
        palmtree.position.y = 2;
        palmtree.position.z = -600.0613083252872;
        palmtree.scale.x = 64;
        palmtree.scale.y = 64;
        palmtree.scale.z = 64;
        scene.add(palmtree);
        objects.push( palmtree );
        console.log(objects.length); // <-- will output new length
    });
});

In this case it will output 1.

So the array length will only return 1 after your loading process has finished and the callback has been executed.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. This is really helpful. Now I have to figure out how I reach it from outside the load.
@EmielSchumacher You can reach it the same way, the question is more about when you use this array. What do you want to do with this array?
I want to check if I click on an object in the onDocumentMouseDown function with raycaster.intersectObjects( objects ). That is why it needs to be in the array. :-)
@EmielSchumacher it will be in the array after load is done, so you can use this code without problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.