Using a scene from Reality Composer Pro in an IOS app?

I am trying to establish a workflow with using Reality Composer Pro to make scenes - I am grey boxing a scene using primitives at the moment.

I have set up a cube with a texture material and a simple animation to spin.

I am confused as to what I should be loading. I have created what I think is a scene asset in the package for the Reality Composer Project.

Here is a code snippet:

struct ContentView: View {
    var body: some View {
        RealityView { content in
            do {
                let scene = try await ModelEntity(named: "HOF")
                content.add(scene)
            } catch {
                print("Error loading scene: \(error.localizedDescription)")
            }
        }
    }
}

Here is the project layout in Reality Composer Pro:

Answered by Miro in 791266022

I went took a look at the Hello World sample & I see I made 2 mistakes:

  • use Entity(named:
  • and use the appropriate path:
struct ContentView: View {
    var body: some View {
        RealityView { content in
            do {
                let scene = try await Entity(named: "Scenes/HOF", in: HeadsOnFire.headsOnFireBundle)

                content.add(scene)

            } catch is CancellationError {
                // The entity initializer can throw this error if an enclosing
                // RealityView disappears before the model loads. Exit gracefully.
                return

            } catch let error {
                // Other errors indicate unrecoverable problems.
                print("Failed to load cube: \(error)")
            }
        }
    }
}
Accepted Answer

I went took a look at the Hello World sample & I see I made 2 mistakes:

  • use Entity(named:
  • and use the appropriate path:
struct ContentView: View {
    var body: some View {
        RealityView { content in
            do {
                let scene = try await Entity(named: "Scenes/HOF", in: HeadsOnFire.headsOnFireBundle)

                content.add(scene)

            } catch is CancellationError {
                // The entity initializer can throw this error if an enclosing
                // RealityView disappears before the model loads. Exit gracefully.
                return

            } catch let error {
                // Other errors indicate unrecoverable problems.
                print("Failed to load cube: \(error)")
            }
        }
    }
}

Well, I figured it out from the Happy Beam demo code here: Happy Beam Docs

Problem: the Bundle var wasn’t found in scope.

Solution:

  1. Make sure that your Reality Composer Pro Package has been as a Framework in he General Project Settings

  2. Import (your package name)

  3. In the Sources directory that Reality Composer Pro created, there is a Swift file that contains var you’re looking for usually (your project name + Bundle; i.e. “projectnameBundle”)

  4. Load by creating an entity; scene = Entity(named: “Scene”, in: projectnameBundle)

  5. Add the entity to your RealityView; content.add(scene)

Note: those will place the scene at your cameras location. So, be sure to move the camera away from the starting point to verify, but it’d be best to add a horizontal anchor and add the entity to the anchor, then the anchor to the RealityView to be less confusing Visually.


import RealityKit
import projectName

struct ContentView : View {

    var body: some View {
        RealityView { content in

// Create horizontal plane anchor
           let anchor = AnchorEntity(.plane(.horizontal, classification: .any, minimumBounds: SIMD2<Float>(0.2, 0.2)))

// Load Scene from Reality Composer Pro Package
            do {
                let scene = try await Entity(named: "Scene", in: projectnameBundle)

                // Add model to anchor
                anchor.addChild(scene)

                // Add anchor to RealityView
                content.add(anchor)


            } catch is CancellationError {

                // The entity initializer can throw this error if an enclosing
                // RealityView disappears before the model loads. Exit gracefully.
                return
            } catch let error {
                // Other errors indicate unrecoverable problems.
                print("Failed to load scene: \(error)")
            }

            // View Settings
            content.camera = .spatialTracking
        }
        .edgesIgnoringSafeArea(.all)
    }
}
Using a scene from Reality Composer Pro in an IOS app?
 
 
Q