I've been trying to create a script for offsetting keyframes in AE.
The problem is that I cannot make the keyframes to offset in ascending order. Visually each property that is above another property in the layer should be offset 2 keyframes to the right from the property below (http://i.imgur.com/argwtAd.png). I tried sorting the properties by propertyIndex, but the indexes of the properties don't correspond to the ascending order, i.e. property with index 2 does not mean that it visually is situated above/below property with index 1 in the timeline. This is what happens:

Is there any way I can sort the properties in real ascending order, so that they always look like this:

Here is my code:
Code: Select all
var arr,ctr;
function offsetKeys()
{
var curItem = app.project.activeItem;
var selectedLayers = curItem['selectedLayers'];
var ctro = 0;
ctr = -1;
arr = [];
var allLayers = [];
for(var g=0;g<selectedLayers.length;g++){
allLayers.push({idx:selectedLayers[g].index,lr:selectedLayers[g]});
}
allLayers.sort(function(a,b){return b.idx - a.idx}); //Sorting layers in ascending order
for(var h=0;h<allLayers.length;h++){
var lr = allLayers[h].lr;
var props = lr.selectedProperties;
describeOffsetKeys(lr,props);
}
var prop;
for(var i=0;i<arr.length;i++){
for(var k=0;k<arr[i].keys.length;k++)
{
var idx = arr[i].keys[k];
prop = idx.ma_pr;
lay = idx.ma_lr;
ctro = i*curItem.frameDuration*2; //offset keys with two frames
var keyToCopy = idx.ma_key;
prop.removeKey(keyToCopy);
var newTime = idx.ma_time + ctro;
var keyToCopyValue = idx.ma_value;
var newKeyIndex = prop.addKey(newTime);
prop.setSelectedAtKey(keyToCopy, false);
prop.setValueAtKey(newKeyIndex, keyToCopyValue);
}
}
}
function describeOffsetKeys(lr,props){
var tempA = [];
for(var i=props.length-1; i>=0 ; i--){
var prop = props[i];
if (prop.propertyType == PropertyType.INDEXED_GROUP || prop.propertyType == PropertyType.NAMED_GROUP) describeOffsetKeys(lr,prop);
else if (prop instanceof Property) {
var selectedKeys = prop.selectedKeys;
if (prop.canVaryOverTime && selectedKeys.length > 0) {
ctr++;
var inA = [];
for(var j=0;j<selectedKeys.length;j++){
var k = selectedKeys[j];
var obj = {} ;
obj.ma_time = prop.keyTime(k);
obj.ma_value = prop.keyValue(k);
obj.ma_key = k;
obj.ma_pr = prop;
obj.ma_lr = lr;
inA.push(obj);
}
var prgr = prop;
while(prgr.propertyDepth > 2) prgr = prgr.parentProperty; //getting the actual property group property of the current layer
inA.sort(function(a,b){return b.ma_time - a.ma_time});
tempA.push({ler:lr,keys:inA,pridx:prgr.propertyIndex});
}
}
}
if(tempA.length > 0)
{
tempA.sort(function(a,b){return b.pridx - a.pridx}); //Here I try to sort the properties in ascending order (bottom to top) but the indexes of the properties differ from the actual order in the layer
for(var p = 0;p<tempA.length;p++){
arr.push({ler:tempA[p].ler,keys:tempA[p].keys,pridx:tempA[p].pridx});
}
}
}
Thx!