/* name:缩放所有合成
* source:https://github.com/ff6347/after-effects-script-snippets/blob/master/scale%20all%20comps%20in%20project%20panel.jsx
*
*/
(function(thisObj) {
run(thisObj);
// 初始化 定义缩放倍率
function run(thisObj) {
var data = {
scale_factor: 0.25,
scirptname: File($.fileName)
};
doScale(data);
}
// var curComp = app.project.activeItem;
// if (!curComp || !(curComp instanceof CompItem)){
// alert("noComp");
// return;
// };
//
function doScale(data) {
// 设置撤销组
app.beginUndoGroup('do sale');
for(var i = 0; i < app.project.selection.length; i++) {
var activeItem = app.project.selection[i];// = app.project.activeItem;
if ((activeItem === null) || !(activeItem instanceof CompItem)) {
alert('请选择/打开一个图层先.', data.scriptName);
} else {
// 验证输入字段,以防用户没有先对其进行散焦(时常发生)。
// this.parent.parent.optsRow.text_input.notify("onChange");
var activeComp = activeItem;
// app.beginUndoGroup(data.scriptName);
// 创建一个空的3D 空对象
var null3DLayer = activeItem.layers.addNull();
null3DLayer.threeDLayer = true;
// 位置归零 (0,0,0).
null3DLayer.position.setValue([0, 0, 0]);
// 将空对象设置为 所有没有父层图层 的父级层。
makeParentLayerOfAllUnparented(activeComp, null3DLayer);
// 设置新合成的宽高.
activeComp.width = Math.floor(activeComp.width * data.scale_factor);
activeComp.height = Math.floor(activeComp.height * data.scale_factor);
// 对于相机,按比例缩放Zoom参数。
scaleAllCameraZooms(activeComp, data.scale_factor);
// 按初始化参数设置空对象的缩放。
var superParentScale = null3DLayer.scale.value;
superParentScale[0] *= data.scale_factor;
superParentScale[1] *= data.scale_factor;
superParentScale[2] *= data.scale_factor;
null3DLayer.scale.setValue(superParentScale);
// (with dejumping enabled)开启冒泡的情况下 删除空对象 .
// 冒泡:
null3DLayer.remove();
// app.endUndoGroup();
// Reset data.scale_factor to 1.0 for next use.
// ~ data.scale_factor = 1.0;
// ~ if (this.parent.parent.optsRow.scaleButton.value) {
// ~ this.parent.parent.optsRow.text_input.text = "1.0";
// ~ }
}
}
app.endUndoGroup();
}
//
// Scales the zoom factor of every camera by the given scale_factor.
// Handles both single values and multiple keyframe values.
function scaleAllCameraZooms(theComp, scaleBy) {
for (var i = 1; i <= theComp.numLayers; i++) {
var curLayer = theComp.layer(i);
if (curLayer.matchName === 'ADBE Camera Layer') {
var curZoom = curLayer.zoom;
if (curZoom.numKeys === 0) {
curZoom.setValue(curZoom.value * scaleBy);
} else {
for (var j = 1; j <= curZoom.numKeys; j++) {
curZoom.setValueAtKey(j, curZoom.keyValue(j) * scaleBy);
}
}
}
}
}
// Sets newParent as the parent of all layers in theComp that don't have parents.
// This includes 2D/3D lights, camera, av, text, etc.
//
function makeParentLayerOfAllUnparented(theComp, newParent) {
for (var i = 1; i <= theComp.numLayers; i++) {
var curLayer = theComp.layer(i);
if (curLayer !== newParent && curLayer.parent === null) {
curLayer.parent = newParent;
}
}
}
}(this));