Difference between revisions of "AfterEffects"

From cgwiki
Line 28: Line 28:
 
* https://fendrafx.com/tutorial/after-effects-extendscript-training-complete-series/
 
* https://fendrafx.com/tutorial/after-effects-extendscript-training-complete-series/
  
=== Quick things: ===
+
== Quick things: ==
  
Extendscript Editor:
+
=== Extendscript Editor ===
 
* F5 - run the script ( like ctrl-enter in a wrangle)
 
* F5 - run the script ( like ctrl-enter in a wrangle)
 
* ctrl-shift-c - clear the console
 
* ctrl-shift-c - clear the console
 
* $.writeln('foo'); - print something to the internal console in the extendscript editor
 
* $.writeln('foo'); - print something to the internal console in the extendscript editor
  
VSCode:
+
=== VSCode ===
 
* F1, 'Adobe After Effects' - run the script, the command prompt will store the last run, so its usually just F1, enter
 
* F1, 'Adobe After Effects' - run the script, the command prompt will store the last run, so its usually just F1, enter
 
* writeLn('foo'); - print something to the info panel within AE itself (make sure the panel is expanded first)
 
* writeLn('foo'); - print something to the info panel within AE itself (make sure the panel is expanded first)
 
* alert('foo'); - bring up an alert box
 
* alert('foo'); - bring up an alert box
  
 +
=== Misc ===
 
Find comp by name: https://gist.github.com/dokluch/1ca33a65dc67d4a3047f
 
Find comp by name: https://gist.github.com/dokluch/1ca33a65dc67d4a3047f
  
Line 60: Line 61:
 
Because of localization! "Shape" as a property name will change based on the language zone you're in, but "ADBE Vector Shape" won't. It's irritatingly long, but that's the reason. I've found sometimes you can cheat and the shorter names work, but then others won't. It's less hassle in the long run to just use the proper names.
 
Because of localization! "Shape" as a property name will change based on the language zone you're in, but "ADBE Vector Shape" won't. It's irritatingly long, but that's the reason. I've found sometimes you can cheat and the shorter names work, but then others won't. It's less hassle in the long run to just use the proper names.
  
=== Shapes ===
+
== Shapes ==
  
 
[https://youtu.be/ZQgyVcSNF7Y?t=347 Fiddly. How? Fiddly.]
 
[https://youtu.be/ZQgyVcSNF7Y?t=347 Fiddly. How? Fiddly.]
  
==== Get shape point positions ====
+
=== Get shape point positions ===
 
Get vertices from a shape layer path:
 
Get vertices from a shape layer path:
  
Line 72: Line 73:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Update an existing shape path ====
+
=== Update an existing shape path ===
  
 
<syntaxhighlight lang="javascript">
 
<syntaxhighlight lang="javascript">
Line 85: Line 86:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Create a new layer and new shape ====
+
=== Create a new layer and new shape ===
  
 
Paths created via scripting aren't visible by default, so you have to add stroke and width properties too:
 
Paths created via scripting aren't visible by default, so you have to add stroke and width properties too:
Line 107: Line 108:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== Set Text Layers ===
+
== Set Text Layers ==
  
 
Less fiddly than shapes. Which is nice. I also included how to get the comp, which is a modified version of a script linked further up, without all that pesky type checking and correct coding. :)
 
Less fiddly than shapes. Which is nice. I also included how to get the comp, which is a modified version of a script linked further up, without all that pesky type checking and correct coding. :)
Line 125: Line 126:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== paths on disk ===
+
== Hex to RGBA ==
 +
 
 +
Infuriatingly several functions that are exposed to the expression editor aren't available in jsx scripting, hextorgb is one of them. Here's a standin function, it assumes for regular web style #FF22BB style rgb input, and returns an AE 4 value vector array with alpha set to 1.
 +
 
 +
<syntaxhighlight lang="javascript">
 +
function hex_to_rgba(hex){
 +
var rgb = parseInt(hex, 16);
 +
var red  = (rgb >>16) & 0xFF;
 +
var green = (rgb >>8) & 0xFF;
 +
var blue  = rgb & 0xFF;
 +
 +
var colorArray = [red/255.0, green/255.0, blue/255.0, 1.0];
 +
 +
return colorArray;
 +
}
 +
