user8079295
user8079295

Reputation:

How can i iterate through scene children in three js

Using three js:

I have a function removeBysid(id)

What i want to do is go into all of the children in my scene and check the treeNode of the third child and get its unique sid (a variable I created).

In the console when I do :

scene.children[4].children[0].children[0].treeNode

I get what I want to achieve with the function.

Now I want a function to iterate all of the children without hardcoding the index like I did in the console.

function removeBysid(id) {
 scene.traverse(function(element) {
    //element returns Scene {uuid: "AC30E121-517A-40F9-8F86-F3626003A3C7", name: "", type: "Scene", parent: null, //children: Array(6)…} now i want to check scene.children[4].children[0].children[0].treeNode.sid = sid
 })
}

Upvotes: 2

Views: 5319

Answers (1)

juanferrer
juanferrer

Reputation: 1250

It should look somewhat like this:

function removeBysid(id) {
    scene.traverse(function(element) {
        if (element.children[0].children[0].treeNode.sid == id) {
            scene.remove(scene.getObjectById(element.id);
            // And since you know it's unique...
            return;
        }
    })
}

Upvotes: 2

Related Questions