How to move timeframe keys programatically?

How do I move, let's say, all keys on frame 3 to frame 1 programmatically?

Comments

  • You would prsumably go through the scene getting nodes, then go through their properties finding the ones with keys on their value channel at the desired time, copy the value, clear the key, and assign the value as a key to the new frame.

  • DafaDafa Posts: 97

    Ok, so I did that and when I try out the script I made on a character, hair and clothing no longer smooths.

    Here is my script:

    var oSelectedNode = Scene.getPrimarySelection();var nCurrentFrame = Scene.getFrame();var nCurrentTime = Scene.getTime();var nTimeStep = Scene.getTimeStep();/** * @returns {DzNode[]} */function recursivelyGetElements() {    return oSelectedNode.getNodeChildren(true);}/** *  * @param {DzNode} node  * @returns {DzProperty[]} */function recursivelyGetProperties(node) {    var aProperties = node.getPropertyGroups().getAllProperties();    return aProperties;}function clearAllKeysExceptCurrent(property, node) {    if (nCurrentTime <= 0 ) {        var nStart = nCurrentTime + nTimeStep;        var nEnd = getLastKeyTime(property);        if (nEnd == null || Number.isNaN(nEnd)) return;        var range2 = new DzTimeRange();        range2.start = nStart;        range2.end = nEnd;        node.clearAnimData(range2);    } else {        var nStart = 0;        var nEnd =  nCurrentTime - nTimeStep.valueOf();        var range1 = new DzTimeRange();        range1.start = nStart;        range1.end = nEnd;        node.clearAnimData(range1);        nStart = nCurrentTime + nTimeStep.valueOf();        nEnd = getLastKeyTime(property) + 1;        if (nEnd == null) return;        var range2 = new DzTimeRange();        range2.start = nStart;        range2.end = nEnd;        node.clearAnimData(range2);    }}function moveAllKeysToFirst(properties) {    Scene.setFrame(0);    var ctime = Scene.getTime();        for (var i = 0; i < properties.length; i++) {        var property = properties[i];        if (property.hasKeys() && property.getNumKeys() == 0) {            continue;        }        var value = property.getValue();        property.setValue(ctime, value);        // Delete the older key.                try {            property.deleteKeys(1,1);        } catch (e) {            print("An error occurred:", e);        }    }}function main() {    if (oSelectedNode == null) return;    var aNodes = recursivelyGetElements();    aNodes.push(oSelectedNode);    // Clear properties.    for (var i = 0; i < aNodes.length; i++) {        Scene.setFrame(nCurrentFrame);        var aWorkingProperties = recursivelyGetProperties(aNodes[i]);        // for (var j = 0; j < aWorkingProperties.length; j++) {        //     /**        //      * @type DzProperty        //      */        //     var property = aWorkingProperties[j];        //     if (property.canAnimate() && property.hasKeys()) {        //         clearAllKeysExceptCurrent(property);        //     }        // }        clearAllKeysExceptCurrent(aWorkingProperties[0], aNodes[i]);        moveAllKeysToFirst(aWorkingProperties);    }}main();

     

Sign In or Register to comment.