djeustice

Play Animation Button [SOLVED]

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.