Can not read 3d model data

Hi, I want get 3d model data at a function.
But always return null data.
This is my code.

InitPCObject(){
    const loader_PC = new FBXLoader(); //fbx loader
    loader_PC.load("3dmodels/PC/PC.FBX", model => {
    model.scale.set(0.02,0.02,0.02); 
    model.position.set(0, 0, 0 );

    ObjectDatas.push(model); //list type
    console.log(ObjectDatas.length) // length is 1
    three.scene.add(model);  //add scene
    });
}

-> load is OK

InitBox() {
   var geometry = new THREE.BoxGeometry( 1, 1, 1 );
   var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
   var cube = new THREE.Mesh( geometry, material );
   three.scene.add( cube );
   cube .position.set(ObjectDatas[0].position.x ,ObjectDatas[0].position.y,ObjectDatas[0].position.z);
   //Error, ObjectDatas length is 0 and empty
}

InitPCObject();
InitBox();

How can I get ObjectData at InitBox function ?
Thank you.

Because the model takes some time to load, you are calling initBox immediately after starting to load the model, but you need to wait until it has finished loading.

The easiest way of doing this is to call initBox inside onLoad:

// start to load the model, it will take some time
loader_PC.load("3dmodels/PC/PC.FBX", model => { 
    // code here, in the onLoad callback, will run whenever 
    // the model has finished loading
   InitBox(); // => OK
})

// now you call initBox immediately after starting the load operation
// but the model is still loading
initBox(); // => error, model not loaded
1 Like

Hi, I understand.Thank you.

But, is there another way to input in the loader?

Sure, you can use loadAsync which is uses Promises.

async function loadModel() {
    const loader_PC = new FBXLoader(); //fbx loader
    const model = await loader_PC.loadAsync("3dmodels/PC/PC.FBX");
    
    ObjectDatas.push(model)
    InitBox();
}
1 Like

Yes,
Thank you.
It should be helpful.