Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions script mask shape to motion path

  • script mask shape to motion path

    Posted by James Primhak on June 30, 2013 at 3:49 pm

    Hello,

    I am trying to write a script which involves doing the ‘copy mask path to the position of a layer, creating a motion path’ thing, but I am a bit stumped as to how you would automate this process, and haven’t found anything in the scripting guide or on the web that helps. Has anyone done this before? as it would save me a lot of time and tedium if you had already worked out an answer. If not, I’ll have a go and post my results (if any!)

    Cheers,
    James

    Benoit Kergosien replied 12 years, 1 month ago 3 Members · 3 Replies
  • 3 Replies
  • Xavier Gomez

    June 30, 2013 at 7:34 pm

    Hello,
    assuming that your script has successfully identified a source [path property] and a target [2D point property] (that actually carry keyframes, not their parents with customizable names etc), here are the rough steps. Actually the hardest part is not to read/write the source/target but to correctly handle already existing keyframes on the target (for instance if you apply the script for the second time on the same target) especially if you want to set the keyframes roving. The sketch below doesnt handle that.

    *** slt means strictly less than ***

    to read the path content, do:
    var shape = source.value,
    verts = shape.vertices,
    inTan = shape.inTangents,
    outTan = shape.outTangents,
    numVerts = verts.length;

    or use valueAtTime if you want to read the path at a specific time;
    vertices is an array of 2D points, in and out tangents arrays of 2D vectors (which in pratice is the same).
    If the path is closed and you want to have a motion that takes it into account, do:
    if (shape.closed) {verts.push(verts[0]); inTan.push(inTan[0]); outTan.push(outTan[0]); numVerts++;};
    Then you also must inject 2D tangents into 3D because tangents for a motion path are always 3D (even for 2D):
    for (var k = 0; k slt numVerts; k++) inTan[k][2] = outTan[k][2] = 0;
    Finally you have to specify a collection of numVerts times to write the keys. This is a matter of choice. You can choose a 2 seconds time interval from the current comp time (like AE does), or what ever you want.
    Once you have chosen these times, assuming they are arranged into an array:

    target.setValuesAtTimes(times, verts);
    for (var idx, k=0; k slt numVerts; k++)
    {
    idx = target.nearestKeyIndex(times[k]);
    target.setSpatialTangentsAtKey(idx, inTan[k], outTan[k]);
    }

    To set roving:
    for (var idx, k=0; k slt numVerts-1; k++)
    {
    idx = target.nearestKeyIndex(times[k]);
    target.setRovingAtKey(idx, true);
    }

    Xavier.

  • James Primhak

    June 30, 2013 at 10:31 pm

    Wow, what an answer!

    Cheers man thats amazing

  • Benoit Kergosien

    April 5, 2014 at 5:08 pm

    Hello,

    I’m trying to achieve the same thing than James except that I’m a complete beginer in Scripting!

    Could you tell me all the code’s steps to realise such a script? And if it’s not too much to ask, please could you comment it, I would gladly appreciate to understand what I would write.

    Benoit.

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