[SOLVED] How To Extract Keyframes form Animation Asset

Is there any way to Extract keyframe data from animation assets using a script or other method ?

I got it

var AnimTotextureConverter = pc.createScript('animTotextureConverter');

AnimTotextureConverter.attributes.add('animations', {
    type: 'asset',
    assetType: 'animation',
    array: true
});

AnimTotextureConverter.attributes.add('character', {
    type: 'entity'
});

AnimTotextureConverter.attributes.add('animationsEntity', {
    type: 'entity'
});

// initialize code called once per entity
AnimTotextureConverter.prototype.initialize = function () {
    this.convert();
    console.log(this.animations.map((a) => a.resource));
};

AnimTotextureConverter.prototype.convert = function () {
    var animTextures = {};

    for (var a = 0; a < this.animations.length; a++) {
        var animation = this.animations[a];
        var resource = animation.resource

        animTextures[animation.name] = this.convertToTexture(resource);
    }

    console.log(animTextures);

};

AnimTotextureConverter.prototype.convertToTexture = function (animTrack) {
    var keyFrames = this.convertToFrames(animTrack);

    return keyFrames;
};

AnimTotextureConverter.prototype.convertToFrames = function (animTrack) {
    var keyFrames = [];

    for (var c = 0; c < animTrack.curves.length; c++) {
        var curve = animTrack.curves[c];
        var targetFrames = this.getTargetFrames(animTrack.curves[c], animTrack.inputs, animTrack.outputs);

        keyFrames.push(targetFrames);
    }

    return keyFrames;
};

AnimTotextureConverter.prototype.getTargetFrames = function (curve, inputs, outputs) {
    if (curve && inputs && outputs) {
        var data = {
            target: curve.paths[0].entityPath,
            property: curve.paths[0].propertyPath,
            frames: []
        }

        var input = inputs[curve.input];
        var output = outputs[curve.output];

        for (var t = 0; t < input.data.length; t++) {
            var value = [];

            for (var c = 0; c < output.components; c++) {
                value.push(output.data[c * t]);
            }

            data.frames.push({ time: input.data[t], value });
        }

        return data;

    }
}

// update code called every frame
AnimTotextureConverter.prototype.update = function (dt) {

};
2 Likes