title: SceneKit - Stop continuously looping Collada animation
body: <p>I am using SceneKit to load a Collada (.dae) file. </p>
  
  <p>Whatever I have tried, the animation repeats in a continuous loop whereas I only want it play once and then stop. </p>
  
  <p>Here's the loading code where sceneView is my SCNView:</p>
  
  <pre><code>    //Load the scene and play
      let scene = SCNScene(named: "Scenes.scnassets/test4.dae")
      sceneView.scene = scene
      sceneView.autoenablesDefaultLighting = true
      sceneView.allowsCameraControl = true
  </code></pre>
  
  <p>The scene loads correctly and the animation plays on loading as required, but in a continuous loop.</p>
  
  <p>I have tried removing all animations as follows:</p>
  
  <pre><code>sceneView.scene?.rootNode.removeAllAnimations()
  </code></pre>
  
  <p>but this seems to have no effect.</p>
  
  <p>I have also tried retrieving all animations and setting the repeatCount = 1 or even to 0</p>
  
  <pre><code>    let url = NSBundle.mainBundle().URLForResource("Scenes.scnassets/test4", withExtension: "dae")
      let sceneSource = SCNSceneSource(URL:url!, options: nil)
  
  
      let animationIndentifiers = sceneSource?.identifiersOfEntriesWithClass(CAAnimation)
  
      if let ids = animationIndentifiers
      {
          for id in ids {
              let animation: CAAnimation = sceneSource!.entryWithIdentifier(id as! String, withClass: CAAnimation.self)! as! CAAnimation
              animation.repeatCount = 1
          }
      }
  </code></pre>
  
  <p>I have also tried this another way:</p>
  
  <p>let keys = sceneView.scene!.rootNode.animationKeys()  //get all the animation keys</p>
  
  <pre><code>if let theKeys = keys {
      for key in theKeys {
          let animation = sceneView.scene?.rootNode.animationForKey(key as! String)
          animation?.repeatCount = 1
      }
  }
  </code></pre>
  
  <p>but again no luck.</p>
  
  <p>I can find nothing in the Collada file itself that is obviously causing the animation to repeat. However, I notice that the .dae file icon on my Mac disk has a play button and clicking this also plays the animation within the icon on a continuous loop.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I now notice that in the code above I am setting the constant 'animation' attributes and this is not copied back to the actual scene nodes. Also, the only animation in the .dae file is in a child node. So here's my next attempt:</p>
  
  <pre><code>  //Get the child nodes
    let children = scene?.rootNode.childNodes
  
    //Initialise a childNode counter
    var childCount = 0
  
    //Iterate through the child nodes     
    if let theChildren = children {
       for child in theChildren {
          let keys = child.animationKeys()  //get all the animation keys
          //Iterate through the animations
          if let theKeys = keys {
             for key in theKeys {
                 let animation = child.animationForKey(key as! String)
                 println("Initial Repeat count: \(animation.repeatCount)")
                 animation.repeatCount = 1
                 //Remove existing animation
                 scene?.rootNode.childNodes[childCount].removeAnimationForKey(key as! String)
                 //Add amended animation
                 scene?.rootNode.childNodes[childCount].addAnimation(animation, forKey: key as! String)
              }
           }
           childCount++
      }
   }
  </code></pre>
  
  <p>There is actually only one animation attached to one child node. (I have also tried setting this using the the actual animation id string in place of 'key' above.)</p>
  
  <p>The above shows <code>Initial Repeat count: inf</code>
  and on checking afterwards it is indeed set to 1.
  However, the animation still runs in an infinite loop :-(</p>
  
  <p>Any help to resolve this would be much appreciated.</p>
  
  <p><strong>Further update</strong></p>
  
  <p>I have now created a new Collada file with simple animation using Maya and for some reason one of the trials attempted above actually works:</p>
  
  <pre><code>    func sceneSetup() {
        let scene = SCNScene(named: "Scenes.scnassets/test10.dae")
  
        let children = scene?.rootNode.childNodes
        var childCount = 0
        if let theChildren = children {
          for child in theChildren {
            let keys = child.animationKeys()  //get all the animation keys
            if let theKeys = keys {
              for key in theKeys {
                let animation = child.animationForKey(key as! String)
                animation.repeatCount = 1
                scene?.rootNode.childNodes[childCount].removeAnimationForKey(key as! String)
                scene?.rootNode.childNodes[childCount].addAnimation(animation, forKey: key as! String)
              }
            }
            childCount++
          }
      }       
       sceneView.scene = scene
       sceneView.autoenablesDefaultLighting = true            
    }
  </code></pre>
  
  <p>if anybody can explain that would be great!</p>
  
  <p><strong>Ah, but there's another problem!</strong></p>
  
  <p>Here's the start frame of the animation:</p>
  
  <p><img src="https://i.stack.imgur.com/OSC5s.png" alt="Start frame"></p>
  
  <p>and here's the end frame where we want to end up:</p>
  
  <p><img src="https://i.stack.imgur.com/qLvde.png" alt="End frame"></p>
  
  <p>But at the end of the animation the scene jumps back to the start view.</p>
  
  <p>I have 'fixed' this by amending the animation so that frame 1 is copy of the final frame. This works and isn't noticeable but doesn't seem a very elegant solution.</p>