Sketchfab Forum

Example of correct use of API Animation api.setCurrentAnimationByUID


(Steebus) #1

The following statement is incorrect, read the reply response below... BT

This is an example of how we used api.setCurrentAnimationByUID

The use of animation and the array wasn't entirely clear to me that I had to place api.setCurrentAnimationByUID inside api.getAnimations initially so I thought it may help to post how we used it: I would recommend to Sketchfab for this to be clarified on the API page, it's the only thing that wasn't immediately working or clear to us:)

var success = function ( api ) {

api.addEventListener( 'viewerready', function() {
$( '#Button1' ).click( function () {

     api.getAnimations( function ( err, animations ) {
                            console.log( animations );
			var firstAnimationUID = animations[1][0];
			api.setCurrentAnimationByUID( firstAnimationUID );


// hope this helps:)

(Shaderbytes) #2

This info is incorrect, you dont have to place setCurrentAnimationByUID within getAnimations function. You can call it from where ever you want just so long the viewer is ready. Also since the animations never change it is redundant to make the call to get animations every time you click your button.

So when the viewer is ready you should call get Animations just once and cache those results to a variable.

Then when wanting to call setCurrentAnimationByUID you should reference your cached results directly for the relevant UID.

(Steebus) #4

Ah I see, very useful. I'll adjust it on our end, we couldn't get it to work at first, that's possibly how we got to the incorrect conclusion when we did get it to work inside the getAnimations function.
Also placing these results in a variable is a great idea.
I am making the assumption from the above that you need to run the getAnimations function at least once before you can setCurrentAnimationByUID, or could you remove getAnimations when you have the UIDs?


(Shaderbytes) #5

well yes you have to run it once ,to get the UID's you need to use :slight_smile: Once you have this data there is no need to call getAnimations again.

(Steebus) #6


I have been comparing it (in my mind) to getCameraLookAt where you don't need getCameraLookAt, you can just set the position and target in setCameraLookAt as long as you know the arrays,
e.g. I remove or comment out getCameraLookAt, once I know the array.

But if I understand it correct you can never run setCurrentAnimationByUID without getAnimations at least once when the viewer is ready. i.e. you can't just provide an array (you may have gotten earlier and removed the function) or UID without running the function first during the same instance setCurrentAnimationByUID is active in.

(Shaderbytes) #7

for anything using UID's , animations or node objects you should always get them fresh per session because they are not guaranteed to be the same across sessions. In my utility i like to abstract the concept of needing to use UID's and rather want to use names of objects and animations. so for example create an object to store animations by name and the create a play function that accepts a name and then does the lookup for the UID :

var animationClips = {}; 


function generateAnimationControls  (err, animations) {
        if (err) {
            console.log('Error when calling getAnimations');
        for (var i = 0; i < animations.length; i++) {

            var ob = animationClips[animations[i][1]] = {};
   = animations[i][1]
            ob.uid = animations[i][0];
            ob.length = animations[i][2];

function getAnimationObject  (key) {
        var dataObjectRef = animationClips[key];
        if (dataObjectRef == null) {
            console.error('a call to  getAnimationObject using key/name ' + key + ' has failed , no such object found');
            return null;
        return dataObjectRef;

function PlayAnimationClip  (key) {
       var dataObjectRef = getAnimationObject(key);
       if (dataObjectRef != null) {          

function OnSetAnimationClip  (err) {