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
  • 1432 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

the idea is to apply the code on several layers to read the color information of an underlying layer to calculate the distance to that point. This information will then be used to drive other expressions. 

So to code needs to be slim enough to run on several several layers at the same time. 

 

Perhaps there is a better way to find the point at where this underlying layer is white? 


I think I'd be tempted to track the white points instead.  That will get you usable data, you can run the tracker for each white point and AE will be more manageable.