Skip to main content
Participant
January 19, 2024
Question

Expression not controlling scale properly

  • January 19, 2024
  • 2 replies
  • 179 views

Okay so I'm trying to get a 3d layer to change its scale based on its orbit around a point in 3d space, and the expression isn't working properly. I can add the expression to a text layer and output the values to the source text and see the values changing but sometimes the values never change for the scale.

Here's the expression I'm working with:

// Reference to the null layer's position
var nullLayerPosition = thisComp.layer("Controls for Churchs").transform.position;
var nullPosZ = nullLayerPosition[2];

// Current position of the layer
var currentPosition = transform.position.valueAtTime(time - 0.01);
var currentZ = currentPosition[2];

// Manually calculate scale factor
var maxScale = 100; // Maximum scale when in front
var minScale = 0;   // Minimum scale when behind the orbit point
var scaleRange = maxScale - minScale;
var compHalfWidth = thisComp.width / 2;

// Adjust scale based on z position
var scaleFactor;
if (currentZ > nullPosZ) {
    // Layer is on the far side of the orbit
    scaleFactor = minScale;
} else {
    // Calculate scale based on distance to the edge of the orbit in y space
    var distanceToEdge = Math.abs(currentZ - nullPosZ);
    var scaleProgress = distanceToEdge / compHalfWidth;
    scaleFactor = maxScale - scaleProgress * scaleRange;
}

[scaleFactor, scaleFactor, scaleFactor];

I've also attached a slimmed down version of the project file as well with the expression(s) that I was using, I also had an expression applied to the position property.

This topic has been closed for replies.

2 replies

Community Expert
January 20, 2024

If the expression is fixed by removing the valueAtTime calculation, in simple terms, it measures the difference in Z position between two layers, a null and a layer rotating around the null. When the Z value is equal, the layer is at 100% scale, and when the layer is at its farthest distance behind the null, the scale is approximately 65% with the current settings.  

 

If that is your intent, It is kind of working, but it makes the layer look like it is farther away from the camera when it is actually closer to it. You are getting kind of a reverse perspective effect.

 

Please explain exactly what you are trying to accomplish, and maybe we can help you simplify the expression and make it accurate.

 

Mylenium
Legend
January 19, 2024

Evaluation order. Inside the code a simple valueAtTime() won't do because the value doesn't really exist persistently. It only works when debugging due to how the properties are forced to fetch their results. You will need to put this in a loop to get the same effect.

 

Mylenium