After Effects Scripting 101
by Mitch Allen
Note that this article was written using Adobe After Effects Professional 6.5 as a guide.
If you are comfortable with scripting languages, especially JavaScript, you will feel right at home developing scripts for Adobe After Effects (AE). AE uses an internal JavaScript interpreter adapted for use inside of it's environment. You can use JavaScript to do everything from creating new compositions to generating animated effects.
Jump Right In
Fire up an ASCII text editor, like Notepad, and copy the following code into it:
if( ! app.project ) app.newProject();
var myComp = app.project.items.addComp( "Hello Comp1", 320, 240, 1, 10, 30 );
var myText = myComp.layers.addText( "Hello World!" );
myText.position.setValue( [ 50, 50 ] );
Save that code into a file called aehello.jsx. Because this is a special brand of JavaScript, only meant to be run in AE, the standard is to give it a special extension (*.jsx).
If you happen to use a JavaScript editor, see if you can extend it to treat *.jsx files like *.js files. It will help you in your code development.
Run the Script
var myComp = app.project.items.addComp( "Hello Comp1", 320, 240, 1, 10, 30 );
Open Hello Comp1 and you should see the text "Hello World!" This again came from the text in the script above:
var myText = myComp.layers.addText( "Hello World!" );
In the timeline window, expand the Transform options for the text object. Note that the Position values are 50.0, 50.0. You set the position through the script, using this line:
myText.position.setValue( [ 50, 50 ] );
If you run the script again, while the project is open, you will end up with multiple comps of the same name. Feel free to adjust the values of the name, text and position, resaving the file and loading it again.
Scripting Reference
You won't find a good reference to the scripting language in the AE 6.5 online help. You will need to pull the scripting guide from your Adobe After Effects CD. It's in *.pdf format. I'd suggest copying it to your desktop.
The scripting reference in AE 6.5 is extremely vague. But it does list all of the available methods and properties of the AE specific objects. One of the reasons I decided to start writing tutorials on the subject is due to the lack of information. These articles will help me as well as you! I don't have AE 7.0 yet. So I don't know if things have improved.
It's Not a Browser
Remember: AE is not a Web browser. You can't just copy and paste code that works in Internet Explorer and expect it to work in AE. Your coding should focus on manipulating the special objects in the AE environment.
Other Resources
See the side bar for more resources. A great place to start is Dan Ebberts MotionScript.com.
JSX Editors
I use both Eclipse and Macromedia's Dreamweaver for JavaScript development. If all else fails, I use Microsoft Visual Studio. My preferred editor for JavaScript and *.jsx development is Eclipse combined with the Web Tools Platform (http://www.eclipse.org/webtools/). It includes a basic JavaScript editor. It's free and it was easy to extend it to apply color coding to *.jsx files as well. AE 7.0 is supposed to have a built in *.jsx editor. But I haven't upgraded yet.
What's Next
The sample code I posted here was deliberately simple, so as not to confuse the newbie. I did try to make it fool-proof by adding a check in the first line that will automatically open a project if one isn't found. But in the real world, I'd never release code that didn't include exception handling. All future examples will be "industrial strength" and include it. So we need to get that out of the way. I'll cover that next.
Note that this article was written using Adobe After Effects Professional 6.5 as a guide.
If you are comfortable with scripting languages, especially JavaScript, you will feel right at home developing scripts for Adobe After Effects (AE). AE uses an internal JavaScript interpreter adapted for use inside of it's environment. You can use JavaScript to do everything from creating new compositions to generating animated effects.
Jump Right In
Fire up an ASCII text editor, like Notepad, and copy the following code into it:
if( ! app.project ) app.newProject();
var myComp = app.project.items.addComp( "Hello Comp1", 320, 240, 1, 10, 30 );
var myText = myComp.layers.addText( "Hello World!" );
myText.position.setValue( [ 50, 50 ] );
Save that code into a file called aehello.jsx. Because this is a special brand of JavaScript, only meant to be run in AE, the standard is to give it a special extension (*.jsx).
If you happen to use a JavaScript editor, see if you can extend it to treat *.jsx files like *.js files. It will help you in your code development.
Run the Script
- Start Adobe After Effects, if it isn't started.
- Save and Close all projects.
- From the File menu, select Run Script / Choose File ...
- Navigate to where you saved aehello.jsx.
- Select the file and click Open.
var myComp = app.project.items.addComp( "Hello Comp1", 320, 240, 1, 10, 30 );
Open Hello Comp1 and you should see the text "Hello World!" This again came from the text in the script above:
var myText = myComp.layers.addText( "Hello World!" );
In the timeline window, expand the Transform options for the text object. Note that the Position values are 50.0, 50.0. You set the position through the script, using this line:
myText.position.setValue( [ 50, 50 ] );
If you run the script again, while the project is open, you will end up with multiple comps of the same name. Feel free to adjust the values of the name, text and position, resaving the file and loading it again.
Scripting Reference
You won't find a good reference to the scripting language in the AE 6.5 online help. You will need to pull the scripting guide from your Adobe After Effects CD. It's in *.pdf format. I'd suggest copying it to your desktop.
The scripting reference in AE 6.5 is extremely vague. But it does list all of the available methods and properties of the AE specific objects. One of the reasons I decided to start writing tutorials on the subject is due to the lack of information. These articles will help me as well as you! I don't have AE 7.0 yet. So I don't know if things have improved.
It's Not a Browser
Remember: AE is not a Web browser. You can't just copy and paste code that works in Internet Explorer and expect it to work in AE. Your coding should focus on manipulating the special objects in the AE environment.
Other Resources
See the side bar for more resources. A great place to start is Dan Ebberts MotionScript.com.
JSX Editors
I use both Eclipse and Macromedia's Dreamweaver for JavaScript development. If all else fails, I use Microsoft Visual Studio. My preferred editor for JavaScript and *.jsx development is Eclipse combined with the Web Tools Platform (http://www.eclipse.org/webtools/). It includes a basic JavaScript editor. It's free and it was easy to extend it to apply color coding to *.jsx files as well. AE 7.0 is supposed to have a built in *.jsx editor. But I haven't upgraded yet.
What's Next
The sample code I posted here was deliberately simple, so as not to confuse the newbie. I did try to make it fool-proof by adding a check in the first line that will automatically open a project if one isn't found. But in the real world, I'd never release code that didn't include exception handling. All future examples will be "industrial strength" and include it. So we need to get that out of the way. I'll cover that next.

0 Comments:
Post a Comment
<< Home