Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Forums Adobe After Effects Expressions distance from first vertex expressed in 0-100%?

  • distance from first vertex expressed in 0-100%?

  • Santi Agustí

    March 6, 2020 at 6:16 pm

    I’m trying to make a script that gets the vertex on a path and determines their distance in % from the first vertex, so I can create a trim path per vertex/segment with that percentage number (so it would be like splitting the curve at its vertex and having all the segments separated but on a non destructive way)*
    Do you know how to get the percentage of a vertex along the path from the first vertex?

    like, v[2] is 23%, etc?

    Any idea will be great!

    *having this illustrator script adapted for after effects would be absolutely awesome: cut at selected anchors

  • Alex Printz

    March 6, 2020 at 6:51 pm

    Do you want it to measure curves, or just straight line sections? Curves are going to require more work, but it is possible to approximate the distance (measuring bezier curves is basically splitting the curve into lots of little bits and measuring between those as straight lines).

    Alex Printz
    Mograph Designer

  • Alex Printz

    March 6, 2020 at 9:41 pm

    So, I figured it out, but it cannot be calculated from a single expression, and because of how after effects’ “createPath” function works it requires a minimum number of additional path layers in a sub folder equal to or greater than the number of vertices in the target path.

    In short, it’s a pain, but here’s how you do it:

    make a shape layer with a bunch of paths inside the same isolated folder, and into all of those put this expression:

    L = thisComp.layer("Shape Layer 1").content("master path").path //target path;
    i = thisProperty.propertyGroup(1).propertyIndex; //get index;

    P = L.points();
    T = L.inTangents();
    O = L.outTangents();

    tP = [ P[i-1], P[i] ];
    tT = [ [0,0], T[i] ];
    tO = [ O[i-1], [0,0] ];
    createPath(tP, tT, tO, false); //create temp path
    } catch(e) {createPath([[0,0],[0,0]],[],[],false)}

    Then, after that, place this code into whatever property you want to return the percentage, and make sure you property link the location of the master path, and the location of the sub-paths.

    use r to set the resolution (higher means more accurate, but more calculations), while n will target the index you desire to grab.

    L = content("master path").path //target path;
    T = content("split paths"); //target content folder;

    n = 1; //index of point to get percentage
    r = 10; //resolution

    P = L.points();
    n = clamp(n,0,P.length-1) //limits the target index to number of points

    pA = []; //point array
    pA[i] = T.content(i).path; //pushes the sub-paths into an array

    pL = [0]; //per-point segment length
    pT = 0; //total line length
    pd = [0]; //per-point distance

    for(i=1;i<P.length;i++){ //for each point
    pL[i] = 0; //initial distance 0;
    for(j=1;j<r;j++) pL[i] += length(pA[i].pointOnPath((1/r)*(j-1)),pA[i].pointOnPath((1/r)*j)); //measure length;

    for(i=0;i<pL.length;i++) pT+= pL[i]; //total length
    for(i=1;i<pL.length;i++) pL[i] += pL[i-1]; //adds distances before to current point;
    for(i=1;i<pL.length-1;i++) pd.push(pL[i] / pT); //gets a percentage
    pd.push(1); //last point 100%


    Alex Printz
    Mograph Designer

  • Santi Agustí

    March 6, 2020 at 11:17 pm

    wow! thank you very much for your effort and expanation!
    my idea is creating a basic script with 3 buttons, each one will divide the path on different ways: N parts with random length each (whose sum is the total length), N parts all equal, and N parts, given by the vertex points.
    I was using this snippet from Paul Conigliaro to get the total path length and then starting with the 3 approaches.

    I did first the random sub lengths with the sum of the total length, and I changed the approach when I realized using the Trim Paths Offset parameter was giving me the 360º as a total length and it could point me to something. My scripting knowledge is pretty basic, so I love to explore different approaches which most of the times goes to dead ends, but I also love to get lost on those.
    So, for this part (“the vertex and segment division”s button), I was just starting to doodle how to approach. I was starting to convert the array of vertex from cartesian coordinates to polar and then see if that could lead me to something, but before going further on that direction I wanted to ask because something was telling me it was not going to be the right direction.

    So, I love your approach, even if it relays on the expression side (the script I have in mind is just using the code to set the amount of path duplicates and needed values in the trim paths offset or end, and having a couple lines for the path dependencies with the master path).
    I couldn’t make the second expression part you wrote work (the code going into whatever property I want to return the percentage), it says Index out of range. But for now its ok, because on this case, applying a simple trim paths below all the subpaths splitted with the expression you wrote, does the work.

    Now I’m going to see how to add via script the amount of shape path copies equal to the amount of vertex (considering these as not variable over the timeline) and then appply the expression on each. It’s more effective than the multiple shapes+trim paths with clamped range I was doing since the beginning.

    Thank you again for your time and knowledge!

  • Alex Printz

    March 9, 2020 at 2:17 pm

    Which part of the 2nd script is getting the index out of range? The only thing I can think is the group with the nested paths inside; IIRC that’s the only index measuring. Shape layers can be tricky, there’s the group and then the content nested seperately, so make sure you’re linking it similar to how I have it written.

    Yes this would be easier to do in a script than expressions; you could simply store the selected path data as a variable, cycle through modifying the selected path down to its individual segments and measuring their length, put the final path back in,
    then compare the different path path lengths relative to their positions in the total length and then get a percentage.

    Paul’s awesome, glad to see his stuff is being put to use;. looks like he’s pulled in a more advanced measuring method than I would use (or could ever begin to think of).

    Alex Printz
    Mograph Designer

  • Santi Agustí

    March 9, 2020 at 3:47 pm

    oh, I just reapplied the 2nd script the exact way than the last time and now it works, so thats great! thanks!
    Yes, shape layers are always tricky with the hierarchy!

    As for the script, I’ve done the split at vertex part simply cycling into the n vertices, creating the n number of shape copies and applying each path your expression, and it works great.
    It made me rethink the way I did the option to add the number of splits. Now it works adding a trim to each path split’s subgroup(but the path length doesn’t vary, only the way the trim reveals it) , but I love the way your expression cuts the path. Even considering baking the expression to have real splits.

    I guess I’d have to work on the P = L.points(); line, (on the expresison side and the script side) so it reads only the array of segments the user enters on the script UI

Viewing 1 - 6 of 6 posts

Log in to reply.

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