Find the size of a loaded GLTF model

Hi!
I’m trying to figure out a way to get the size of any GLTF model loaded into the application. I want to use this to make a calculation to position the camera at a distance that shows the entire model, regardless if it is too big or too small.

        import { GLTFLoader } from '/GLTFLoader.js';     
        var scene, camera, renderer, controls;
        var objects = []; 

        function init() {

            scene = new THREE.Scene();
            scene.background = new THREE.Color(0xdddddd);

            camera = new THREE.PerspectiveCamera(40,window.innerWidth/window.innerHeight,1,5000);
            camera.position.x = 1;
            camera.position.y = 1;
            camera.position.z = 1;
            

            renderer = new THREE.WebGLRenderer({antialias:true});
            renderer.setSize(window.innerWidth,window.innerHeight);
            document.body.appendChild(renderer.domElement);

            controls = new THREE.OrbitControls(camera, renderer.domElement);
           

            var hlight = new THREE.AmbientLight(0x404040,100);
            scene.add(hlight);

            var directionalLight = new THREE.DirectionalLight(0xffffff,100);
            directionalLight.position.set(0,1,0);
            directionalLight.castShadow = true;
            scene.add(directionalLight);
           

            let loader = new GLTFLoader();
                loader.load("table_football.glb",
                    function(gltf){
                        gltf.scene.traverse(function (child) {
    
                            //console.log(child);

                        });
                        scene.add(gltf.scene);
                        objects.push(gltf.scene);
                        animate();
                    },
                    (xhr) => {
                        console.log((xhr.loaded / xhr.total) * 100 + '% loaded')
                    },
                    (error) => {
                        console.log(error)
                    });
               
          
            var box = new THREE.Box3().setFromObject( objects[0] );
            var size = box.getSize();
            //console.log( size );

            var vFOV = camera.fov * Math.PI / 180;        // convert vertical fov to radians
            var height = 2 * Math.tan( vFOV / 2 ) * dist; // visible height
            var dist = size.y / ( 2 * Math.tan( camera.fov * Math.PI / 360 ) );
            camera.position.set( 0, 0, (size.y)/2 + dist );
            camera.lookAt(scene.position);

        }
        

        function animate() {
            renderer.render(scene,camera);            
            requestAnimationFrame(animate);
        }

        init();

For that I’m trying to use THREE.Box3().setFromObject(), but it seems that it doesn’t accept the GLTF model or lacks parameters. Does anyone know how I can resolve it?

It should be this:

const box = new THREE.Box3().setFromObject( gltf.scene ); 
const size = box.getSize(new THREE.Vector3());

But you have to put this code in the onLoad() callback. Right now it is executed before the glTF asset has been fully loaded which means objects[0] is undefined.