Skip to main content
mart-martinlofqvist
Known Participant
April 7, 2023
Question

Get the x position for underlaying layer is white

  • April 7, 2023
  • 2 replies
  • 1427 views

Hey! 

 

Trying to figure out how to do this but I'm not as good with code as I want... 

 

The concept is:

• I have a layer from which I want to sample to color information using sampleImage

• On a each layer I want a for loop stepping through the x position one pixel at the tame until the sampled color is white

• When the desired color is found I want the for loop to stop executing. 

• Next step is I want to measure the distance from the left side of the comp until this new x position of the color change.

 

I figured this concept is not that complicated but my for loops keeps crashing. 

 

Anyone have an idea how to set this up? 

 

 

 

 

This topic has been closed for replies.

2 replies

mart-martinlofqvist
Known Participant
April 9, 2023

Update: 

I have something that sort of works, I used the updated code from ShiveringCactus and modified it. 

 

 

 

found = false; 
for (x = 500; x < thisComp.width-100; x += thisComp.layer("sample precomp 2").effect("resolution")("Slider")) {
sample = thisComp.layer("sample precomp 2").sampleImage([x, transform.yPosition], [1,1]);
if (sample[0] == 1) {
		found = true;
		break;
	}
}
[linear(x, 0, 1080, 0, 238), value[1]];

 

 

I have a slider with a "resolution" value letting me choose how many steps it should take at each frame. I also modified it to start at x = 500 and end -100 from the comp width. It sort of works at several layers but AE crashes from time to time. 

 

Maybe someone has further suggestions on how to improve this approach...

 

It's applied to the scale, that's why I'm using a two dimensional value as the result. 

 

Also @Rick Gerard since I'm only looking for white right now it works fine to just use one of tha channels from sampleImage. 

Mylenium
Legend
April 7, 2023

If you already have code, you need to include it in your post. If it crashes your loops probably don't terminate and you run out of memory. Otherwise it shouldn't be too difficult to construct something with two simple nested for() loops to comb through the lines and then through the X positions and you simply add break and continue rules to the loops.

 

Mylenium

mart-martinlofqvist
Known Participant
April 7, 2023

It only needs to do one line, meaning the current y position of the layer. Not going down. 

 

 

 

 

x = 0;
found = false;
while (!found && x <= thisComp.width) {
    sampleColor = thisComp.layer("sample").sampleImage([x, transform.yPosition], [1, 1]);
    if (sampleColor[0] == 1) {
        found = true;
    } else {
        x += 1;
    }
}
if (found) {
    x;
} else {
    value;
}

 

 

 

This is the code I'm trying with but it's a mess. Not a coder.  

ShiveringCactus
Community Expert
Community Expert
April 7, 2023

I've just tried your code and I think the problem is that (asuming HD) you're asking it to perform up to 1920 sampleImage calculations.  And due to the way After Effects works, it then performs all the same again on the next frame as expressions have no "frame memory".  

What's the purpose for this expression?  Maybe there's a better way to get the end result.

 

This is a slightly smaller version of your expression, and AE struggled when it was enabled but not previewing, but on preview, it seems to work fine:

x = 0;
found = false;
for (var x = 0; x < thisComp.width; x++) {
    var sampleColor = thisComp.layer("sample").sampleImage([x, 540], [1, 1]);
    if (sampleColor[0] == 1) {
        found = true;
		break;
    }
}
x;

 

Just disable the expression when you're working on the other elements of the comp.