Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the elements of scene.children in threeJS can be accessed?

Tags:

three.js

I added some objects to ThreeJS scene (using scene.add(object)). I know the name of object and just want to fetch index of it from scene.children.

I tried to use scene.children.indexOf("objectName") but it returns -1 index. Can anybody suggest what can I do?

Thanks

like image 223
harman052 Avatar asked Aug 22 '13 16:08

harman052


2 Answers

var object = scene.getObjectByName( "objectName" );

or to recursively search the scene graph

var object = scene.getObjectByName( "objectName", true );

Alternatively, you can search by ID.

var id = scene.getObjectById( 4, true );

three.js r.60

like image 101
WestLangley Avatar answered Oct 30 '22 17:10

WestLangley


I believe that something that can actually can answer your question is the following code that I use to clean the scene:

while (scene.children.length > 0) {
            scene.remove(scene.children[scene.children.length - 1]);
}

this way I don't need to access an element by its name or id. The object are simply fetched from the array with an index, so it's like scene.children[i].

like image 2
alelom Avatar answered Oct 30 '22 17:10

alelom