Adding individual object from GLTF file, without adding full scene?

I have noticed that I seem to only be able to use individual objects from a GLTF file, if I add the full scene from the file to my scene. To illustrate, here is my code:

loader.load( 'assets/scene.glb', function (gltf) {
    scene.add(gltf.scene);
    let clone = gltf.scene.getObjectByName("MyObject").clone();
    clone.position.y = 10;
    scene.add(clone);

This works as expected, showing both the entire scene, and the moved cloned object. However, if I either remove the scene.add(gltf.scene), or add a scene.remove(gltf.scene) after adding the cloned object, everything disappears, including the cloned object.

I might be misunderstanding something, but shouldn’t it be possible to load a GLTF file and add individual objects from the file to your scene, without having to add the entire scene in the file?

Well, you can select specific objects from the parsed object hierarchy and add it to the scene but you most likely will corrupt the object’s final transformation.

Keep in mind that the ancestors in the hierarchy might apply important transformations which are also influence the object. If you add the object directly to the scene graph, these transformation will be missing and the object might end up e.g. way to large or wrongly rotated.

1 Like

Oh yes, in general you are correct. I forgot to mention that in this case gltf.scene has position (0, 0, 0), rotation (0, 0, 0), quaternion (0, 0, 0, 1) and scale (1, 1, 1), and the same is true for the object I am cloning.

In this case it’s probably best if you demonstrate the issue with a live example.

Mmmh, embarrassing as this is, after turning my code into a minimal example, things work as expected. So I guess the error was somewhere else in my code.

Apologies for wasting your time. and thank you for responding!

1 Like