### AfterEffects - Joining Points in 3D Space with a Line with Expressions

So the final chapter of the Line tutorials, let's look at how to connect our 2D line layer to 3D points, that are either parented or not.
Continuing from here, we're going to make the line work with 3D objects.

Before, we worked with 2D layers and we managed to keep the line connected when it was parented, but when the Nulls were made 3D and the camera was turned, it looked like this:

Which is just terrible! So how do we keep the connection?

## toComp

Okay, time to learn a new expression: toComp.
toComp is a bit hard to explain.

Think of it this way, the position of our points in X and Y haven't changed. Only our camera has changed. (remember that we only saw this problem when we moved the camera. When the camera stayed in its default position, we didn't see it). Basically, the camera is converting the 3D space (World) into a 2D image (Comp).
When we moved the camera, the X Y and Z positions of the Nulls don't change in 3D or world space, they're still at whatever value they are. However, RELATIVE to the camera, they APPEAR to change position (in the Comp). This is the problem. Since the ACTUAL World positions of the objects aren't changing, then the expressions have no change to calculate. In that case we need to tell the expression to consider the position of the camera, and in doing that, we have to convert the 3D positions of the Nulls to positions relative to what the camera is seeing. I hope that makes sense.

DON'T PANIC! The toComp expression is so easy to use, you'll probably wish you hadn't read the above paragraph. There's no complex math to it.
So to get it working, let's go to the first expression in our comp, which is the "Starting Point" of the first beam, linked to the "START" Null.
Select it and clear the expression. Then, apply the following expression:
`thisComp.layer("START").toComp([0,0,0]);`

Yes, it's that easy!
So what's going on here?
This expression tells AE to convert the layer "START"'s position to Composition space, rather than World space. Comp space obviously in this case means relative to the camera in 2D (since the Camera produces a 2D image). The [0,0,0] means that it needs to convert the Null's 3D position relative to the Comp origin! You can adjust this value to apply an offset, though I've never had to do this.

This expression also serves its advantages:
1. We no longer have to worry about whether our object is parented or not. This is because the POSITION of the Null relative to the Comp is what's used. It doesn't matter where in the world it is, only where in the COMP it is. So no complicated math like last time.
2. It's awesome. If you fly around now, you can see that it connects perfectly. you can even go behind and all.

So, this means that we've managed to make a 2D effect look 3D, by simply using the toComp function.
You may also notice that you can make the layers 2D again and parent, and it will still stick!

And that's all there is to it :-D

I hope you enjoyed this trilogy. Feel free to check out the new After Effects series: Workflow tips.

See you in the next one! Like? Hate? Opinion? Comment below will be appreciated!

1.  Easy! Thanks professor.
Sandemanhttp://4after.wordpress.com

2. Always happy to help :) Feel free to send me what you make, would love to see it :D

3. i can't because i've no right to diffuse it. But you can see some of them on my childish blog (where i put a reference to yours!)
http://4after.wordpress.com

4. Awesome! Thanks for sharing :-D Man I wouldn't call it childish, it looks quite nice, and you update regularly so that makes it a great resource! All it's missing is a fancy header and logo!!

5. I've never seen or read such a clear explanation of layers space transformation.
For all of us who think of the "expressions chapter" as if it is an intimidating "beast" , you really are the only one who debunked this myth .I'm new to the expression field and I had difficulties to grasp the deeper meaning of the "toComp" function.I would really love to see more about the layers space transformation( fromComp,toComVec...etc) Thank you so much

6. Hey, thanks for the comment! I'm really glad you enjoyed this post! Thanks!

I think out of all the layer transformations, toComp is the most useful. I haven't found myself having to use the others, but as soon as I do I'll break it down right here.

Btw, you can also check out my free ebook: After Effects Expressions Basics, right here: http://ideastocreations.blogspot.com/p/after-effects-expressions-basics.html

Ιt was so explanatory that it gave me a positive boost to continue with the expression chapter without so much "fear". Once again,you are the only one who has the ability to convey the message so successfully and believe me I keep track of all kind of AE tutorial and I know what I'm saying .Few are the tutors who can impart the knowledge the right way.I quess they forgot how it feels to run into the expressions field for the first time. Thank you David.

8. Thanks, really appreciate it. I'll look into posting more on expressions (especially with my upcoming video tutorials). What, may I ask, would you be interested in seeing here on the site?

9. Expressionssssssssss would be the most interesting thing, taking into account how successfully you demystify them. I also think that there are only few tutorials about AE expressions around the net and most of them are for advanced users.Thank you David for asking.

10. Great! Will look into it and post more. Glad you like it :-D Thanks for reading!

11. Michael Wentworth-BellAugust 20, 2013 at 10:50 AM

What an extremely helpful series of tutorials. Really thankful for putting this together. Really appreciate it and wish you the best!!

12. Thanks, I'm glad you liked it! I'll have more on expressions very soon :D

13. Hi. In case you haven't seen more:
http://ideastocreations.blogspot.com/2013/09/ae-expression-driven-gauge-part-1.html#more

14. YES YES YES!!!! I needed this!! thank youuuuuu :D

15. AWESOME! Thanks for taking the time to comment :D

16. I have seen the expression linking a 2D flare to a light so that it moves in 3D space along with the light. It also uses 'toComp' as part of the expression. This explanation is an eye-opener ... never seen it explained better. However I would like to apply it to a sightly different situation as follows : I have many words on different layers distributed randomly in 3D space; I have applied the "beam" plugin to a solid layer & would like to have it's start point attach to the (x,y,z) anchor point of text layer 1 and it's end point attach to (x,y,z) anchor point of the text layer 2; then repeat the same process from layer 2 to layer 3, layer 3 to layer 4 & so on. I have used SureTarget from videocopilot.net to animate the camera between the words already. The end goal is to show a line connecting one word to another as the camera moves between the words. I am thinking it has to be a modification of this expression, but just can put into realization. Any idea how to convert the beams start-end points (x,y) to link with text layers anchor point (x,y,z)? Thanks.