Switching Animation On & Off in 3D

The goal of this tutorial is to show how to switch an Anim node on and off by clicking on an object, using a TouchSensor and this single line of script:

spin.pause = spin.play

This tutorial also shows how to spin an object using the single line of script:

p.rotation = Rotation(0, 0, 1, PI * 2 * v)

But to be able to switch any animation we have already created using an Anim node, the only script we need to add is the first line so we only really need to focus on operation 7: Add toggle script.

Both lines of script can be copied and pasted from this tutorial using the standard short cut keys (Ctrl V) to paste them into a Seamless CodeLine node but if our Anim node has a different name to "spin" we must replace "spin" with the name we use for the Anim node. Script lines are not processed (compiled) until we press the Enter key (or select another node and/or window), so we can still edit a line immediately after it has been pasted. A sucesfully compiled line shows the line colour coded.


Start the robot demo by moving the mouse to the top left corner of the 3d window and selecting switch anim in 3d demo as shown:


All of the titles for the robot demo operations will be shown here in bold beginning with the operation number followed by the title itself. The operations for this tutorial are from 0 to 8 only. Operations from 9 onwards are for follow up tutorials which are more advanced.

0. Create a new Seamless file

The button is clicked to create a new smls file.

1. Build a box

While the part node is selected, the button is clicked from the horizontal toolbar to create a BoxBuild node that references the selected part. The part is the node that contains the triangles while the BoxBuild is the node that generates the triangles for the part.

The BoxBuild's yCentred field is ticked so that the box's pivot point will be in the box's center.

2. Add Anim node

An Anim node is added to the Scene node. The Anim node is set to loop and play endlessly. It does not yet animate anything but we can see the slider move for each cycle.

3. Add Function for Anim

The onFrame function is added to the Anim node. The onFrame function runs shortly before each frame is displayed in the 3d window and so this is the function we will add our animation script to. In the beeline to animation tutorial this function gets added automatically as a result from dragging the anim control points for the parts. It is fine to add animation this way and so if we do we can bypass this operation and jump to operation 5, however to make an object continuously spin evenly it is probably easier to manually drag in a function and a CodeLine   and then paste in the line of script.

4. Add spin script

A CodeLine node is added to the onFrame function and then the line of script:

p.rotation = Rotation(0, 0, 1, PI * 2 * v)

is entered into the CodeLine node.

There is no need to understand how this line works if we accept it makes the specified part "p" spin.

The line we use rotates our box on the Z axis but any axis can be specified for the Rotation value:

For the X axis:

p.rotation = Rotation(1, 0, 0, PI * 2 * v)

For the Y axis:

p.rotation = Rotation(0, 1, 0, PI * 2 * v)

For the Z axis:

p.rotation = Rotation(0, 0, 1, PI * 2 * v)

5. Add TouchSensor

A TouchSensor  node is added to the Scene. A TouchSensor watches out for any geometry that gets clicked on if the geometry belongs to a node that is a sibling of the TouchSensor and or a descendent of a node that is a sibling of the TouchSensor. Because both our TouchSensor node and our Seamless node (which contains the box) are direct descendents of the Scene node, they are siblings.

6. Add Function for TouchSensor

The onTouchTime function is added to the TouchSensor. This function runs it's script when the box is clicked.

7. Add toggle script

A CodeLine node is added to the onTouchTime function and then the line of script:

spin.pause = spin.play

is entered into the CodeLine node.

This single line of script is so simple anyone can understand it no matter how little programming experience one has had.

"spin" refers to our Anim node's name, so what ever we name our Anim node, we must use in our script too.

We know when using the anim bar we can directly "pause"(and resume) the animation using the button and "play" the animation using the  button.

   = play.

  = pause

The play button is always in the opposite logical state to the pause button. That is when the animation is playing the play button will be red (true) and the pause button will be black (false)  and when the pause button is red (true) the play button is black (false).

So by making pause = play for our script we create a toggle switch. (alternates between true and false)

8. Click box to switch off & on

To test our theory click the box to switch the animation on and off. This completes our tutorial switching animation on and off in 3d. We can apply this single line of script to any geometry we make and get the same results.

To see the 2 short follow up tutorials that show how an "if" statement can be used with ease go to:

Play for a Specified Number of Times  

[3d Modelling Software] [Tutorials] [Forum] [Features] [Download] [Gallery] [FAQ] [Worlds] [Avatars] [Links] [Thyme]