Trouble with Easing Dynamic Layer Sorting Transitions in After Effects Expression
Hi everyone,
I'm having trouble with an expression, and after trying multiple workarounds, I still can't get it to work. Maybe you have an idea or a hint?
Here's my setup:
- I have several text layers (Item01, Item02, etc.), each with a slider called "Value."
- The slider values change over time on different layers.
- I want to sort the layers dynamically, so the layer with the highest value is always on top. If the values change, the order updates automatically. This part is already working!
The issue is that the position changes instantly, and I want to smooth the transition using ease or linear. But no matter what I try, I can't get it to work properly.
I've experimented with different approaches. This (below) method works for the first few seconds, but later in the timeline, the smooth transition stops working. I cant figure out to get the ease working with the right timing, the problem is (i guess) that the calculation for the ease time is wrong...but i dont know how to fix it...i also tried it like checking when the value changes and then easing and so on....nothing really worked
Any advice or solutions would be greatly appreciated!
Can someone give me a helping hand? 😄
Code:
// Create an array to store the text values and their corresponding layer indices
var textValues = [];
// Loop through all layers to get their text values
for (var i = 1; i <= thisComp.numLayers; i++) {
var layer = thisComp.layer(i);
// Check if the layer name matches the pattern "ItemXX"
if (layer.name.match(/^Item\d+$/)) {
var textValue = parseFloat(layer.effect("Value")(1).value);
if (!isNaN(textValue)) { // Ensure the text value is a number
textValues.push({value: textValue, index: i});
}
}
}
// Sort the array based on text values in descending order
textValues.sort(function(a, b) {
return b.value - a.value;
});
// Find the current layer's index in the sorted array
var currentIndex = textValues.findIndex(function(element) {
return element.index == index;
});
// Calculate the new Y position based on the sorted order
var baseY = 300; // Starting Y position
var spacing = 150; // Space between each text layer
var newY = baseY + currentIndex * spacing;
// Interpolate and ease between the current position and the new position
var currentPosition = value;
var transitionDuration = 1; // Duration of the transition in seconds
// Calculate the eased position
var t = (time - inPoint) / transitionDuration;
t = Math.min(Math.max(t, 0), 1); // Clamp t between 0 and 1
var easedY = ease(t, currentPosition[1], newY);
// Return the new position with easing
[currentPosition[0], easedY];
