ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Clamping x position on after effects

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>

Gabriel Rocha
Clamping x position on after effects
on Oct 8, 2012 at 8:42:13 pm

hello,
Is there a way to clamp a X Position opn after effects on several different values?
I've been using this code:

x1=1024.3322
x2=1224.1572
clamp(transform.xPosition,x1,x2)
if (transform.xPosition>x1) {
transform.xPosition=x2;
}

But that only allows me to snap from one position to another. I'd like to be able to snap into 6 positions (on X dimension).
is there a way to do that?
I use the Snaped position to get the position value and use in another layer but i really need to be clamped.
Thanks,

GR


Return to posts index


Dan Ebberts
Re: Clamping x position on after effects
on Oct 8, 2012 at 8:56:41 pm

Something like this should work:

xVals = [1024,1224,1444,1624,1824,2024];
i = 0;
while (value > xVals[i] && i < (xVals.length-1)) i++;
xVals[i]


Dan



Return to posts index

Gabriel Rocha
Re: Clamping x position on after effects
on Oct 9, 2012 at 12:54:37 pm

Hey Dan,
I knew you would come for the Help.
Right now for some reason it's not working.
im not sure if i made myself 100% clear.
Check the file i uploaded as an example of what i have now. Just slide the layer to On and OFF.
Download the AEP here:
http://www.sendspace.com/file/gjj1pi

But instead of ON and OFF i would have Value 1, Value, 2 Value 3, Value 4...
Im adding your code on the xPosition but i get the Layer stuck in one single position.
Thanks again for your help!

http://www.gabrielrocha.com


Return to posts index


Gabriel Rocha
Re: Clamping x position on after effects
on Oct 9, 2012 at 12:57:45 pm

ok forget about it.
The code you sent wasn't working on my original Project, but its working on a plain project (like the one i uploaded). SO it must be something on my previous project.
I'll check it out! Thanks man. You rock!

http://www.gabrielrocha.com


Return to posts index

Gabriel Rocha
Re: Clamping x position on after effects
on Oct 10, 2012 at 8:23:19 pm

Hey Dan,
What if i want to slide a layer on the xPosition, but having a limitation.
Let`s say i want to slide it from 1024 to 1530, not clamping just being able to slide from 1024 to 1530, limiting the beginning and the end to those values?
I`ve found similar possibilities, but none quite the same here on CC.
THanks again in advance,

GR

http://www.gabrielrocha.com


Return to posts index

Dan Ebberts
Re: Clamping x position on after effects
on Oct 10, 2012 at 8:36:56 pm

Are you talking about something different than just right-clicking on the slider value in the Effect Controls window and setting the slider range?

Dan



Return to posts index


Gabriel Rocha
Re: Clamping x position on after effects
on Oct 10, 2012 at 8:42:33 pm

Yes, i want to do that on the Viewport.
I know about the slider, checkbox and stuff on the Effects tab, but i want to do that on the viewport. you know what i mean?

http://www.gabrielrocha.com


Return to posts index


Dan Ebberts
Re: Clamping x position on after effects
on Oct 10, 2012 at 8:57:27 pm

> you know what i mean?

Sorry, I'm not sure. By "Viewport" do you mean the Comp window?

If you're talking about an expression that would keep you from dragging a layer outside that range, that be a clamping expression for X Position like this:

clamp(value,1024,1530)

If not, I'm not sure what you're trying to do.

Dan



Return to posts index

Gabriel Rocha
Re: Clamping x position on after effects
on Oct 10, 2012 at 9:01:24 pm

Actually that's perfect.
I didn't know Clampping would give me that result.
I thought that clamping would be more like snapping.
THanks again man, that helped!

http://www.gabrielrocha.com


Return to posts index

Meng Zhang
Re: Clamping x position on after effects
on Apr 10, 2017 at 5:10:11 pm

That's awesome. Does anyone know how can I limit the position within a polygon, like a mask or shape layer? I have been searching for this, but no luck. Thanks guys!


Return to posts index

Dan Ebberts
Re: Clamping x position on after effects
on Apr 10, 2017 at 8:17:27 pm

I don't think there's a practical way to do that. You could hack something together with sampleImage() on a shape layer, but it would be quite a bit of work to get it set up.

Dan



Return to posts index

Meng Zhang
Re: Clamping x position on after effects
on Apr 10, 2017 at 9:12:30 pm

Dan, thank you for your quick response. I agree with you, although I didn't find a perfect solution, I managed to find out a way to detect if the position is within a mask using this attached code by M. Katz based on Nirg's approach:

By changing the polygon to to a layer mask, I successfully got a right result. And it is all I need for now. I just need to detect the position key frames outside the mask and delete them. I'm planning looping through the key frames and delete those that are out of range.

I hope to take the chance to thank you for all your awesome answers in this forum. I have always been learning a lot from you. Thank you.

function pointIsInPoly(p, polygon) {
var isInside = false;
var minX = polygon[0].x, maxX = polygon[0].x;
var minY = polygon[0].y, maxY = polygon[0].y;
for (var n = 1; n &lt; polygon.length; n++) {
var q = polygon[n];
minX = Math.min(q.x, minX);
maxX = Math.max(q.x, maxX);
minY = Math.min(q.y, minY);
maxY = Math.max(q.y, maxY);
}

if (p.x &lt; minX || p.x > maxX || p.y &lt; minY || p.y > maxY) {
return false;
}

var i = 0, j = polygon.length - 1;
for (i, j; i &lt; polygon.length; j = i++) {
if ( (polygon[i].y > p.y) != (polygon[j].y > p.y) &&
p.x &lt; (polygon[j].x - polygon[i].x) * (p.y - polygon[i].y) / (polygon[j].y - polygon[i].y) + polygon[i].x ) {
isInside = !isInside;
}
}

return isInside;
}


Return to posts index

Dan Ebberts
Re: Clamping x position on after effects
on Apr 10, 2017 at 10:54:31 pm

So this is for a script? That should work because scripting has access to mask/path vertices.

Dan



Return to posts index

Meng Zhang
Re: Clamping x position on after effects
on Apr 11, 2017 at 12:34:14 am

Hey Dan,

It is a script. My fault not making this clear. I didn't see it is under expression discussion. Thank you again for help!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]