Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Cogs with Bar attached always oriented in one axis

  • Cogs with Bar attached always oriented in one axis

    Posted by Mike Moon on December 3, 2014 at 11:25 pm

    Hey guys, been a while since I’ve posted here. What I’m trying to do is have a bar that is attached to a gear cog and the right always goes into the building which means no matter how the cog spins with the bar attached, the bar is always oriented to one axis or null? Does that make sense? Here is a picture I have. The bar is attached to the gear on the left. i want it to go into the hole on the right while it turns. The gear is spinning counter clockwise. I don’t think auto-orient does this particular task. Thanks!

    Mike Moon replied 11 years, 5 months ago 2 Members · 5 Replies
  • 5 Replies
  • David Conklin

    December 3, 2014 at 11:32 pm

    Assuming you have 2 nulls at either end of your bar.. the left being called “Gear” and the right being called “Hole.” This also assumes that the anchor point of your bar is at 0 on the X-axis (i.e. all the way at the left). The position of your bar should be linked to the position of the “Gear” null.

    Put this on the rotation prop:

    (or, check out this file):
    8261_gearbarhole.aep.zip

    //expression for rotation prop.

    //get layers
    var gear = thisComp.layer("Gear");
    var hole = thisComp.layer("Hole");

    //get 2 sides of triangle
    var dist = hole.transform.position - gear.transform.position;

    //do math
    radiansToDegrees(Math.atan(dist[1]/dist[0]));

    David Conklin
    Motion Designer

  • Mike Moon

    December 3, 2014 at 11:43 pm

    David thanks for your rapid response. I tried this and as soon as I put your expression into the Z rot of my bar, it immediately rotated to the Hole null however as the gear turns with the bar attached, it no longer auto orients to the hole.

  • Mike Moon

    December 3, 2014 at 11:57 pm

    oh ok I get it. When I move the “Gear” null it does work. I guess the expression is linked to the position of the Gear Null? Right now, I have the “Gear Null” parented to the rotation of the Gear itself which is spinning on the z axis with “time*-60” Is there a way to make your expression work if the Gear Null is parented to something or do I have to animate the position by hand? Thanks.

  • David Conklin

    December 4, 2014 at 12:19 am

    This solution is a bit more complicated.

    You essentially need to get the ‘global’ position of your gear null, not the position it’s inherited from it’s parent. You can use an expression like this to link your bar’s position to the global position of the Gear null.

    p = thisComp.layer("Gear");
    p.toComp(thisComp.layer("Gear").transform.anchorPoint)

    Once you’ve established that link, you just need to do the same thing to your rotation expression:

    var gear = thisComp.layer("Gear");
    var hole = thisComp.layer("Hole");

    var dist = hole.transform.position - gear.toWorld(gear.transform.anchorPoint);

    radiansToDegrees(Math.atan(dist[1]/dist[0]));

    Have a look at this file to help clear things up.
    8262_gearbarholeworldtransform.aep.zip

    Good luck!

    David Conklin
    Motion Designer

  • Mike Moon

    December 4, 2014 at 4:58 pm

    Your example proj cleared things up completely! I just ran into one problem, the example didn’t work for 3D layers. It kind of shifted everything and I tried moving the anchor points but it only messed it up more. So I just pre-comp’d it and then made that comp a 3D layer. So that worked out great. Thanks so much for figuring this out for me! I’m really amazed by scripting. How did you learn to do expressions? I tried some tutorials but I’m just lost.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy