2

I have a simple RealityKit scene that displays a .USDZ cow. I saw a couple answers to these questions, but I wasn't able to implement them into my code:

  1. How can I play the animations that are built into this file?
  2. How am I able to bring a second object (let's say a horse) into the scene?

Additionally, is there anything I can do to clean up my code/ make it easier to read?

I appreciate any help I can get! Thank you,

import UIKit
import RealityKit
import ARKit

 class WelcomeViewController: UIViewController {

//delay app launch to show splash screen
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        Thread.sleep(forTimeInterval: 3.0)
        // Override point for customization after application launch.
        return true
    }
//end splash screen delay

   @IBAction func gotPressed(_ sender: Any) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    if let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as? ViewController {
        self.present(viewController, animated: true, completion: nil) /// present the view controller (the one with the ARKit)!
    }    }

 }
 class ViewController: UIViewController {

@IBOutlet var arView: ARView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    overlayCoachingView()
    

}
//Overlay coaching view "adjust iphone scan"
func overlayCoachingView () {
    
    let coachingView = ARCoachingOverlayView(frame: CGRect(x: 0, y: 0, width: arView.frame.width, height: arView.frame.height))
    
    coachingView.session = arView.session
    coachingView.activatesAutomatically = true
    coachingView.goal = .horizontalPlane
    
    view.addSubview(coachingView)
    
}//end overlay
{
let cow = try! ModelEntity.load(named: "COW_ANIMATIONS")
let horse = try! ModelEntity.load(named: "White_Horse")

cow.position.x = -1.0
horse.position.x = 1.0

let anchor = AnchorEntity()
cow.setParent(anchor)
horse.setParent(anchor)
arView.scene.anchors.append(anchor)

let cowAnimationResource = cow.availableAnimations[0]
let horseAnimationResource = horse.availableAnimations[0]

cow.playAnimation(cowAnimationResource.repeat(duration: .infinity),
                                    transitionDuration: 1.25,
                                          startsPaused: false)

horse.playAnimation(horseAnimationResource.repeat(duration: .infinity),
                                        transitionDuration: 0.75,
                                              startsPaused: false)}


  }

enter image description here

enter image description here

Tyler Kurtz
  • 137
  • 9

1 Answers1

4

In RealityKit SDK, to play an animation contained in a single .usdz file, use the following code:

let cow = try ModelEntity.load(named: "cow")
let horse = try ModelEntity.load(named: "horse")

cow.position.x = -1.0
horse.position.x = 1.0

let anchor = AnchorEntity()
cow.setParent(anchor)
horse.setParent(anchor)
arView.scene.anchors.append(anchor)

let cowAnimationResource = cow.availableAnimations[0]
let horseAnimationResource = horse.availableAnimations[0]

cow.playAnimation(cowAnimationResource.repeat(duration: .infinity), 
                                    transitionDuration: 1.25, 
                                          startsPaused: false)

horse.playAnimation(horseAnimationResource.repeat(duration: .infinity), 
                                        transitionDuration: 0.75, 
                                              startsPaused: false)

In RealityKit 2.0, by default, you can play only the first animation contained in a single usdz file. However, there are the workaround 1 and the workaround 2.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    Thank you so much! I'll let you know how it looks when I figure out this anchor issue I'm having. Appreciate the assistance! – Tyler Kurtz Oct 06 '20 at 23:48
  • 1
    Hey Andy, I'm hoping you can help me on this bit of code. I've added it to my project (code is updated in post) and then I placed curly brackets at the start and end of the code you supplied. Now I'm getting an "Expected Declaration" error right before the "let cow =" statement. Clicking that brings me to the ViewController class and says "1. In declaration of 'ViewController'" Did I do this wrong? Any help is greatly appreciated! – Tyler Kurtz Oct 11 '20 at 15:23
  • Hey Tyler. Show me the lines of your code that produce errors, please. – Andy Jazz Oct 11 '20 at 15:32
  • I've attached a screenshot in the posting! Thank you! – Tyler Kurtz Oct 11 '20 at 15:46