Jump to content

Play Animation Button [SOLVED]


djeustice
 Share

Recommended Posts

I received a model of a tractor / crane arm.  It is boned and animated.  I exported this crane arm out of 3DS Max as a glTF.  When I import it into BabylonJS, the animation begins playing immediately.  I would like import the crane arm and not have the animation play immediately.  I have a "Play" button and I'd like the animation to start from frame 0 when clicked.  I'd like the animation to stop and return back to frame 0 when the button is clicked again.   Is creating a Play / Stop animation button fairly basic (loader.beginAnimation())?  Do I have to work with skeletons?  Below is my code:

    var loader = BABYLON.SceneLoader.ImportMesh("", "models/", "CraneArm.gltf", scene, function (newMeshes)  {  
    });     
 
    document.getElementById("AddOars").addEventListener("click",function () {   
    }); 

CraneArm.png.46e933f14f3926505f61b472e305f420.png

Link to comment
Share on other sites

The animation button works now!  This is the code I ended up with:

    var isAnimating = false;
    var meshesForAnimation = null;    
    BABYLON.SceneLoader.ImportMesh("", "test/", "Crane.gltf", scene, function (newMeshes)  { 
        for (i = 0; i < newMeshes.length; i++) {
            scene.stopAnimation(newMeshes);
            isAnimating = false;
        }
        meshesForAnimation = newMeshes;
    });

    document.getElementById("Button").addEventListener("click",function () {
        if(!meshesForAnimation){
            return;
        }
        if (!isAnimating)
        {
        for (i = 0; i < meshesForAnimation.length; i++) {
            scene.beginAnimation(meshesForAnimation, 0, 300, true); 
      }
      isAnimating = true;
       }
       else
       {
          for (i = 0; i < meshesForAnimation.length; i++) {
            scene.stopAnimation(meshesForAnimation, 0, 300, true);               
          }
          isAnimating = false; 
       }
    });

Thanks for pointing me in the right direction!

animate.png.c75c4049c9a23c46709b14e124fd204d.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...