Snap code for Scenes: Node examples

Snap code can manipulate nodes

Learn about creating nodes in your 3D scene – such as meshes, lights, etc. – before using Snap like the examples below to manipulate those nodes.

Changing an attribute of a node

A node is any 3D object in the scene, visible or not. use the “set node” and “get node” blocks to manipulate meshes, materials, global lights, viewpoints, mates, annotations, and the like.

Changing features of a node

Features applied to any mesh node can be named, and then manipulated with Snap. Here, we have created a linear pattern feature called “Linear Pattern- Property Line” on a mesh called “Fence Post”. The Snap blocks here change the translation and count of the pattern to make the row of fenceposts longer or shorter.

Animating Nodes

Many changes to nodes in the node tree can be animated. The “animate” block adjusts parameters much like the “set” block, only the change is not applied instantly. Rather, the change can be applied at some time in the future, and can last a certain duration.

A Simple “set” of a mesh’s rotation. This happens instantly.

The same change, applied through an animation. The rotation will take 1 second to complete (duration is 1000 milliseconds). It will start immediately (delay of 0ms), and be performed only once (iterations of 1). Easing defines how the animation movement will be distributed over time. For a constant speed, choose linear. For a sense of inertia (like a ball falling, or a heavy door opening), use a sinusoidal option.

The animate block can be mutated to add a “followed by” block, so when this animation ends, another event (like another animation) can start, as shown below.

The parser and the animator are separate processes.

Note that the parser which processes the rule cycle is separate from the animator. When the parser encounters an animate block, it simply passes the request to the animator, and proceeds with the next Snap block. In this way, the rule cycle completes with no delay.

The animator – like a chef behind the scenes in the kitchen – takes these animate requests from the parser and performs them at the requested time. In this way, you can create complex movements through a series of simple movements scheduled to run at the same time.

For example, you can create a solar system animation by rotating the moon around the earth, and then rotating the earth around the sun. You do not need to calculate the complex path of the moon around the sun: the two simple animations work in conjunction with each other to create that complex path.

Animations can run indefinitely.

You may want an animation to “run” indefinitely, or until the user turns the animation on or off by setting a field value. This is best accomplished by breaking the animation into small repeatable parts, and checking before each part is run if the user still wants it.

This example Scene Rule will slowly spin a mesh, as long as a boolean field “IsRotating” is set to true.

Collision Detection

Use the bounding box or nodes intersect blocks to detect if the geometry of any two nodes occupy the same space. Here, we change the albedo color of the material used on a mesh called Box if it intersects with another mesh called Plane.

(This example is a short demo. For production use, we don’t suggest using the nodes intersect block within the dragging slot of the add draggable for performance reasons. Rather, use the “dropped” slot.)

Was this article helpful?