[SOLVED] Uploading .json animations fail

I have an fbx file that has an animation. I have downloaded the json animation file after PlayCanvas extracted it from the fbx. I edited a number and uploaded the file to my project. What I got was a json file which can’t be used as an animation.

Is there a proper way to upload such file? Animation parser through the codemaybe? Or is it not possible to do any tweaks to the animation json file?

I think you can’t do that right now, with any of the files that get generated from source asset files (models, animations etc).

You will have to edit the original FBX file and reupload it to generate new animations.

1 Like

I went through the hassle and made the code my self. :grin:
I’m sharing it here so… hope it doesn’t break :sweat_smile:

var GJson = pc.createScript('gJson');

/**
 * Converts json to Animation
 * @param {string} j json
 */ 
GJson.prototype.toAnimation=function(j){
    var i,j,k,q,m,n;
    var s=j.animation;
    var a=new pc.Animation();

    a.name=s.name;a.duration=s.duration;
    i=-1;while(++i<s.nodes.length){
        n=new pc.Node();
        m=s.nodes[i];
        n._name=m.name;
        //TODO defaults
        //if (m.)
        var d,dp,dr,ds;//defaults
        dp=this.tov3();
        dr=this.toq3();
        ds=this.tov3([100,100,100]);
        if(m.defaults){
            d=m.defaults;
            if(d.p)dp=this.tov3(d.p);
            if(d.r)dp=this.toq3(d.r);
            if(d.s)dp=this.tov3(d.s);
        }
        j=-1;while(++j<m.keys.length){
            q=m.keys[j];
            k={time:q.time || q.t};


            k.position=(q.position||q.p)?this.tov3(q.position||q.p):dp;
            k.rotation=(q.rotation||q.r)?this.toq3(q.rotation||q.r):dr;
            k.scale=(q.scale||q.s)?this.tov3(q.scale||q.s):ds;
            n._keys.push(k);
        }
        a.addNode(n);
    }
    return a;
};

/*
 * Converts array or XYZ object to pc.Vec3 
 * @param {any} v array or XYZ object
 * @param {number} d default value 
 */ 
GJson.prototype.tov3=function(v,d){
    if(!v)return new pc.Vec3(d,d,d);
    if(Array.isArray(v))
        return new pc.Vec3(v[0],v[1],v[2]);
    return new pc.Vec3(v.x,v.y,v.z);
};

/*
 *Converts array or XYZ object to pc.Quat

 */ 
GJson.prototype.toq3=function(v){
    var q=new pc.Quat();v=this.tov3(v);
    q.setFromEulerAngles(v.x,v.y,v.z);
    return q;
};
//
2 Likes

Thanks for sharing!

:+1:

Actually, I’ve just realized you could just change the resource type at runtime, reload it and the parser converts it to an animation XD

If anyone reading this in the future, all you have to do is:

     pc.app.assets.get(33755555).type="animation";
     pc.app.assets.get(33755555).reload();
     //now that resource can be used as animation
     yourEntity.animation.animations["newAnim"] = yourReloadedAsset;
     yourEntity.animation.play("newAnim");
4 Likes