Line Connectors

Learn how to create a line between 2 layers

REMINDER
If you are an absolute beginner, please start your learning by checking the essential resources below:
http://www.jjgifford.com/expressions/basics/index.html
https://helpx.adobe.com/after-effects/using/expression-basics.html
http://www.motionscript.com

1. Setup

  • show the grid and the rulers, and activate snapping (from the “view” menu in the menu bar).
  • draw a line shape from the center of the composition to +100px to the right; for example, if you work in 1920×1080, draw a line from [960,540] to [1060,540]
  • rename shape layer to “line”
  • Create 2 shape layers, conveniently renamed “from” and “to”, or import the assets that you want to link with a line, and make sure both layers anchor points are centered.
  • Reposition one layer to the right, and the other to the left side of the composition.

2. Expressions

  • Add an expression to shape layer(“line”)> shape 1> Position, to link the line start point to the Layer(“From”)
toComp / fromComp
convert point space to composition space
Shape Layers transform controls are relative to the composition center,
[0,0] in the shape layer space is the center of the comp [960,540].
fromComp(thisComp.layer(“from”).transform.position)
  • Add an expression to shape layer(“line”)> shape 1> Scale, to evaluate the distance between the 2 objects
length(point1,point2)
evaluates the distance between 2 points
var from = thisComp.layer(“from”).transform.position;
var to = thisComp.layer(“to”).transform.position;
[length(from,to),value[0]]
  • Add an expression to shape layer(“line”)> shape 1> Rotation, to orient the line to point to the destination object

Geometry: what is an arc tangent ?


Math.atan2(x,y) evaluates the counterclockwise angle in radians (not degrees)
between the positive X axis and the point (x, y).
radiansToDegrees() / degreesToRadians() convert angle measuring units

var from = thisComp.layer(“from”).transform.position;
var to = thisComp.layer(“to”).transform.position;
radiansToDegrees( Math.atan2( from[1]-to[1], (from[0]-to[0]) ) )-180

Because Math.atan2() evaluates counterclockwise, substract to the result 180º

  • pull out “Stroke 1” object out of “Shape 1” group and put it down in the stack, so that the stroke is rendered after transformation. You can also delete “Fill 1” object.

3. Fake Elasticity

  • Add a zigzag object under the group, change Ridges per Segment to “1” and Points to “Smooth”
  • Add an expression to shape layer(“line”)> ZigZag 1> Size, to determine the maximum size of the line and distort the line depending on the distance between the 2 layers
var from = thisComp.layer(“from”).transform.position;
var to = thisComp.layer(“to”).transform.position;
var maxd = 960; //manually adjust the maximum length
(maxd-length(from,to))/4

ZigZag works like a sinusoid, distorting in both Y and -Y

  • Add an expression to shape layer(“line”)> shape 1> Anchor Point, to shift the line position depending on the Zigzag distortion intensity
[value[0],content(“Zig Zag 1”).size]

4. Alternative routes

The oldest method around is to use Effect> Generate> Beam and link Starting / Ending Points to both layers positions. This methods can be good enough if you need only a few lines and don’t need to stylize them. But if you have more than 5 lines then render time can increase dramatically.

Motion Boutique released a while ago Connect Layers, a free script originally made as a debug tool for Plexus, that works wonders to create rendered line connectors, elastic ropes and triangulations. But you will have to re-render keyframes each time you modify the animations.

Lastly, it is worth mentioning Lines creator, a quite expensive script ($25) that automatize the method explained in this post.

DON’T FORGET
build your own presets collection to speed up your workflow
Click icon below to download presets (rename .key to .zip after download) download

7 thoughts on “Line Connectors

    1. Hello, thank you for commenting; it should work if you copied the FFX file in your AE presets folder (after renaming downloaded file to .zip and unzipping it, because wordpress won’t allow to host .FFX files).

      Like

    1. Dan Ebberts is a highly active member of the after effects pro users community, you can ask him anything about expressions and scripts on the creativecow, and his solution will blow your mind away

      Like

Leave a comment