Basic Expressions Example


Table of contents

Jumping into Expressions

To understand expressions better, try the following simple exercises. Once you’re comfortable with the process and how to use the expression editor, you can go on to do more complicated expressions and setups.

Setting Up a Constant Expression

1. Get a primitive sphere (keep it simple!).

2. Select the sphere and press Ctrl+K (this opens the sphere's Local Transform property editor).

3. Right-click the animation icon (green box) of the local Position Y parameter and choose Set Expression.

Image:Cx0703a5.jpg

4. In the expression editor, enter the value of 5 in the expression pane and click the Apply button, but keep the expression editor open to take a peek at what you just did:

  • At the top of the expression editor is the parameter (sphere.kine.local.posy) that you've set to be equal to 5.
  • Notice that the Position Y animation icon has the letter C in it to indicate that it's a constant expression (where the value is a number).

Image:Cx070409.jpg

  • An Expression page is added to the Local Transform property editor so that you can easily edit this expression later, including adding comments.

Image:Cx070457.jpg

5. Close the expression editor.

Congratulations, you've just set the simplest kind of expression! The sphere is now fixed at 5 units on its Y axis. You won't be able to translate it anywhere else in this direction, but you can still move it in X and Z.

Trying it another way

Here's another way of creating a constant expression:

1. Get another primitive sphere and set its Translation Y value to 5.

2. Press Ctrl+K, right-click the Position Y parameter's animation icon, and choose Set Expression.

Notice the expression editor opens with the value of 5 already in it. You've just created a constant expression the same as in the first exercise.

Creating a Simple Equivalency Expression

You can also easily create simple A = B expressions between parameters:

1. Open a new scene and get a primitive sphere and a null.

2. Translate the null in X a little to offset it from the origin and the sphere.

3. Select the sphere and press Ctrl+K to open its Local Transform property editor. Lock this property editor to keep it open (click its keyhole icon).

4. Select the null and press Ctrl+K to open a Local Transform property editor for it.

5. Drag and drop the animation icon for the null’s Position X parameter to the sphere’s Position X parameter in the other property editor.

Image:Cx070529.jpg

The expression editor opens with the affected parameter is sphere.kine.local.posx. Its expression below is null.kine.local.posx, which means that the sphere takes its posx value from the null’s posx value.

The sphere’s animation icon for the Position X parameter now has an equal sign (=) in it to indicate the expression.

Image:Cx0704fa.jpg

Tip: If there was animation on the icon being dragged, the animation would have been copied to the other parameter, but no expression would have been set.

6. Test the expression by translating the null in X: the sphere should follow in X.

7. Close the expression editor and the two property editors to keep the scene uncluttered.

Trying it another way

Here’s another way to create an equivalency expression:

1. Select the sphere, press Ctrl+K, right-click the animation icon for Position Y, and choose Set Expression.

2. In the expression editor, type null.kine.local.posx in the white expression panel below and click the Apply button.

Now as the null is translated in X, the sphere moves diagonally, getting both its X and Y translation values from the null’s position X value.

Tip: You can also use the Parameter Connection Editor to create equivalency expressions.

Making a Bouncing Ball

You will put an expression on the soccer ball’s Position Y parameter to control its up-and-down movement.

1. Choose Get > Primitive > Polygon Mesh > Soccer Ball. You could also create any type of sphere or ball as you like.

2. In the explorer, activate the All+Animatable Parameters filter. Then expand the soccer ball’s Local Transform properties, right-click on the Position Y parameter or its animation icon, and choose Set Expression.

Image:Expr3.jpg


The expression editor opens with the PosY parameter already loaded. You will apply a sinus function.

3. In the editing pane in the expressions editor, replace the existing 0.0 with sin( ( Fc * 4 ) * 4 )

Fc stands for "current frame", so the formulat will be recalculated at each frame.

4. Click Validate and then Apply. If no errors are found, the expression is added to the parameter.

Click the Play button to check the animation. The ball is moving up and down a bit. Not quite what you want, but at least the ball is moving.

5. In the expression editor, choose View > Show Graph or press Ctrl+g. Now you can see the function curve of your formula. As you can see, the cuve is also going into negative values.

6. To make sure that the ball just hits the ground and then jumps up again, change the formula to:

abs( sin( ( Fc * 4 ) * 7 ) ) 

The abs (absolute) statement changes every negative value into the equivalent positive value — always handy for cases like this where you don't want negative values. Now check what the graph looks like.

Image:Expr5.jpg


Play the animation again to see how it looks. The ball is jumping and hitting the floor. Now it would be great if you could easily control how high the ball is jumping. Well, you can by adding a custom parameter.

7. From the Animation toolbar, choose Create > Parameter > New Custom Parameter Set, and name the set Force. If you check in the explorer under the soccerball’s node, you’ll see a square orange “C” icon which is the custom parameter set.

8. To create a custom parameter in this set, choose Create > Parameter > New Custom Parameter. In its property editor, name it Height, set the default value to 7, and set the Value Range - Minimum to 4 and the Maximum to 30. Click OK.

9. In the expression editor, replace the 7 with the name of the new custom parameter that represents the ball’s height (Y position) so that it looks like this:

abs( sin( Fc * 4 ) * SoccerBall.Force.height )

Apply the expression and click play with looping on.

In the custom parameter set, drag the Height slider back and forth to see that you can control how high the ball is jumping. The ball is jumping pretty slowly and might not look like a heavy basketball or soccer ball.

Now you will create a second custom parameter so that you can control the speed at which it bounces, which gives the illusion of weight.

10. Select the Force custom parameter set in the explorer and choose Create > Parameter > New Custom Parameter.

In its property editor, name it Speed, set the default value to 8, and set the Value Range - Minimum to 1 and the Maximum to 30. Click OK.

11. In the expression editor, replace the 4 with the name of the new custom parameter that represents the ball’s speed so that it looks like this:

abs(sin( Fc * SoccerBall.Force.speed ) * SoccerBall.Force.height )

Apply the expression and play the animation while dragging the two custom sliders.

Image:Expr10.jpg


Although it’s moving up and down, you’ll notice that the ball is not yet moving forward. You’ll add an expression to the soccer ball’s Position Z parameter to remedy this.

12. In the explorer, right-click on the soccer ball’s Position Z parameter or its animation icon and choose Set Expression.

In the expression editor, replace the 0.0 with the formula Fc * 0.2 and apply the expression. The ball is now constantly moving forwards.

Now you will create a third custom parameter to control the distance the ball is moving.

13. Select the Force custom parameter set in the explorer and choose Create > Parameter > New Custom Parameter.

In its property editor, name it Distance, set the default value to 0.2, and set the Value Range - Minimum to 0 and the Maximum to 3. Click OK.

14. In the expression editor, replace the 0.2 with the name of the new custom parameter that represents the ball’s distance so that it looks like this:

Fc * SoccerBall.Force.distance 

Apply the expression and play the animation while dragging the sliders in combination to get the animation you want. You can also key the custom parameters to make the animation you want, you could set expressions for the ball's rotation to get a spin, or animate the scaling of the ball to get a squash and stretch effect.

Relational Expressions

In the example above called Jumping into Expressions, you saw how Parameter B can directly take the value of Parameter A. But you can also use the values from parameter A and modify them.

1. Create a sphere and a cube, just to keep things simple.

2. Animate the cube moving back and forth on its X axis.

3. Select the sphere and press Ctrl+K to open its Local Transform property editor.

4. Right-click the animation icon of the Position X parameter and choose Set Expression.

5. Enter the following into the expression editor:

cube.kine.local.posx * -1

6. Validate and apply the expression and close the expression editor. Notice the expression on its own page inside the Local Transform property editor. The animation icon for the Position X parameter now has an arrow in it indicating that the parameter is being controlled by a relationship.

7. Play the animation and notice that as the cube moves back and forth on the X axis, the sphere moves the same but in the opposite direction (the -1 value did this).

If you plotted the Position X animation of the sphere, you would see that its fcurve is the same as the cube’s Position X fcurve, except that it would be flipped vertically.

8. Edit the expression in the property editor to the following:

cube.kine.local.posx * -0.5 

Image:Cx073fc3.jpg

Notice how the sphere now moves in the opposite direction, but only half as far. If you plotted its Position X fcurve, you would see that it’s upside-down and half as tall as the cube’s Position X fcurve.

By multiplying the value of the cube’s Position X parameter, you’re basically altering the shape of the source fcurve. You can make the fcurve taller by multiplying by numbers greater than 1, or shorter by dividing. Adding and subtracting values to the parameter shifts the resulting fcurve left and right.

9. With the sphere selected, choose Create > Parameter > New Custom Parameter Set from the Animate toolbar and name the set Control.

10. Choose Create > Parameter > New Custom Parameter (or press Shift+P) to create a custom parameter for the set and name it slider.

11. Set the Minimum value range to -10 and Maximum to 10 and leave everything else as a default.

12. Edit the expression for the sphere’s Position X parameter again, but this time as follows:

cube.kine.local.posx * sphere.Control.slider

13. In the explorer, find the Control custom parameter set under the sphere’s node and click its icon to open up its property editor. Inside, there should be the parameter you created (slider).

Image:Cx0740d4.jpg

14. Loop the animation of the cube while dragging the custom parameter’s slider across its range of values.

Instead of multiplying the cube’s Position X parameter by a number like -1 or -0.5, you multiplied it by the slider’s value. You can see that this slider now controls the amplitude of how the sphere translates in X relative to the cube translating in X.

You can imagine that when the slider is set to 1, the sphere’s Position X fcurve matches the cube’s Position X fcurve precisely. As you increase the slider value, the sphere’s Position X fcurve grows taller. As you decrease the value to zero, the fcurve flattens out, only to grow again in the opposite direction as you move the slider to negative values.

15. Change the expression so that instead of multiplying, you add the two parameters together:

cube.kine.local.posx + sphere.Control.slider

Now as you move the slider, the sphere shifts to the right or left.

This sort of mathematical manipulation of fcurves is exactly what’s taught in math analysis or pre-calculus. Basically, it’s drawing pictures with math equations. A pre-calculus text book can give you all the information you’ll need to know about how to manipulate the shapes of functions using different math operations.

Conditional Expressions

If you’re getting into expressions, try this simple exercise using a conditional expression.

1. In a new scene, get a primitive sphere.

2. Get two primitive nulls and translate them so that one is above the sphere and the other is below so that they have noticeably different Y positions.

3. Set an expression on the sphere’s local Position Y parameter:

cond( sphere.kine.local.posx > 0, null.kine.local.posy, null1.kine.local.posy )

This is a conditional expression, which works according to this format:

cond( True or False Condition, This Value if True, This Value if False )

Tip: If you choose Functions > Conditions > Condition in the expression editor, this formula is added for you in the expression pane so that you don’t have to type it all out.

4. Translate the sphere on its X axis to see how it jumps when the value is greater than or less zero.

If it’s greater than zero, the Position Y value from the first null is used; otherwise, the Position Y value from the second null (null1) is used.

Image:Cx074155.jpg

In the image on the left, the sphere’s Position X value is greater than zero, so it takes the Position Y value of the null object (circled on the left).

In the image on the right, the sphere’s Position X value is less than zero, so it takes the Position Y value of the null1 object (circled on the right).

Oscillation Using Cosine

And now for a simple oscillation using the cosine function and the frame rate of the scene’s playback.

1. In a new scene, get a primitive sphere.

2. Create a custom parameter set for it called Control.

3. Create two custom parameters for this set, called amp and freq, each with value ranges between -100 and 100.

4. Set the following expression on the sphere’s local Position X parameter:

cos( T * 90 * sphere.Control.freq ) * sphere.Control.amp

Tip: You can also find the cosine function in Functions > Trigonometry > Cosine in the expression editor.

T represents Time and is dependent on the frame rate setting of your playback. If it is set to 30 frames per second, T will equal 1 at frame 30.

5. Set the playback in a loop, play around with the custom amp and freq sliders (set the freq first).

This page was last modified 20:22, 14 Dec 2010.
This page has been accessed 30196 times.

© Copyright 2009 Autodesk Inc. All Rights Reserved. Privacy Policy | Legal Notices and Trademarks | Report Piracy