//一键预合成(Ae自有预合成功能复刻) v1.0.1 2021-06-09
//Raymond Yan 2021 (RaymondClr@outlook.com / QQ: 1107677019)

var win = new Window(
    "palette { margins: 10, spacing: 5, alignChildren: 'left', \
    A: RadioButton { text: '保留合成中的所有属性', value: false }, \
    B: RadioButton { text: '将所有属性移动到新合成', value: true }, \
    C: Group { margins: [17, 0, 0, 0], \
        A: Checkbox { text: '将合成持续时间调整为...', helpTip: '将合成持续时间调整为所选图层的时间范围', value: true } \
    }, \
    D: Group { margins: [1, 0, 0, 0], \
        A: Checkbox { text: '打开新合成', value: false } \
    } \
    E: EditText { size: [200, 25], text: '预合成' }, \
    F: Button { size: [200, 25], text: '一键预合成' } \
}"
);
win.show();

var rdb_keepAttributes = win.A;
var rdb_moveAttributes = win.B;
var ckb_matchDuration = win.C.A;
var ckb_openPreComp = win.D.A;
var edt_preComp = win.E;
var preCompose = win.F;

rdb_keepAttributes.onClick = function () {
    ckb_matchDuration.enabled = !rdb_keepAttributes.value;
};

rdb_keepAttributes.addEventListener('mouseover', keepAttributesButtonsOnMoveHandle());

rdb_keepAttributes.addEventListener('mouseout', keepAttributesButtonsOnMoveHandle());

rdb_moveAttributes.onClick = function () {
    ckb_matchDuration.enabled = true;
};

function keepAttributesButtonsOnMoveHandle() {
    return function () {
        var activeItem = getActiveItem();
        if (!activeItem) return;
        if (activeItem.selectedLayers.length === 1) {
            rdb_keepAttributes.enabled = true;
            return;
        }
        var buttonState = (rdb_keepAttributes.value = rdb_keepAttributes.enabled = false);
        rdb_moveAttributes.value = !buttonState;
        ckb_matchDuration.enabled = !buttonState;
    };
}

preCompose.onClick = function () {
    var activeItem = getActiveItem();
    if (!activeItem) return;
    if (!edt_preComp.text === '') return;
    var rawActiveItem = activeItem;
    var selectedLayers = activeItem.selectedLayers;
    var indiciesArr = getLayerIndiciesArr(selectedLayers);
    var timeAttributesArr = getSelectedLayersTimeAttributes(selectedLayers);
    var selectedLayersTimeRange = getSelectedLayersTimeRange(timeAttributesArr);
    var preCompLayerIndex = getPreCompLayerIndex(selectedLayers);
    var moveAttributes = selectedLayers.length === 1 ? rdb_moveAttributes.value : true;

    app.beginUndoGroup('Pre-compose');

    var preComp = activeItem.layers.precompose(indiciesArr, edt_preComp.text, moveAttributes);

    if (moveAttributes && ckb_matchDuration.value) {
        setPreCompDuration(preComp, selectedLayersTimeRange);
        setPreCompLayerStartTime(
            rawActiveItem.layer(preCompLayerIndex),
            selectedLayersTimeRange.minInPoint
        );
        setPreCompChildLayersStartTime(preComp, selectedLayersTimeRange.minInPoint);
    }
    app.endUndoGroup();

    if (ckb_openPreComp.value) preComp.openInViewer();
    return preComp;
};

function getActiveItem() {
    var activeItem = app.project.activeItem;
    if (activeItem instanceof CompItem) {
        return activeItem;
    }
    return false;
}

function getLayerIndiciesArr(selectedLayers) {
    var indiciesArr = [];
    for (var i = 0, l = selectedLayers.length; i < l; i++) {
        indiciesArr[i] = selectedLayers[i].index;
    }
    return indiciesArr;
}

function setPreCompChildLayersStartTime(preCompItem, newStartTime) {
    $.writeln(preCompItem.layer(1).name);
    for (var i = 1, l = preCompItem.numLayers; i <= l; i++) {
        preCompItem.layer(i).startTime -= newStartTime;
    }
}

function setPreCompDuration(preCompItem, TimeRangeObj) {
    var inPointAndOutPointDifference = TimeRangeObj.maxOutPoint - TimeRangeObj.minInPoint;
    preCompItem.duration = inPointAndOutPointDifference;
}

function getPreCompLayerIndex(selectedLayers) {
    var selectedLayersIndexArr = [];
    for (var i = 0, l = selectedLayers.length; i < l; i++) {
        selectedLayersIndexArr.push(selectedLayers[i].index);
    }
    return Math.min.apply(null, selectedLayersIndexArr);
}

function setPreCompLayerStartTime(preCompLayer, newStartTime) {
    preCompLayer.startTime = newStartTime;
}

function getSelectedLayersTimeAttributes(selectedLayers) {
    var timeAttributes = {};
    timeAttributes.startTimeArr = [];
    timeAttributes.inPointArr = [];
    timeAttributes.outPointArr = [];
    for (var i = 0, l = selectedLayers.length; i < l; i++) {
        var currentSelectedLayer = selectedLayers[i];
        timeAttributes.startTimeArr.push(currentSelectedLayer.startTime);
        timeAttributes.inPointArr.push(currentSelectedLayer.inPoint);
        timeAttributes.outPointArr.push(currentSelectedLayer.outPoint);
    }
    return timeAttributes;
}

function getSelectedLayersTimeRange(selectedLayersTimeAttributesArr) {
    return {
        minStartTime: Math.min.apply(null, selectedLayersTimeAttributesArr.startTimeArr),
        minInPoint: Math.min.apply(null, selectedLayersTimeAttributesArr.inPointArr),
        maxOutPoint: Math.max.apply(null, selectedLayersTimeAttributesArr.outPointArr),
    };
}

Leave a Reply

Your email address will not be published.