1

The situation

I am making an AR app in xcode (11.3.1). I have added objects (e.g. a cube) into the scene using reality composer and added behaviours (i.e. tap and flip and look at camera) to those objects, also using reality composer. Saved that, switched to ViewController.swift

In ViewController, I load in the Experience.rcproject and access the default Box scene by writing var box = try! Experience.loadBox(). All works as expected.

I am then printing the various objects in the hierachy to understand how they are constructed. So I will for example write print(box) and see all the entities and components as they are described here

The problem

I can see things like Transform for position etc. and ModelComponent for the mesh, materials etc. and this all makes sense but I cannot see where the behaviours are stored within the object that the .rcproject becomes within swift.

For example, if I have added a look at camera behaviour in reality composer, my assumption would be that there would be something like a `billboard' component attached to that object, but I don't see any difference between objects that have behaviours applied, and those that don't..

Another example would be, having addded tap and flip to an object, I would expect to find some animation information somewhere within the object, but again I can't see it attached to the object. Nor can I see any animation info, or behaviour components any where within the scene object.

Does anywhere know where I might be able to access these? There seems to be something under box called actions but printing that simply returns Experience.Box.Actions with no more info.

Am I looking in the wrong place? Or are these not exposed?

Andy Jazz
  • 36,357
  • 13
  • 107
  • 175
Nick
  • 854
  • 10
  • 28

1 Answers1

1

Partial Solution

You can access and recreate behaviours in a certain, quite limited way. For that you need to include Notify action with definite name in action's sequence in Reality Composer.

enter image description here

Here's how a code for RealityKit looks like:

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)

        boxAnchor.actions.repetitive.onAction = self.remodelling
    }
    
    fileprivate func remodelling(_ entity: Entity?) {

        guard let entity = entity else { return }
        entity.children[0].position.x = -0.25
        
        let modelEntity = entity.children[0] as! ModelEntity
        modelEntity.model?.mesh = .generateSphere(radius: 0.1)
        modelEntity.model?.materials = [UnlitMaterial(color: .green)]
    }
}

enter image description here

Where actions instance property is:

public private(set) lazy var actions: Actions { get set }

repetitive is:

public private(set) lazy var repetitive: Experience.NotifyAction { get set }

onAction is:

public var onAction: ((Entity?) -> Void)?


Additional Info

Here's non-editable content for ActionsAndBehaviours.Experience.Box.Actions:

import Combine
import Foundation
import RealityKit
import SwiftOnoneSupport
import UIKit
import simd

@available(iOS 13.0, macOS 10.15, *)
public enum Experience {

    public class NotifyAction {

        public let identifier: String

        public var onAction: ((Entity?) -> Void)?
    }

    public enum LoadRealityFileError : Error {

        case fileNotFound(String)
    }

    public static func loadBox() throws -> ActionsAndBehaviours.Experience.Box

    public static func loadBoxAsync(completion: @escaping (Result<ActionsAndBehaviours.Experience.Box, Error>) -> Void)

    public class Box : Entity, HasAnchoring {

        public var steelBox: Entity? { get }

        lazy public private(set) var actions: ActionsAndBehaviours.Experience.Box.Actions

        public class Actions {

            lazy public private(set) var repetitive: ActionsAndBehaviours.Experience.NotifyAction

            lazy public private(set) var allActions: [ActionsAndBehaviours.Experience.NotifyAction]
        }
    }
}
Andy Jazz
  • 36,357
  • 13
  • 107
  • 175
  • Hi AndyFedo, thanks for your response but printing the components does not show any of the ‘behaviours’ that have been added in Reality Composer (E.g. animations or look at camera) perhaps they are not components? But if not, what are they and how can they be accessed within the scene object? – Nick Jan 31 '20 at 20:21
  • 1
    Thanks for the info AndyFedo. I look forward to a future version. And thanks for the snippet, that's helpful. – Nick Feb 03 '20 at 21:01