Skip to main content

Functions

Overview

Functions allow you to structure your block program. Just as a precomp wraps an entire collection of layers into a single layer, a function can wrap a stack of blocks into a single block.

Creating a function

Create Function

Block procedures_defnoreturn

With the Create Function block you can make your own function. Just replace the text "do something" with a text which describes what your function does and plug the blocks inside of it, which the function should execute.

Example

Creating a function in Automation Blocks

Here we replaced "do something" with "create my usual bins" and plugged the blocks inside which create project bins for audio, video and precomps. As a result, among the blocks in the category "functions" you now find a new block called "create my usual bins", which creates all these bins. You can think of this little block as a shorthand for the three blocks that you have plugged into the function definition.

Structure Your Scripts

If your scripts tend to be long, it can help to structure them into individual parts using functions. Say you need to first import some footage, then create some comps, and finally put something in the render queue. You can wrap each of these three tasks into a separate function, so that your main script just consists of three function calls.

This is a very good way of documenting what your script is doing. If you wrap a part of your code into a function, you effectively give that part of your code a descriptive name and isolate it from other parts.

Collapse Functions

You can right-click on a "Create Function" block and choose "collapse". That way, the entire content of the function is hidden and saves you a lot of space.

Functions with Arguments

If you think of functions as precomps, then arguments are your master properties. With master properties you can use the same precomp many times, but customize it each time you use it by setting the master properties to a different value. In the same way, a function can have arguments and each time you call the function you can give it a different value for that argument.

You can add, remove and rename arguments by clicking on the blue cog wheel icon of the block which defines the function. Here, we added an argument with the name "my argument" to a function:

Creating a function with an Arguments

For each argument, the function block creates a variable and when the function is executed, the variable will have the value that you gave as an argument to the function call.

Creating a function with an Arguments 2

In this example the function "say hello" is called two times, once with the argument "Victoria" and again with the argument "Sue". So, during the first execution the function gives the message "Hello Victoria!" and during the second execution the message is "Hello Sue!"

Return in Functions

When a function is executed, usually the entire block stack of the function is executed. But with an If Return block you can stop the execution of a function anywhere in the middle of a block stack.

Here is an example:

Block procedures_defreturn

This function shows two messages using an alert block. The first message is always executed. But the second message is only shown if there is an active comp in the After Effects project. We achieve this with the "not has active comp" condition, which quits the execution of the function if there is no active composition.

Functions with Return Values

In the category "Functions" of the block library, you'll also find a block to create functions with an extra return parameter:

Block procedures_defreturn

These functions have corresponding function call blocks with a round shape, which indicates that they actually return a value. The functions work in exactly the same way, except that after all blocks you've plugged in to the function have been executed, the value you specify in the "return" parameter is used as the end result.

Example

Creating a function which returns a value

This "get random label color index" block returns a different random number in the range 1-16 each time it is called. Note that in this case, the main code block of the function definition is empty. But if the calculation of the random number required any preparational steps, you could plug these into the function definition.