How to work with gltf hierarchy?

Hi I’m struggling to understand how to work with the hierarchy you get with gltf import. I can import the scene for a single mesh, no animation glb using the example code without issue, model shows fine.
But unlike the other importer / geometry generation examples I can’t figure out how to then refer to the elements. For example to rotate an object.
Thanks in advance for any guidance.

Importing the .glb into three.js editor shows:
drone.glb -> root -> droneobject->droneobject - latest

Console log of scene using gltf boilerplate shows:
OSG_Scene->RootNode_(gltf_orientation_matrix) -> " " -> RootNode -> droneobject -> droneobjectphong

Scene type -> Object 3D -> Object 3D -> Object 3D -> Object 3D -> Mesh

This depends a bit on how you’re creating the model… stuff from Sketchfab will often have some extra Object3Ds cluttering the hierarchy, basically leftover from their pipelines and whatever format the artist originally used. For example, a file created in Blender probably has an extra node added to handle the Z=up -> Y=up conversion. Files converted from FBX are often scaled by 100 to convert from FBX conventions. A glTF file could potentially contain many meshes, unlike THREE.ObjectLoader for example.

If you know your file contains only one mesh (multi-material meshes are a special case), you can probably just get it by name.

var model = gltf.scene.getObjectByName('droneobject');

If you don’t know what’s in the model, or just want to treat it as a single unit, it’s better to save the whole gltf.scene reference and work with that:

var model = gltf.scene;
model.rotation.x = Math.PI;
model.traverse((o) => {
  if (o.isMesh) o.castShadow = true;

I don’t know why the the three.js editor and the boilerplate would show you different results… maybe the editor automatically strips unneeded objects? If you open the JS console on what do you see?

1 Like

In gltf-viewer js console it shows as Scene → root (object3d) → drone object (object 3D) → drone object (Mesh).

Thanks for your help :slight_smile: