Reference model in heirarchy

Hi

I’m stuck on a very basic concept…

I’m loading a GLB model into the scene and the model displays fine

// 
const gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)

gltfLoader.load(
    '/models/aws.glb',
    (gltf) =>
    {
        gltf.scene.scale.set(1, 1, 1)
        scene.add(gltf.scene)

    }
)

Scene info panel in three.js inspector looks like this

image

I’m trying to reference the Cube object in the model and rotate it

I tried cube = scene.getObjectByName(“Cube”,true)

and a few variations but have been unable to access the mesh from the scene.

example error: Uncaught ReferenceError: assignment to undeclared variable cube

Hi, I’v done similar tasks

I would advise you this way:

let my_model;
let gltfLoader = new GLTFLoader()
gltfLoader.setDRACOLoader(dracoLoader)

gltfLoader.load(
    '/models/aws.glb',
    (gltf) =>
    {
        my_model = gltf.scene;
        my_model.scale.set(1, 1, 1);
        my_model.name = 'my_identificator';
        scene.add(my_model)
    }
)

then you will have access to your model like this:

scene.getObjectByName( ‘my_identificator’)

Now you have to linking the separate meshes to control them. I don’t know your model structure - maybe it contains more groups or submeshes, but you can anyway detecting any mesh using function traverse()

// for example:
my_model.traverse(function(element){
    if(element.isMesh){
      element.name = 'submesh_unique_identificator';
   }
});

this way you will have guaranteed links to submeshes and access:
scene.getObjectByName( ‘submesh_unique_identificator’)

Many thanks @stanis3d ! that works! - Solved a piece of the puzzle…

gltfLoader.load(
‘/models/aws.glb’,
(gltf) =>
{
my_model = gltf.scene;
my_model.name = ‘cubey’;
scene.add(my_model);
scene.getObjectByName( ‘cubey’).scale.set(2,2,2)
}
)

My next question is - how can I use that model from outside the gltfLoader.load section? I’m not sure how to reference it

Ie; if I wanted to reference outside that to animate…


const clock = new THREE.Clock()
let previousTime = 0

const tick = () =>
{
    const elapsedTime = clock.getElapsedTime()
    const deltaTime = elapsedTime - previousTime
    previousTime = elapsedTime

   
    // Update controls
    controls.update()

cubey.rotation.y += 0.001

    // Render
    renderer.render(scene, camera)

    // Call tick again on the next frame
    window.requestAnimationFrame(tick)
}

tick()

also tried scene.cubey.rotation.y += 0.001
&
scene.getObjectByName( ‘cubey’).rotation.y += 0.001

Console is giving a cubey is undefined error.