ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

# For Loop crashes After Effects - Is it me?

FAQ   •   VIEW ALL
 For Loop crashes After Effects - Is it me? on Dec 8, 2018 at 11:09:27 pm

I am trying to write an expression that will measure the length of a stroke. It's to be used in another expression later on.

Since the stroke has bezier curves, I am breaking it into many smaller segments (triangles) and just measuring the delta X and delta Y so I can determine the incline between the two sides of the triangle. It's not perfect, but close enough for what I want to accomplish.

I tested the various elements of the code. I am able to grab the position coordinates of two points on the path and calculate the incline correctly. However, when I put the calculation into a For Loop, so it can move through the stroke piece by piece, it causes After Effects to crash.

Is something wrong with my code or is it just too much for AE to handle?

Hope you can help.

```sPath = thisComp.layer("StrokePath").content("Shape 1").content("Path 1").path; sLayer = thisComp.layer("ButterflyPath"); strokeLength = 0; iStart = 0; iEnd = 0.10; for (i=0;i&lt;1;i+0.10) { p1 = sLayer.toComp(sPath.pointOnPath(iStart+i)); p2 = sLayer.toComp(sPath.pointOnPath(iEnd+i)); dX = p2[0] - p1[0]; dY = p2[1] - p1[1]; c = Math.sqrt(Math.pow(dX,2)+Math.pow(dY,2)); strokeLength += c; } strokeLength;```

 Re: For Loop crashes After Effects - Is it me?on Dec 9, 2018 at 12:04:02 am

It might just be that your for loop needs to increment i like this:

for (i=0;i<1;i+=0.10) {
etc.

This is how I've done it in the past (it seems to work pretty well):

``` path = thisComp.layer("StrokePath").content("Shape 1").content("Path 1").path; len = 0; steps = 100; p0 = path.pointOnPath(0); for (i = 1; i <= steps; i++ ){ p1 = path.pointOnPath(i/steps); len += length(p0,p1); p0 = p1; } len ```

Dan

 Re: For Loop crashes After Effects - Is it me?on Dec 9, 2018 at 12:53:13 am

Thanks Dan!

I didn't know about the length function.

 Re: For Loop crashes After Effects - Is it me?on Dec 9, 2018 at 3:38:44 am

Hi Dan,

Sorry, one more question. Do you know if it is possible to measure the stroke length from the start of the stroke until a specific point?

In my code, I am pulling a specific point (vertex?) on the stroke path (segmentEnd) and trying to break out of the for loop when the position of p1 matches that of s1.

However, I am now getting a crazy high length returned (71 thousand), whereas the length of the entire stroke is only about 1500 or so. I would think that if my if statements are incorrect, it would just keep looping until it hit the end of the stroke or give an error. Even stranger, if I increase the amount of steps, the length returned increases as well.

I verified that the nextPoint and segmentEnd variables are correct. No errors are given.

```sLayer = thisComp.layer("StrokePath"); sPath = sLayer.content("Shape 1").content("Path 1").path; steps=100; segmentLength = 0; p0 = sLayer.toComp(sPath.pointOnPath(0)); nextPoint = (thisComp.layer("Global Variables").effect("NextKey")("Slider"))-1; segmentEnd = sLayer.toComp(sPath.points()[nextPoint]); for (i=0; i&lt;=steps; i++) { p1=sLayer.toComp(sPath.pointOnPath(i/steps)); segmentLength += length(p0,p1); if (p1[0].toFixed(0)==segmentEnd[0].toFixed(0)) { if(p1[1].toFixed(0)==segmentEnd[1].toFixed(0)) { break; } } } segmentLength.toFixed(0);```

 Re: For Loop crashes After Effects - Is it me?on Dec 9, 2018 at 3:43:16 am

Sorry, posted too soon. I forgot the p1=p0 and now it is looping through the entire stroke.

So I guess my question is just : is there a way to measure from the start of the stroke to a specified point? My if statements are doing nothing.

```path = thisComp.layer("ButterflyPath").content("Shape 1").content("Path 1").path; len = 0; steps = 100; p0 = path.pointOnPath(0); for (i = 1; i &lt;= steps; i++ ){ p1 = path.pointOnPath(i/steps); len += length(p0,p1); p0 = p1; } len.toFixed(2); ```

 Re: For Loop crashes After Effects - Is it me?on Dec 9, 2018 at 3:58:13 am

Off hand, I think the best you could do would be to come within a certain tolerance. It's unlike that one of your steps along the path would ever land precisely on a particular vertex.

Dan