How to get actual mesh position in GLTF scene?

I have loaded the GLTF scene which was exported from Sketchup. I am trying to get the meshes actual position in the scene. The console I do not get the mesh positions. If I use child.position.set(1,1,1) which is reflected in the console log but without using position.set() the position of the mesh not shown.
Below my code

    const gltfLoader = new GLTFLoader();
        var model = new THREE.Object3D();
        gltfLoader.load('images/ann2.gltf', (gltf, el) => {
            model = gltf.scene;
            model.name = 'model';

            // Add gltf model to scene
        scene.add(model);
        //change color
        var newMaterial = new THREE.MeshBasicMaterial({color: 0xffff});
        var newMaterial2 = new THREE.MeshStandardMaterial({color: 0xffffff});
        
        scene.traverse(function (child) {
            
        //console.log(child,'sureshhhhh')
        if (child.name == "Component#1")  {
            console.log(child.name, "child_name")
            child.material = newMaterial;
            
        
        
        }else if (child.name == "Component#2") {
            
            child.material = newMaterial2;
            //child.position.set(1,1,1)
            var positionA = new THREE.Vector3();
            scene.updateMatrixWorld(true);
            positionA.setFromMatrixPosition(child.matrixWorld);
            console.log(positionA.x+',' +positionA.y+',' +positionA.z);
            console.log(child.localToWorld( positionA ),'position comp' );
           
        } 
            const box1 = new THREE.Mesh(
            new THREE.BoxGeometry(2, 2, 2),
            new THREE.MeshStandardMaterial({
                color: 0xffff00,
                name: 'suresh'
            })
        );
        // Retrieves Object3D in the scene
       
        
        model.add(box1);
        //box1.position.set(1, 2, 3);
        var axes = new THREE.AxesHelper(15);
        model.add( axes );
        //this is for get model name
        //model = scene.getObjectByName(model.name)
        //console.log(model,'dsdddgfdsggfdsgdg')
        scene.up
        var positionB = new THREE.Vector3();
        scene.updateMatrixWorld(true);
        positionB.setFromMatrixPosition(box1.matrixWorld);
        console.log(positionB.x+',' +positionB.y+',' +positionB.z);
        
        });
       
        
          const box = new THREE.Box3().setFromObject(model);

          const boxSize = box.getSize(new THREE.Vector3()).length();
          const boxCenter = box.getCenter(new THREE.Vector3());
          console.log(boxSize,'box bounding box')

If you want an object’s position in world space (which is in some sense it’s “real” position in the world), use Object3D.getWorldPosition().

1 Like

@Mugen87 Thank you.
I used gltf.scene.getWorldPosition( child.position ); but again it shows the position result like Vector3 {x: 0, y: 0, z: 0}

   scene.traverse(function (child) {
            
    if (child.name == "Component#1")  {
        console.log(child.name, "child_name")
        child.material = newMaterial;
    
    
    }else if (child.name == "Component#2") {
        
        child.material = newMaterial2;
        //child.position.set(1,1,1)
        var positionA = new THREE.Vector3();
        scene.updateMatrixWorld(true);
        positionA.setFromMatrixPosition(child.matrixWorld);
        console.log(positionA.x+',' +positionA.y+',' +positionA.z);
        console.log(child.localToWorld( positionA ),'position value' );
        console.log(gltf.scene.getWorldPosition( child.position ));
        
    }