-
expression to move from one layers position to another…
I’m trying to cook up an an expression that moves a null from the position of a start layer (index ‘x’), then move to the next layers position (index ‘x+1’) and pause. The null then moves to the next layer (index ‘x+2’) and pause, and so on until the final layer is reached.
(I’m using shape layers with a rectangle)
The code needs to define the first layer and the last layers index numbers, moving between every layer in-between the two values.
The time it takes to move from one layer to the next is dictated by how far the next layer is from the previous layer. So the further a layer is from its previous layer, the longer it takes to to get to the new layer, so the move is a constant speed regardless of the distance between layers.
(A variable on an slider needs to control the overall speed, acting as a multiplier of the speed).
How long the null pauses between moves depends on the Y size of the shape layer that is driving the current position of the null. So the the bigger the target layers Y size, the longer it pauses.
(A variable on an effect slider needs to control the overall pause time, acting as a multiplier of the pause).
I’ve been trying to use Chat GTP to help me write this and it turns out it knows less than me!
Current code looks like this…
// Define the start and end layer indices
startLayerIndex = 3;
endLayerIndex = 7;
// Define the speed multiplier
speedMultiplier = effect("Speed Multiplier")("Slider");
// Define the pause multiplier
pauseMultiplier = effect("Pause Multiplier")("Slider");
// Define the initial position of the null object
initialPosition = thisComp.layer(startLayerIndex).transform.position;
// Define the initial time
initialTime = 0;
// Loop through each layer and move the null object
for (i = startLayerIndex; i <= endLayerIndex; i++) {
// Define the current layer
currentLayer = thisComp.layer(i);
// Define the target position
targetPosition = currentLayer.transform.position;
// Convert the target position to the composition's coordinate system
targetPosition = thisComp.layer(currentLayer.index).toComp(targetPosition);
// Calculate the distance to the target position
distance = length(targetPosition - initialPosition);
// Calculate the time it should take to cover the distance at a constant speed
moveTime = distance / speedMultiplier;
// Define the pause time based on the Y size of the current layer
pauseTime = currentLayer.sourceRectAtTime().height * pauseMultiplier;
// Move the null object to the target position over the calculated time, then pause for the calculated pause time
linear(time - initialTime, 0, moveTime, initialPosition, targetPosition) + linear(time - (initialTime + moveTime), 0, pauseTime, targetPosition, targetPosition);
// Update the initial position and time for the next iteration
initialPosition = targetPosition;
initialTime += moveTime + pauseTime;
}
anyone got any ideas? I’m wondering if a ‘while’ loop will work better