Seed Random to the Rescue. For the first half second, the seed calculates out to be 0. So for every frame in the first half second, the call to random generates the same random Position based on a seed of 0. After the first half second, the seed changes to 1 and the random Position changes. Dec 23, 2015 Expressions can really change the way you work within After Effects. If you want to get serious about it, you can start writing your own expressions to do all kinds of things for you. But you can also start building a library of simple expressions like the ones above to be more efficient and give you more creative control over your work.
Use the After Effects expression elements along with standardJavaScript elements to write your expressions. You can use the ExpressionLanguage menu at any time to insert methods and attributes intoan expression, and you can use the pick whip at any time to insertproperties.
If an argument description contains an equal sign (=)and a value (such as t=time or width=.2),then the argument uses the included default value if you don’t specifya different value.
Some argument descriptions include a number in square brackets—thisnumber indicates the dimension of the expected property or Array.
Some return-value descriptions include a number in square brackets—this numberspecifies the dimension of the returned property or Array. If aspecific dimension is not included, the dimension of the returnedArray depends on the dimension of the input.
The W3Schools JavaScript reference website providesinformation for the standard JavaScript language, including pagesfor the JavaScript Math and String objects.
After Effects uses JavaScript engine when evaluating expressions. The performance of evaluating expressions during renders can be up to 5x faster than the Legacy ExtendScript engine.
On Windows, After Effects uses the V8 open-source JavaScript engine. This provides a more modern version of JavaScript than available in ExtendScript, which only supports the 3rd Edition of the ECMA-262 Standard.
To choose whether a project uses the JavaScript engine or Legacy ExtendScript, in the Project Settings dialog, click the Expressions tab, and change the Expressions Engine option. Projects saved in previous versions of After Effects default to Legacy ExtendScript.
For more information on specific syntax differences between the JavaScript and Legacy ExtendScript expression engines, see Syntax differences between the JavaScript and Legacy ExtendScript expression engines.
- Higher mathematical precision in floating-point operations.
- Array and string methods from modern JavaScript can be used.
- Other modern JavaScript objects like typed arrays, usage of keywords 'let' and 'const', and many other additions made in recent versions of the ECMA-262 Standard are available.
Differences between the JavaScript engine and ExtendScript
- The syntax for if/else statements is strict in the JavaScript engine and need to be written for standardized JavaScript.
- When accessing the indexes of characters on a Text Layer as an array, use text.sourceText.value[i] instead of text.sourceText[i].
- Older snake_case expression methods such as this_comp and to_world, which had been previously deprecated in ExtendScript) are not supported.
- Simplified 'this(arg)' syntax is not supported.
Global objects, attributes, andmethods (expression reference)
Return type: Comp.
Argument type: name isa String.
Retrieves another composition by name.
Return type: Footage.
Argument type: name isa String.
Retrieves a footage item by name.
Return type: Comp.
Represents the composition containingthe expression.
Return type: Layer, Light, or Camera.
Represents thelayer containing the expression. Because thisLayer isthe default object, its use is optional. For example, you can startan expression with thisLayer.width or width andget the same result.
Return type: Property.
Represents the property containingthe expression. For example, if you write an expression on the Rotationproperty, you can start an expression with thisProperty torefer to the Rotation property.
Return type: Number.
Represents the composition time,in seconds, at which the expression is being evaluated.
Return type: Number.
Returns the project color depthvalue. For example, colorDepth returns 16 whenthe project color depth is 16 bits per channel.
Return type: Number.
Argument type: framesPerSecond isa Number.
The framesPerSecond value becomesthe frame rate from which the rest of the expression operates. Thisexpression allows you to set the frame rate for a property to belower than the frame rate of the composition. For example, the followingexpression updates the property value with a random value once per second:
posterizeTime(1); random()Return type: Number, Array, or String.
Representsthe value at the current time for the property containing the expression.
timeToFrames(t = time + thisComp.displayStartTime,fps = 1.0 / thisComp.frameDuration, isDuration = false)
Return type: Number.
Argument type: t and fps areNumbers; isDuration is a Boolean.
Convertsthe value of t, which defaults to the currentcomposition time, to an integer number of frames. The number offrames per second is specified in the fps argument,which defaults to the frame rate of the current composition (1.0 / thisComp.frameDuration).The isDuration argument, which defaults to false,should be true if the t value represents a differencebetween two times instead of an absolute time. Absolute times arerounded down toward negative infinity; durations are rounded awayfrom zero (up for positive values).
framesToTime(frames, fps = 1.0 / thisComp.frameDuration)
Argument type: frames and fps areNumbers.
timeToTimecode(t = time + thisComp.displayStartTime,timecodeBase = 30, isDuration = false)
Return type: String.
Argument type: t and timecodeBase areNumbers; isDuration is a Boolean.
Convertsthe value of t to a String representing timecode.See timeToFrames for an explanation of the t and isDuration arguments.The timecodeBase value, which defaults to 30,specifies the number of frames in one second.
timeToNTSCTimecode(t = time + thisComp.displayStartTime,ntscDropFrame = false, isDuration = false)
Return type: String.
Argument type: t isa Number, ntscDropFrame and isDuration areBooleans.
Converts t to a String representingNTSC timecode. See timeToFrames for an explanationof the t and isDuration arguments.If ntscDropFrame is false (the default), theresult String is NTSC non-drop-frame timecode. If ntscDropFrame is true,the result String is NTSC drop-frame timecode.
timeToFeetAndFrames(t = time + thisComp.displayStartTime,fps = 1.0 / thisComp.frameDuration, framesPerFoot = 16, isDuration= false)
Return type: String.
Argument type: t, fps,and framesPerFoot are Numbers; isDuration isa Boolean.
Converts the value of t toa String representing feet of film and frames. See timeToFrames foran explanation of the t, fps,and isDuration arguments. The framesPerFoot argumentspecifies the number of frames in one foot of film. It defaultsto 16, which is the most common rate for 35mm footage.
timeToCurrentFormat(t = time + thisComp.displayStartTime,fps = 1.0 / thisComp.frameDuration, isDuration = false)
Return type: String.
Argument type: t and fps are Numbers; isDuration is a Boolean.
Converts the value of t to a String representing time in the current Project Settings display format. See timeToFrames for a definition of all the arguments.
An optional ntscDropFrame argument was added to the timeToCurrentFormat() function in After Effects CS5.5 and later. Default: ntscDropFrame = thisComp.ntscDropFrame.
Argument type: vec1 and vec2 areArrays.
Argument type: vec1 and vec2 areArrays.
Argument type: vec isan Array, amount is a Number.
Multipliesevery element of the vector by the amount.
Argument type: vec isan Array, amount is a Number.
Dividesevery element of the vector by the amount.
Argument type: value, limit1, and limit2 are Numbers or Arrays.
Argument type: vec1 and vec2 areArrays.
Returns the dot (inner) product of the vector arguments.
Argument type: vec1 and vec2 areArrays [2 or 3].
Argument type: vec isan Array.
Argument type: vec isan Array.
Returns the length of vector vec.
Argument type: point1 and point2 areArrays.
length(position, pointOfInterest)Argument type: fromPoint and atPoint are Arrays [3].
Argument type: offset isa Number, timeless is a Boolean.
seedRandom(123456, true); random()*100The multiplicationby 100 in this example converts the value in the range 0–1 returnedby the random method into a number in the range0–100; this range is more typically useful for the Opacity property,which has values from 0% to 100%.
Returns a random number in the range 0–1.
Argument type: maxValOrArray isa Number or Array.
Argument type: minValOrArray and maxValOrArray areNumbers or Arrays.
Argument type: maxValOrArray isa Number or Array.
Argument type: minValOrArray and maxValOrArray areNumbers or Arrays.
Argument type: valOrArray is a Number or an Array [2 or 3].
For additional explanations and examples of the Interpolation methods, see JJ Gifford’s website.
Argument type: t, tMin, and tMax are Numbers, and value1 and value2 are Numbers or Arrays.
Argument type: t isa Number, and value1 and value2 areNumbers or Arrays.
Argument type: t is a Number, and value1 and value2 are Numbers or Arrays.
Argument type: t, tMin, and tMax are Numbers, and value1 and value2 are Numbers or Arrays.
Argument type: t isa Number, and value1 and value2 areNumbers or Arrays.
Argument type: t, tMin,and tMax are Numbers, and value1 and value2 are Numbersor Arrays.
Argument type: t isa Number, and value1 and value2 areNumbers or Arrays.
Argument type: t, tMin,and tMax are Numbers, and value1 and value2 are Numbersor Arrays.
Argument type: rgbaArray isan Array [4].
rgbToHsl.effect('Change Color')('Color To Change')Argument type: hslaArray isan Array [4].
Argument type: degrees isa Number.
Argument type: radians isa Number.
Comp attributes and methods (expressionreference)
Return type: Layer, Light, or Camera.
Argument type: index isa Number.
Retrieves the layer by number (order in the Timelinepanel). Example: thisComp.layer(3)
Return type: Layer, Light, or Camera.
Argument type: name isa String.
thisComp.layer('Solid 1')Return type: Layer, Light, or Camera.
Argument type: otherLayer isa Layer object, and relIndex is a Number.
Argument type: index is a Number.
Argument type: name isa String.
thisComp.marker.key('0').time thisComp.marker.nearestKey(1).timeThisexpression returns the time of the composition marker nearest tothe current time:
thisComp.marker.nearestKey(time).timeReturns the total number of compositionmarkers in the composition.
Returns the number of layersin the composition.
Returns the composition width,in pixels.
[thisComp.width/2, thisComp.height/2]Returns the composition height,in pixels.
Returns the composition duration,in seconds.
Returns true if the timecodeis in drop-frame format. (After Effects CS5.5and later.)
Returns the composition starttime, in seconds.
Returns the duration of a frame,in seconds.
Returns the shutter-angle valueof the composition, in degrees.
Returns the shutter phase ofthe composition, in degrees.
Returns the background colorof the composition.
Returns the pixel aspect ratioof the composition.
Returns the name of the composition.
Footage attributes and methods(expression reference)
Returns the width of the footageitem, in pixels.
Returns the height of the footageitem, in pixels.
Returns the duration of the footageitem, in seconds.
Returns the duration of a framein the footage item, in seconds.
Returns true if the timecodeis in drop-frame format. (After Effects CS5.5 and later.)
Returns the pixel aspect ratioof the footage item.
Returns the name of the footageitem as shown in the Project panel.
Layer Sub-objects attributes andmethods (expression reference)
Returns the layer source corresponding to time t. (After Effects CS5.5 and later.)
sourceRectAtTime(t = time, includeExtents = false)
Argument type: name isa String.
effect('Fast Blur')('Blurriness')Argument type: index isa Number.
Argument type: name isa String.
mask('Mask 1')Argument type: index isa Number.
Layer General attributes and methods(expression reference)
Returns the width of the layer,in pixels. It is the same as source.width.
Returns the height of the layer,in pixels. It is the same as source.height.
Returns the index number of thelayer in the composition.
Return type: Layer, Light, or Camera.
Returns theparent Layer object of the layer, if it has one. Example: position[0] + parent.width
Returns true if the layer hasa parent or false if it doesn’t.
idx = index; if (hasParent) { idx = parent.index; } thisComp.layer(idx).position.wiggle(5,20)Returns the In point of the layer, in seconds.
Returns the Out point of thelayer, in seconds.
Returns the start time of thelayer, in seconds.
Returns true if the layer hasvideo, or false if it doesn’t.
Returns true if the layer hasaudio or false if it doesn’t.
Returns true if the Video switch ison for the layer; false otherwise.
sampleImage(point, radius = [.5, .5], postEffect=true,t=time)
Return type: Array [4].
Argument type: point is an Array [2], radius is an Array [2], postEffect is a Boolean, and t is a Number.
Samples the color and alpha channel values of a layer and returns the average alpha-weighted value of the pixels within the specified distance of the point as an array: [red, green, blue, alpha]. If postEffect is true, the sampled values are for the layer after masks and effects on that layer have been rendered; if postEffect is false, the sampled values are for the layer before masks and effects have been rendered. The input value point is in layer space; the point [0,0] is the center of the upper-left pixel in the layer. The input value radius specifies the horizontal and vertical distance from the sample center to the edges of the sampled rectangle. The default value samples one pixel.
Using sampleImage in an expression no longer disables multiprocessing.
thisComp.layer(1).sampleImage([100, 100], [2, 1.5])
Dan Ebberts provides an example of how to use the sampleImage method on his MotionScript website.
Layer Properties attributes andmethods (expression reference)
Return type: Property [2 or 3].
Returns the anchorpoint value of the layer in the coordinate system of the layer (layerspace).
Return type: Property [2 or 3].
Return type: Property [2 or 3].
Returns the scalevalue of the layer, expressed as a percentage.
Returns the opacity value forthe layer, expressed as a percentage.
Returns the value of the TimeRemap property, in seconds, if Time Remap is enabled.
Argument type: index isa Number.
Returns the MarkerKey object of the layer markerwith the specified index.
Argument type: name isa String.
m1 = marker.key('Start').time; m2 = marker.key('End').time; linear(time, m1, m2, 0, 100); marker.nearestKey(1).timeThisexpression returns the time of the marker on the layer nearest tothe current time:
marker.nearestKey(time).timeReturns the total number of markerson the layer.
Returns the name of the layer.
Layer 3D attributes and methods(expression reference)
Returns the 3D orientationvalue, in degrees, for a 3D layer.
Returns the x rotation value,in degrees, for a 3D layer.
Returns the y rotation value,in degrees, for a 3D layer.
Returns the z rotation value,in degrees, for a 3D layer.
Returns the value of the LightTransmission property for a 3D layer.
Returns a value of 1.0 if thelayer casts shadows.
Returns a value of 1.0 if thelayer accepts shadows.
Returns a value of 1.0 if thelayer accepts lights.
Returns the ambient componentvalue as a percentage.
Returns the diffuse componentvalue as a percentage.
Returns the specular componentvalue as a percentage.
Returns the shininess componentvalue as a percentage.
Returns the metal componentvalue as a percentage.
Layer Space Transforms methods(expression reference)
Argument type: point isan Array [2 or 3], and t is a Number.
Transformsa point from layer space to composition space.
Argument type: point isan Array [2 or 3], and t is a Number.
Argument type: point is an Array [2 or 3], and t is a Number.
Transforms a point from layer space to view-independent world space. Example:
toWorld.effect('Bulge')('Bulge Center')
Argument type: point isan Array [2 or 3], and t is a Number.
Transformsa point from world space to layer space. Example: fromWorld(thisComp.layer(2).position)
Argument type: vec isan Array [2 or 3], and t is a Number.
Transformsa vector from layer space to composition space. Example: toCompVec([1,0])
Argument type: vec isan Array [2 or 3], and t is a Number.
Transformsa vector from composition space to layer space. Example (2D layer):
dir=sub(position, thisComp.layer(2).position); fromCompVec(dir)Argument type: vec isan Array [2 or 3], and t is a Number.
Argument type: vec isan Array [2 or 3], and t is a Number.
Transformsa vector from world space to layer space. Example: fromWorld(thisComp.layer(2).position)
Argument type: point isan Array [2 or 3], and t is a Number.
Camera attributes and methods (expressionreference)
Returns the point of interestvalues of a camera in world space.
Returns the zoom values ofa camera in pixels.
cam = thisComp.activeCamera; distance = length(sub(position, cam.position)); scale * distance / cam.zoom;Returns the focus distancevalue of a camera, in pixels.
Returns the aperture valueof a camera, in pixels.
Returns the blur level valueof a camera as a percentage.
Light attributes and methods (expressionreference)
Returns the point of interestvalues for a light in world space.
Returns the intensity valuesof a light as a percentage.
Returns the color valueof a light.
Returns the cone angle of alight, in degrees.
Returns the cone feather valueof a light as a percentage.
Returns the shadow darknessvalue of a light as a percentage.
Returns the shadow diffusionvalue of a light, in pixels.
Effect attributes and methods (expressionreference)
Returns true if the effect isturned on (the Effect switch isselected).
Argument type: name isa String.
Returns a property within an effect. Effect controlpoints are always in layer space. Example:
effect('Bulge').param('Bulge Height')Argument type: index isa Number.
Mask attributes and methods (expressionreference)
Returns the opacity value ofa mask as a percentage.
Returns the feather value ofa mask, in pixels.
Returns the expansion valueof a mask, in pixels.
Returns true if the mask isinverted or false if it is not.
Property attributes and methods(expression reference)
Return type: Number, Array, or String.
Returns thevalue of a property at the current time.
Returns the value of a property at the specified time, in seconds.
Returns the temporal velocity value at the specifiedtime.
Returns the spatial speed value at the specifiedtime.
Argument type: freq, amp, octaves, amp_mult, and t are Numbers.
Randomly shakes (wiggles) the value of the property.
freq value is the frequency in wiggles per second.
amp value is the amplitude in units of the property to which it is applied.
v = wiggle(5, 10); [v[0], v[0]]
This example, on a two-dimensional property, wiggles only along the y-axis:
freq = 3; amp = 50; w = wiggle(freq,amp); [value[0],w[1]];
temporalWiggle(freq, amp, octaves=1, amp_mult=.5, t=time)
Argument type: freq, amp, octaves, amp_mult,and t are Numbers.
Argument type: width, samples,and t are Numbers.
loop type | result |
---|---|
cycle | (default) Repeats the specified segment. |
pingpong | Repeats the specified segment, alternating between forward and backward. |
offset | Repeats the specified segment, but offsets each cycle by the difference in the value of the property at the start and end of the segment, multiplied by the number of times the segment has looped. |
continue | Does not repeat the specified segment, but continues to animate a property based on the velocity at the first or last keyframe. For example, if the last keyframe of a Scale property of a layer is 100%, the layer continues to scale from 100% to the Out point, instead of looping directly back to the Out point. This type does not accept a keyframes or duration argument. |
Return type: Key or MarkerKey.
Argument type: index isa Number.
Returns the Key or MarkerKey object by number.For example, key(1) returns the first keyframe.
Argument type: markerName isa String.
Returns the MarkerKey object with this name. Useonly on marker properties.
Return type: Key or MarkerKey.
Returns the Key orMarkerKey object nearest to a designated time.
Returns the number of keyframes on a property. Returns the number of markers on a marker property.
The numProperties method for propertyGroup returnsthe number of properties in the property group.
thisProperty.propertyGroup(1).numPropertiesReturns the name of the propertyor property group.
Example: Animating with the propertyGroup methodand propertyIndex attribute
A. propertyGroup(4) B. propertyGroup(3) C. propertyGroup(2) D. propertyGroup(1) E. Position propertyIndexvalue is 2; Rotation propertyIndex value is 4.
Key attributes and methods (expressionreference)
Returns the value ofthe keyframe.
Returns the time of the keyframe.
Returns the index of the keyframe.
Duration, in seconds, of marker.
Contents of Comment field inmarker dialog box.
Contents of Chapter field inmarker dialog box.
Contents of URL field in markerdialog box.
Contents of Frame Target fieldin marker dialog box.
Setting for cue point type inmarker dialog box. True for Event; false for Navigation.
Contents of cue point Name fieldin marker dialog box.
Return type: associative array of String values.
Contentsof Parameter Name and Parameter Value fields in marker dialog box.
thisComp.marker.nearestKey(time).parameters['background color']MarkerValue.protectedRegion (expression reference)
thisComp.marker.key(index).protectedRegion
When true, the composition marker behaves as a protected region.
Expression access to paths points on shapes, masks, and brush strokes (expression reference)
You can use expressions to read and write the x and y coordinates of path points, or vertices, for:
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
Array of number pair arrays, rounded to the fourth decimal place.
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
Array of number pair arrays, rounded to the fourth decimal place.
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
Array of number pair arrays, rounded to the fourth decimal place.
percentage | Number between 0 and 1, optional. The percentage along the arc-length of the path to sample. Values smaller than 0 and larger than 1 are clipped. Default is 0.5. |
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
percentage | Number between 0 and 1, optional. The percentage along the arc-length of the path to sample. Values smaller than 0 and larger than 1 are clipped. Default is 0.5. |
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
percentage | Number between 0 and 1, optional. The percentage along the arc-length of the path to sample. Values smaller than 0 and larger than 1 are clipped. Default is 0.5. |
t | Number, optional. The composition time (in seconds) at which to sample the path. Default is time (the current time). |
myMask = mask('Mask 1').path;
myMask.createPath(myMask.points());
points | An array of length 1 or greater containing number pair arrays representing the [x,y] coordinates of the path points. Required unless no parameters are passed (i.e., createPath()). Default is [[0,0], [100,0], [100,100], [0,100]]. |
is_closed | Boolean, optional. Determines if the mask is closed. If true, the last point will be connected to the first point. Default is true. |
inTangents | An array containing number pair arrays representing the [x,y] offset coordinates of the outgoing tangent handles to the path points. Required unless no parameters are passed (i.e., createPath()). The array length must be the same as points, or you can pass an empty array ([]), which will assume the same length as points and [0,0] for all tangents. Default is an empty array. |
outTangents | An array containing number pair arrays representing the [x,y] offset coordinates of the incoming tangent handles to the path points. Required unless no parameters are passed (i.e., createPath()). The array length must be the same as points, or you can pass an empty array ([]), which will assume the same length as points and [0,0] for all tangents. Default is an empty array. |
var myData = eval(footage('sample.json').sourceText);
String, the contents of the .JSON file; read-only.
var myData = footage('sample.json').sourceData;
An array of sourceData objects; read-only.
footage('sample.mgjson').dataValue([0]) returns data of the first child.
footage('sample.mgjson').dataValue([1][0]) returns data of the first child in the second group.
dataPath | Array, required. The path in the hierarchy to a static or dynamic data stream. |
- footage('sample.mgjson').dataKeyCount([0]) returns the count of samples for the first child
- footage('sample.mgjson').dataKeyCount([1][0]) returns the count of samples for the second group
dataPath | Array, required. The path in the hierarchy to a static or dynamic data stream. |
The number of samples in the dynamic data stream.
footage('sample.mgjson').dataKeyTimes([0], 1, 3)
dataPath | Array, required. The path in the hierarchy to a dynamic data stream. |
t0 | Number, optional. The start time, in seconds, of the span from which to return samples. Defaults to startTime. |
t1 | Number, optional. The end time, in seconds, of the span from which to return samples. Defaults to endTime. |
Array of numbers representing the sample times.
dataPath | Array, required. The path in the hierarchy to a dynamic data stream. |
t0 | Number, optional. The start time, in seconds, of the span from which to return samples. Defaults to startTime. |
t1 | Number, optional. The end time, in seconds, of the span from which to return samples. Defaults to endTime. |
Array of numbers representing the sample values.
Hex to RGB color conversion method (expression reference)
hexString | String representing an hex triplet (6 digits, no alpha channel) or quartet (8 digits, includes alpha channel) containing only numerals or characters A–F. Optional leading characters 0x, 0X, or # are ignored. Characters beyond 8 digits are ignored. |
Any of the following will return [1.0, 0.0, 1.0, 1.0]:
- hexToRgb('FF00FF')
- hexToRgb('#FF00FF')
- hexToRgb('0xFF00FF')
- hexToRgb('0XFF00FFFF') Note: 8-digit hex quartet; last two digits set alpha to 1.0.
Twitter™ and Facebook posts are not covered under the terms of Creative Commons.