Participant
June 2, 2022
Question
Autofit / Autoscale expression to comp
- June 2, 2022
- 2 replies
- 946 views
I've created these expressions:
// Autofit & Autoscale expression
//Anchor property expression:
//center anchorpoint
s=sourceRectAtTime();
[s.left+s.width/2,s.top+s.height/2];
//Position property expression:
//!!! Manually set position x and y to 0 so position = [0,0,0] !!!
//original compsize
org_x = 1920;
org_y = 1080;
//create precentage correction:
corrx = (thisComp.width / org_x) * 100;
corry = (thisComp.height / org_y) * 100;
var cw = thisComp.width/2;
var ch = thisComp.height/2;
lw = thisLayer.sourceRectAtTime(time).width;
lh = thisLayer.sourceRectAtTime(time).height;
//apply position but with a correction based on original comp size...
mx = (transform.position[0]) * (corrx/100);
my = (transform.position[1]) * (corry/100);
//center position
x = thisComp.width/2;
y = thisComp.height/2;
[x + mx,y + my];
//Scale property expression:
//Two options scale to fit or fit to comp:
scaletofit = true;
var lw = thisLayer.width;
var lh = thisLayer.height;
var cw = thisComp.width;
var ch = thisComp.height;
//which is bigger x or y?
x = ch/lh*100;
y = cw/lw*100;
function testaspect(x,y,scaletofit){
check = (lw/100) * x;
if (scaletofit){
if (check < cw) x = y;
else y = x;
} else {
if (check > cw) x = y;
else y = x;
}
return [x,y];
}
if (cw < ch){
// Comp widht is smaller Comp height;
out = testaspect(x,y,scaletofit);
} else if (cw > ch) {
// Comp widht is bigger Comp height;
out = testaspect(x,y,scaletofit);
} else {
// Comp widht is Comp height;
out = testaspect(x,y,scaletofit);
}
//allow custom scale
mw = value[0];
mh = value[1];
[out[0] * (mw/100),out[1] * (mh/100)]
Next what I'm trying to accomplish is have:
1. A Point Controller Effect applied to the layer.
2. Add a Transform Effect to the layer or add this expression to the transform property but with the Transform Effect it becomes more modular...
3. Add an expression to the position of that Transform Effect that takes the Point Controller Effect into account...
var lw = thisLayer.width;
var lh = thisLayer.height;
var cw = thisComp.width;
var ch = thisComp.height;
scalex = cw/lw;
scaley = ch/lh;
//x = (cw/2) - thisLayer.sourceRectAtTime(time).width/2 * scalex;
//y = (ch/2) - (thisLayer.sourceRectAtTime(time).height/2 * scaley);
centerx = thisLayer.sourceRectAtTime(time).width/2 * scalex;
centery = thisLayer.sourceRectAtTime(time).height/2 * scaley;
//This is the point effect with a correction:
//Tried to use toComp() or fromComp() or toWorld() or fromWorld() but still need to figure out what the differences are...
cx = effect("sweetspotcorrection")("Point")[0] * scalex;
cy = effect("sweetspotcorrection")("Point")[1] * scaley;
cx = centerx / cx * centerx/2
cy = centery / cy * centery/2
x = ch/lh*100;
y = cw/lw*100;
function testaspect(x,y){
check = (lw/100) * x;
if (check < cw) true;
else false;
}
cx = cx * 1
cy = cy * 1
//These things seem to fix some of the issues in some occasions...
//if (lh > ch) cy = cy / 4
//if (lw > cw && cw < ch) cx = cx * 4
if (cw < ch){
// Comp widht is smaller Comp height;
out = testaspect(x,y);
if (out) [0,cy]
else [cx,0]
} else if (cw > ch) {
// Comp widht is bigger Comp height;
out = testaspect(x,y);
if (out) [0,cy]
else [cx,cy]
} else {
// Comp widht is Comp height;
out = testaspect(x,y);
if (out) [0,cy]
else [cx,0]
}
I think I'm close but if someone like @Dan Ebberts might have a look?
