Now when you can easily traverse the scene, you may want to get or change some parameters for nodes. You may have noticed already that we printed node names while traversing the scene. You may do a lots of other things the same way.

Name

Once again, let’s create a sphere, get its name and try to change it:

var n = scene.root.addMesh("sphere", "generator.simple.sphere");
print("current name is %1", n.name);
n.name = "new name";
print("the new name is %1", n.name);

If you run this, you’ll see the following:

current name is: sphere
the new name is: new name

Isn’t that easy?

Visibility

Let’s do a more complex task: find and hide all the top-leve sphere objects. The traversing topic has a ready to use code that finds all the top level nodes, let’s modify it to filter spheres and hide them. It takes just three lines:

var top = scene.root.children;
for (var i in top)
{
    var ch = top[i];
    if (ch.type != 1) continue; // 1*
    if (ch.generator.toString().search("Sphere") == -1) continue; // 2*
    ch.visible = false; // 3*
}

This code needs some comments. At the first line (1) we get the node type and check if it is 1. Boxshot supports three types of nodes:

  • 0 - group node
  • 1 - mesh node
  • 2 - light node

So if the node is not mesh, we simply skip it. The second line (2) is the most complex one. Every mesh node has a generator object that generates a geometry using the provided parameters. You adjust generators at the object’s properties panel at the right. So we get generators, and check if their names contain a “Sphere” word. If not, we skip them, as well.

Finally, the third line (3) hides the node by setting its visible property to false. To show nodes run the same script with “true” instead of “false”.

Transformations

Here’s how you can change the node’s offset, rotation and scale:

var node = ...; // scene.root.children[0];
node.translation = vec3(10, 20, 30);
node.rotation = vec3(0, 0, 90);
node.scale = vec3(1.2, 1.2, 1.2);

Use exactly the same values as you enter to the transformation page in the user interface.

Selecting Nodes

The current selection is controlled by the scene, so the approach is a bit different here. First of all you need to build a list of the node’s you’d like to select. You may use just a single node, as well.

Let’s select a very first top level node in the scene (assuming that the scene is not empty):

scene.selection = scene.root.children[0];

Easy? OK, let’s select all the top level items in the scene:

scene.selection = scene.root.children;

See the difference? We passed an array. What about removing the selection? Pass null or empty array, both work well:

scene.selection = null;
scene.selection = [];

So, this is easy. What if you need to add to the current selection? It’s all about array manipulation. Let’s add a very first top level node to the current selection:

var sel = scene.selection;
sel.push(scene.root.children[0]);
scene.selection = sel;

The second line is a javascript-way to append an element to array.

Rearranging Nodes

You can do this easily using the mouse, you can do this using script, as well. Boxshot allows you to get a list of nodes and make them then children of another one at a specific position. This includes re-parenting, so useful for grouping and ungrouping nodes. Here is the code that gets the current selection and drags it inside the very first node at the top level. Please make sure that you haven’t selected that node, as you will get an error message in that case.

var sel = scene.selection;
var target = scene.root.children[0];
scene.drag(target, target.children.length, sel);

The script is quite straightforward, the main job is done in the last line. The first parameter of the drag() method is the target node, the second is a position (we pass the number of target’s children there to add to the end), and the last one is a list of nodes to drag.

Cloning Nodes

This is useful when you created a complex mesh with all the parameters and materials adjusted and need to create a similar one to put beside. Boxshot allows you to clone just the node, or its children, as well. The code is simple:

var n = scene.root.addMesh("sphere", "generator.simple.sphere");
n.generator.radius = 1;
n.translation = vec3(0, 0, 1);
var n1 = scene.clone(n, false);
n1.translation = vec3(0, 0, 10);

Please note that you can’t clone the root node, and the following code will return error:

scene.clone(scene.root);

That’s because the root node has no parent, so there is no place to attach a clone. If you need to clone all the top level items, please consider moving them inside the group node first and then clone it instead:

var top = scene.root.children;
var g = scene.root.addGroup("group");
scene.drag(g, 0, top);
var g1 = scene.clone(g, true);
g1.name = "cloned group";