Traversing through Meshes/Objects causes bizarre behavior

I’m wondering if anyone else has run into the same problem and what the fixes are…
When I just use ‘scene.add(gltf.scene)’ to the GLTFLoader, the object renders fine:

However, when I traverse through meshes, I get this:

Similarly, when I traverse through Object3Ds, I get this:

I’m stuck as to why there is misalignment and missing elements in the meshes. Thanks in advance for your help!

Please share your code for help debugging it.

Keep in mind that objects inherit the position/rotation/scale of their parents. If you traverse the scene, extract meshes or objects, and then add them directly to a parent scene, you’re removing them from their original parents and any position/rotation/scale that they received from those parents.

Here is my code: script.js (5.6 KB)
Lines 75 is where I traverse through the meshes

Thank you so much. Does this mean that I have to add the meshes to their parents before adding them to the scene? Also do you know why there are missing meshes?

in your traverse loop, before iether isMesh or isObject3D statements, try using child.updateMatrix() and child.updateMatrixWorld()

In general it is not a good idea to modify a scene graph while you’re traversing it, you may miss things or reach them more than once. Adding objects to another parent modifies what you’re traversing, because it removes them from their original parent.

Would you be able to do call arduino.add( gltf.scene ) once, rather than adding all the parts separately?

1 Like

I want to be able to add a 2D label to each of the meshes. Would that be possible with adding the parts all at once?

Yeah that’s no problem — go ahead and add the labels to the model’s subtree. Then add the model’s subtree to the scene (or the arduino group) once.

Thank you so much! This was extremely helpful!