</syntaxhighlight>
 +
 
 +
 
 +
== Paths on disk ==
  
 
Uses forward slashes, windows drive locations are gitbash style so /c/foo/bar:
 
Uses forward slashes, windows drive locations are gitbash style so /c/foo/bar:
Line 133: Line 152:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== includes ===
+
== Includes ==
  
 
Same pathing as above, can set includepath and include:
 
Same pathing as above, can set includepath and include:
Line 142: Line 161:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
=== toComp and fromComp in scripting ===
+
== toComp and fromComp in scripting ==
  
 
You can't, its an expression thing only, arrgh. Luckily someone has written their own module to do space transformation, LST:
 
You can't, its an expression thing only, arrgh. Luckily someone has written their own module to do space transformation, LST:

Revision as of 17:42, 18 July 2022

Open multiple After Effects sessions

Hit the windows key and R at the same time, type

afterfx -m

Scripting

Several tutorial sites mention a scripting interface, but I couldn't find it. Turns out its hidden, this page gave clues to where it is:

https://www.codeandmotion.com/blog/after-effects-absolute-beginners

Go to creative cloud, prefs, turn on 'show older apps'. Restart creative cloud, you'll find Extendscript Toolkit CC. Amusingly you can't launch it from the creative cloud launcher, but it should be available from your Windows start menu.

That same web page says to set After Effects as the target, this is done from the first dropdown in the top left, where it says 'ExtendScript Toolkit CC', change that to 'Adobe After Effects 2022'.

There's also a way to link directly to VSCode, but last time I tried it refused to work. Will try again one day, using this link as a guide: https://medium.com/adobetech/extendscript-debugger-for-visual-studio-code-public-release-a2ff6161fa01

The older Extendscript editor works, but its, well, old. The newer VSCode stuff is much better. I had trouble getting it going the first time, but watching this video clarified. Basically, install 2 VSCode extensions:

  • Extendscript Debugger
  • Adobe Script Runner

With those in place, you can create a jsx file, at any time hit F1, find 'Adobe After Effects', the script will be saved and run within AE.

David Torno helpfully shared links to his pretty extensive free training series too, cool stuff:

Quick things:

Extendscript Editor

  • F5 - run the script ( like ctrl-enter in a wrangle)
  • ctrl-shift-c - clear the console
  • $.writeln('foo'); - print something to the internal console in the extendscript editor

VSCode

  • F1, 'Adobe After Effects' - run the script, the command prompt will store the last run, so its usually just F1, enter
  • writeLn('foo'); - print something to the info panel within AE itself (make sure the panel is expanded first)
  • alert('foo'); - bring up an alert box

Misc

Find comp by name: https://gist.github.com/dokluch/1ca33a65dc67d4a3047f

Good overview of how to get scripting going in 2022, especially with vscode instead of the extendscript editor: https://www.youtube.com/watch?v=DTBtfFiyjNU

Handy guide which is sorta kept more up to date than adobe's internal docs which for some reason are riddled with dead links: https://ae-scripting.docsforadobe.dev/

Why property names are so complex

Why does scripting use

property("ADBE Vector Shape")

and not

property("shape")

as shown in the AE layer property names?

Because of localization! "Shape" as a property name will change based on the language zone you're in, but "ADBE Vector Shape" won't. It's irritatingly long, but that's the reason. I've found sometimes you can cheat and the shorter names work, but then others won't. It's less hassle in the long run to just use the proper names.

Shapes

Fiddly. How? Fiddly.

Get shape point positions

Get vertices from a shape layer path:

var myLayer = myComp.layer("Shape Layer 1");
points = myLayer.property("Contents").property("Path 2").property("ADBE Vector Shape").value.vertices;

Update an existing shape path

// make a shape
var myShape = new Shape();
myShape.vertices = [[0,0], [0,100], [100,100], [100,0]];
myShape.closed = true;

// find the existing shape in the comp, set the new shape
theshape = myLayer.property("Contents").property("Path 2").property("ADBE Vector Shape");
theshape.setValue(myShape );

