Adding motion in RhinoScript
This tutorial explains step-by-step how to setup animation in any RhinoScript. In this particular and simple case, we are going to move a sphere on a path that is defined by a curve. This example can be, then, expanded to more complex scenarios: for example, using the new animation to trigger some response, as in the case of the component wall.
In cartoons and movies, animation is achieved by rapidly displaying a sequence of pictures that have a common structure. This method creates the illusion of movement, because our brains will, as they are used to, reconstruct the pattern of motion from the static pieces.
How many frames
As we are going to animate an object, we will make it follow a curve. We need, therefore, to sample this curve in a number of (small) intervals. The number of intervals will determine the number of final frames we produce. If you plan to show your animation, later, on a TV screen, then you can take the frame rates of the PAL or NTSC systems: the European PAL has 25 frames per second (fps) and the American NTSC 29,97. If you plan to show your video online, you will need relatively fewer images: at least 18 fps, but most times 24 or 25 fps.
- For a European minute, therefore, the computer will need to render (60 s * 25 frames/s = ) 1500 frames.
- Similarly, for an American minute we will need (60 s *Ā 29,97 frames/s = ) 1789 frames.
Subdividing the curve
To make the movement better controllable, we can, instead of subdividing the curve by Euclidean distance, evaluate it at a fraction of its domain (in its own parameterization). We will therefore be able of applying a weight to a control point, and it will influence the speed in proximity of the respective knot.
- A higher weight coefficient “slows” down the movement in the area.
- Two or more control points close to each other will make the animation basically stop for some time.
Moving the objects
RhinoScript gives us a number of methods to define the position of objects. For example, we can calculate the volume centroid of closed Breps, or the area centroid of open meshes. We will use these points to define the original location of our objects (O).
Because we want to follow a curve, we will need to move the object to the curve beginning, or S. For each frame, we will move by a t parameter farther on the curve. To avoid small summing errors, the script takes again the original coordinate and then sums the vector OS and SN, and does NOT move every time from the previous location (P) to the next (N). This would be subject to errors for small numbers, or for many frames.
Download
- Draw a curve, then run animator.rvb first and activity.rvb at last.
Implementation
animator.rvb
The animation details like the ID of the object we move and the original position are saved in a class called animator. As the namespace for VBScript is shared, we can define the class “animator” only once. Therefore, you can load the animator once for every Rhinoceros session. The class does the job of finding out which type of object we are dealing with on its own, so we just need to feed it an ID after initialization with the method setUp(). When we destroy the object, such as in the case we stop the script by pressing ESC or we are finished with our script, the object original position will be restored.
activity.rvb
The activity file will create one sphere and look for the first instance of a curve in the document. Make sure that it can find it. Then, it will make the sphere follow the curve in 750 frames. The constant DontRenderUpTo will skip the rendering part of the loop for all 750 frames.
Other examples
Many other ways to achieve motion in Rhino are explained on the Grasshopper tools page, at the renderAnimation link.
August 6th, 2009 at 1:39 pm
Thank you very much. These are code samples that I can really learn from.
Thank you for sharing them.
March 10th, 2010 at 3:41 pm
I’m trying this script but when i run the animator.rvb i get an error “Name redefined” on Line 1. I’m using Rhino 4 SR7.
Thanks for your work.
March 10th, 2010 at 3:49 pm
Hi Alto,
you need to, and can only, run “animator.rvb” once for each session of Rhino, unless you use _ResetRhinoScript command in between. Please have a look at “Implementation” -> Animator.rvb.
– Giulio