【AE脚本】图层按选择顺序重新排序(其他图层位置不变)

//头号咸鱼
app.beginUndoGroup("SortLayers");
var thisComp = app.project.activeItem;
var selLayers = thisComp.selectedLayers;
var LN = selLayers.length;
var In = [];
for (var i=0; i < LN; i++) 
{
    In[i] = selLayers[i].index;
}
In = In.sort(function(a,b){return a-b});  //选择图层顺序列表

// 随便复制一层置底,防止选择了最后一个图层
var last_layer = selLayers[0].duplicate();
last_layer.moveToEnd();

// 遍历 并复制一层(占位图层)、把选择的图层移动到最后
for (var i = 0; i < LN; i++) 
{
    selLayers[i].duplicate();
    selLayers[i].moveAfter(last_layer);
}

// 遍历 把底部选择的图层 放置到顺序index列表后,再把占位图层删掉
for (var i = 0; i < LN; i++) 
{
selLayers[i].moveAfter(thisComp.layer(In[i]));
thisComp.layer(In[i]).remove();
}
last_layer.remove();
app.endUndoGroup();

思路

  • 先获取选择图层列表,再获取顺序排列的index列表 Array.sort()
  • 随便复制一层置底,防止选择了最后一个图层
  • 遍历选择图层,复制该图层(占个位置),并移动到最后
  • 再把选择的图层移动到顺序数组的图层后
  • 然后把占位图层删掉

反向排序的话,中间的In.sort();后面加一句 In.reverse();

优化1:可以把复制图层改成新建一个空白图层?(by月离)

优化2:直接倒序插入也可(by熊猫)

其他思路2(by熊猫)

  • 把选中的图层index存进一个数组
  • 从小到大排序该数组  Array.sort()
  • 遍历选中的图层,判断当前图层的index与数组index是否对应
  • 一致则不变,不一致则与目标index的图层

其他(Moelody)

参考即可,我懒得polyfill了
Array.prototype.forEach =
    Array.prototype.forEach ||
    function(callback, context) {
        if (Object.prototype.toString.call(this) === '[object Array]') {
            var i, len
            for (i = 0, len = this.length; i < len; i++) {
                if (typeof callback === 'function' && Object.prototype.hasOwnProperty.call(this, i)) {
                    if (callback.call(context, this[i], i, this) === false) {
                        break
                    }
                }
            }
        }
    };

 

 

给TA充电
共{{data.count}}人
人已充电
AE开发脚本开发

【AE脚本】基于文字图层内容排序

2021-9-3 23:29:00

AE开发脚本开发

【AE脚本】基于图层位置 从左到右排序图层

2021-9-4 0:43:16

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
搜索