When you have succesfully created some objects, it’s a good idea to find a way to find them in the scene to be able to control their parameters, rename them and so on. Here you will know how to do this.

The Root Node

Each scene has an invisible root node which contains all the top-level nodes. Here’s how you can get it:

var r = scene.root;
print(r.name);

You will see the “Root” in the output window of the Script Editor. Use the root node as a starting point for traversing the scene.

Finding Children

Create some objects in the scene manually or using script, then run the following code:

var top = scene.root.children;
for (var i in top)
{
    var ch = top[i];
    print("%1: %2", i, ch.name);
}

Here top is array of child nodes, you may get the number of children that way:

var num = scene.root.children.length;

Each child node itself may have its own children, so you may traverse the whole scene if you’d like:

function traverse(node, spaces)
{
    var items = node.children;
    for (var i in items)
    {
        var n = items[i];
        print("%1%2", spaces, n.name);
        traverse(n, spaces + "    ");
    }
}
traverse(scene.root, "");

Parent Node

Every node has a parent. You can get it that way:

var n = scene.root.addMesh("sphere", "generator.simple.sphere");
print("node: %1", n.name);
print("parent: %1", n.parent.name);

If you run the script, you’ll get this:

node: sphere
parent: Root

Looks correct, the sphere was created in the root node, so its parent is “Root”. But what if we try to get a root’s parent?

var p = scene.root.parent;
print(p);

You will get “null”, as the root node is a topmost one and doesn’t have parents. All the others - have.

Finding Siblings

Now you should know how to find sibling nodes easily. There is no special function for that, as you already have everything you need. Let’s create a sphere and get its neighbours.

var n = scene.root.addMesh("sphere", "generator.simple.sphere");
var siblings = n.parent.children;
print("our sphere has %1 siblings", siblings.length - 1); // we don't count our sphere here, so -1