Basically it finds keyframes, sets new ones at the offset time and deletes the old ones. Sounds pretty easy but obviously there's something wrong with the script.
Can you see what the problem is?
There seems to be an infinite loop where for (p = 1; p <= pGroup.numProperties; p++) keeps getting an infinite number of "Time Remap" and "Motion Trackers". I don't know why.
Code: Select all
/**********
* Offset Keyframes
**********
* Filename: Rs - Offset Keyframes.jsx
* Written by: Redsandro - http://www.rednet.nl/en/
* Date: 2010/01/11
* Last updated: 2010/01/11
**********
*
* Description:
*
* Offset all keyframes of a layer for fixing different-length replacement
*
**********/
clearOutput();
var myComp = app.project.activeItem;
var errors = new Array();
if (myComp == null || !(myComp instanceof CompItem)) {
alert("No composition selected.");
}
else if (myComp.selectedLayers.length == 0) {
alert("Select one or more layers to offset keyframes of.");
}
else {
// Vars
var selLays = myComp.selectedLayers
var fDuration = myComp.frameDuration;
var propsCount = 0;
var offsetCount = 25;
offsetCount = parseInt(prompt("Offset keys how many frames? (New footage less handle (1 minute), keys go backwards (-1500).)", offsetCount));
var offset = offsetCount * fDuration;
if (offset == 0) exit;
app.beginUndoGroup("Rs - Offset Keyframes");
// Walk layers
for (var l = 0; l < selLays.length; l++) { // Layers Zero based!
// Vars
var lay = selLays[l];
alert(lay.numProperties);
// Walk props
propOffset(lay, offset)
}
app.endUndoGroup();
}
function propOffset(pGroup, offset) {
for (p = 1; p <= pGroup.numProperties; p++) { // Properties NON-Zerobased
var prop = pGroup.property(p);
if (prop instanceof PropertyGroup)
propOffset(prop, offset);
else if (prop instanceof Property) {
if (prop.canVaryOverTime && prop.numKeys > 0) {
// walk keyframes
for (k = 1; k <= prop.numKeys; k++) { // NON-Zerobased
// Walk forewards or backwards
if (offset > 0) // Start with last key if keys are moving forward
k = prop.numKeys - k + 1;
//var key = prop(k);
// clone key
var time = prop.keyTime(k);
prop.setValueAtTime(time+offset, prop.valueAtTime(time, false));
prop.removeKey(k);
}
}
}
else if (p.name = "Marker") {
//SELFNOTE: Something with markers.. I hardly use them.
// Scripting guide p. ~99.
// PseudoCode:
// foreach something:
// p.setValueAtTime(OldTime+offset, OldMarkerValue);
// p.removeMarker(oldTime);
}
}
}