Fun with Sin


To create the sine wave curve pictured in the first example below, consider the illustration above. Imagine the hypotenuse, C, of the triangle sweeping around the circle in a counter-clockwise rotation. At each point along the sweep, you get back the length of A on the Y axis. The circle is 1 unit in radius, so the value goes from 0 to 1, then back to 0, then 0 to -1 and back to 0 again.The reason you get the "sine wave" curve below is that you are progressing through the values of the 'sweep' over time.

The sin() function uses as its input the angle, , of the counter-clockwise 'sweep', in radians, not degrees.
There are 2* PI radians in 360 degrees.
radians = degrees * (PI / 180)
degrees = radians * (180 / PI)
More easily, in MEL you can convert using the commands "deg_to_rad" and "rad_to_deg".

In these examples, Maya's predefined attribute called "time" (the current time in seconds) is input to the sin function. This means at 3.1415927 seconds, we've gone 180 degrees around the circle, which gives the progression from 0 to 1 and back to 0. At 6.28 seconds we are the rest of the way around (0 to -1 to 0) and the cycle repeats.

Note: the curves graphed below are the result of baking the expressions into keyframes. You won't see these curves in the graph editor when using expressions.

function description example
sin( )

returns the sine of the given value.
The sine ratio is a value between -1 and 1.

With a steadily increasing or decreasing argument, the cos function returns steadily increasing or decreasing values between -1 and 1. This is useful for creating rhythmic, oscillating changes in attribute values.

translateY = sin(time);

you can change the frequency of the sine wave:
translateY = sin(time*5);

you can change the amplitude:
translateY = sin(time) * 2;

and offset the value:
translateY = sin(time) + 6;

or use all in one expression:
translateY = (sin(time * frequencyMultiplier) * amplitude) + offset;

cos( )

The cosine ratio is a value between -1 and 1.

The cos function works like the sin function except its return values are 90 degrees, or pi/2, out of phase.

translateY = cos(time);

The cos function is very handy for making an "opposing" action to an object being controlled by the sin function.
abs( ) Returns the absolute value of number. The absolute value of an integer or floating point number is the number without its positive or negative sign. The absolute value of a vector is a vector with components stripped of negative signs. translateY = abs( sin(time) );

the absolute value of the sine wave makes for a good bouncing action.
max( ) Returns the larger of two floating point numbers.

float max(float number, float number)

number is a number you want to compare.

translateY = max( 0, sin(time) );

max works like a clamp. The sine wave is not allowed to go below 0.
min( ) Returns the lesser of two floating point numbers.

float min( float number, float number)

number is a number you want to compare.

translateY = min( 0, sin(time) );

The sine wave is not allowed to go above 0.
Consider also you can use min and max combined together for some uses that need clipping on both high and low values:
translateY =
min( 0.5, max( 0, sin(time) ) );

linstep( ) Returns a value from 0 to 1 that represents a parameter's proportional distance between a minimum and maximum value. This function lets you increase an attribute such as opacity from 0 to 1 linearly over a time range.

float linstep(float start, float end, float parameter)

start and end specifies the minimum and maximum values.

parameter is the value you want to use to generate the proportional number.

If parameter is less than start, linstep returns 0.

If parameter is greater than end, linstep returns 1.

translateY = linstep( 0, 1, sin(time) );

The result is very similar to "max" above, but keep in mind that the first two values are the upper and lower 'thresholds' that are allowable, and when one or the other is reached, the output is limited between 0 and 1 ONLY.
For example:
translateY = linstep( 0, 0.25, sin(time) );

These are just a few functions available to use in expressions or MEL scripts, and are good candidates for use with sin( ). Find more math functions by using the Maya online docs menu: Help > Mel Command Reference.