Create a new layer and new shape

Paths created via scripting aren't visible by default, so you have to add stroke and width properties too:

// make a layer
var layer = mainComp.layers.addShape();
layer.name = 'my cool shape';

// make a shape
var newshape = new Shape();
newshape.closed = false;    
newshape.vertices = [[0,0], [0,100], [100,100], [100,0]];;

// add a path, set its path values from the shape, set its colour and width
var pathGroup = layer.content.addProperty("ADBE Vector Shape - Group");
pathGroup.property("ADBE Vector Shape").setValue(newshape);
var stroke = layer.content.addProperty("ADBE Vector Graphic - Stroke");
stroke.property("ADBE Vector Stroke Color").setValue([1,0,0,1]);  // rgba, so this is solid red
stroke.property("ADBE Vector Stroke Width").setValue(15);

Set Text Layers

Less fiddly than shapes. Which is nice. I also included how to get the comp, which is a modified version of a script linked further up, without all that pesky type checking and correct coding. :)

findItemByName=function(_name){
	for(var i=1;i<=app.project.numItems;i++)
	{
		var curItem=app.project.item(i);
		if (curItem.name==_name ) return curItem;
	}
	return null;
}

var myComp = findItemByName ("MAIN_COMP");
myComp.layer("text1").property("Text").property("Source Text").setValue("my amazing new text");

Hex to RGBA

Infuriatingly several functions that are exposed to the expression editor aren't available in jsx scripting, hextorgb is one of them. Here's a standin function, it assumes for regular web style #FF22BB style rgb input, and returns an AE 4 value vector array with alpha set to 1.

function hex_to_rgba(hex){
	var rgb = parseInt(hex, 16); 
	var red   = (rgb >>16) & 0xFF; 
	var green = (rgb >>8) & 0xFF; 
	var blue  = rgb & 0xFF;
	
	var colorArray = [red/255.0, green/255.0, blue/255.0, 1.0];
	
	return colorArray;
}


Paths on disk

Uses forward slashes, windows drive locations are gitbash style so /c/foo/bar:

var csvFile = File("/d/projects/myproj/data.csv");

Includes

Same pathing as above, can set includepath and include:

#includepath "/d/ae/libs/LST"
#include 'LST.js'

toComp and fromComp in scripting

You can't, its an expression thing only, arrgh. Luckily someone has written their own module to do space transformation, LST:

https://github.com/rendertom/LST

Nexrender

Handy command line interface to after effects that can control rendering, ffmpeg output, but most importantly allow for you to control AE, so its easy(ish) to swap layers, text, properties, you name it.

https://github.com/inlife/nexrender

The docs above are pretty good, some extra tips cos I keep losing them in that big list:

  • layer searches are wildcarded across all comps by default, so if 'herovideo' is a layer in 6 comps, it will be replaced in all 6.
  • external jsx scripts can be called directly as part of the assets block in json:
   {
        "src": "file:///y:/worktemp/show_it_to_me_update_shapes_v03.jsx",
        "type": "script"
    }
  • text is replaced via a layer and property call, this is also wildcarded across all comps by default:
 {
        "type": "data",
        "layerName": "video_title",
        "property": "Source Text",
        "value": "this is the new text"
    },


Expressions

Annoyingly while it seems its full javascript, its not. It's more like hscript/python expressions in that you can read from anywhere, but ony write to yourself (ie whatever the property/parameter it is that you're writing the expression in).

Debugging is also frustrating, as there's no console. You can print stuff kindasorta by forcing a fake expression with throw, eg

throw myvar;

You'll get an error, when you click in the expression field, the error (ie the thing you want) will be shown in a tooltip. This can't be copypasted though, so if you need to access these values, you need to create a text layer, and have the text expression fetch the thing you want. Frustrating.

Set shape via expression and csv

Similar but different to the jsx version:

let pts = []

data = footage("data.csv")
ptslength = thisComp.layer("data")("Data")("Number of Rows")
for (let i = 0; i < ptslength; i++) {
	pts[i] = [data.dataValue([0,i])*200-800,data.dataValue([2,i])*.1-2500];
}
createPath(pts, [], [], false);