How to record ARCamera Position and Rotation over time and save it to a file?
Asked Answered
B

1

3

I have been trying to create an ARView for over two days now that can record the position of the camera in space over time and then save this to a keyframe file. Basically, I want to create an app that lets you record virtual camera movements that can then be used in 3d applications like Autodesk Maya or Cinema4D to drive a camera. preferred file outputs would be anything that can hold a camera object and animate it over time (alternatively also an object that moves over time, that I can then parent my camera to).

Here is the code I have, sorry for it being a bit chaotic, I have tried a LOT of different things... Basically I try to record device position and rotation and then save it to an MDL object, but somehow it doesn't animate. I have also tried multiple different file types (some of those didn't support keyframe animation, so that didn't help, but from what I understand, Alembic does)

import SwiftUI
import ARKit
import RealityKit
import ModelIO

struct ARViewContainer: UIViewRepresentable {
    let session = ARSession()
    let delegate = MySessionDelegate()
    
    func makeUIView(context: Context) -> ARView {
        // Set up the ARView with session
        let arView = ARView(frame: .zero)
        let boxAnchor = try! Experience.loadBox()
        arView.scene.anchors.append(boxAnchor)
        arView.session.delegate = delegate // assign delegate to the session
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        // Update the ARView if needed
    }
    
    func startARSession() {
        // Start the ARSession
        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = [.horizontal, .vertical]
        session.run(configuration, options: [])
    }
    
    func stopARSession() {
        // Stop the ARSession
        session.pause()
    }
}

class MySessionDelegate: NSObject, ARSessionDelegate {
    var object: MDLMesh?
    let asset = MDLAsset()
    let cameraTransform = MDLTransform()
    var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    
    func session(_ session: ARSession, didUpdate frame: ARFrame) {
            // Get the camera position and orientation for the current frame
            let transform = frame.camera.transform
            let rotation = frame.camera.eulerAngles
            let position = transform.columns.3
            let elapsedTime = frame.timestamp
            cameraTransform.setTranslation(position[SIMD3(0,1,2)], forTime: elapsedTime)
            cameraTransform.setRotation(rotation, forTime: elapsedTime)
            print("Camera Transform: \(cameraTransform.matrix)")
    }

}

struct Camera: View {
    var body: some View {
        VStack {
            ARViewContainer().onAppear(perform: ARViewContainer().startARSession)
                .onDisappear(perform: ARViewContainer().stopARSession)
            Button("Export Recording") {
                // Create an MDLAsset with a box representing the camera transform
                let object = MDLMesh(boxWithExtent: .init(0.1, 0.1, 0.1), segments: .init(10, 10, 10), inwardNormals: false, geometryType: .triangles, allocator: nil)
                object.name = "Camera Transform"
                object.transform = MySessionDelegate().cameraTransform
                
                let asset = MDLAsset()
                asset.add(object)
                
                // Export the MDLAsset to a file
                let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
                let fileURL = documentsDirectory.appendingPathComponent("recording.abc")
                try! asset.export(to: fileURL)
            }
        }
        
    }
}

If there is a completely different way of doing it, please also share, I thank everybody in advance for any help!

Braddock answered 19/2, 2023 at 17:13 Comment(0)
M
2

Recording sixty 4x4 Transform Matrices per second

To write a 4x4 transformation matrix (i.e. a complex transform storing translate, rotate and scale) to a text file, I used write(to:) instance method. After clicking on the Record Transform Values button, the data immediately begins to be written to the toMaya.txt file. For convenience, I put all 16 values ​​of the matrix on the screen (I tested this on an iPad, so use a device with a bigger screen). Pressing the button again stops recording.

Data from nested lists in the toMaya.txt file can be easily read using regular Python or MEL scripting. Take a look at what nesting looks like. Each of the 16 matrix values ​​is in Float type.

enter image description here

[x0,y0,z0,w0] is a first matrix column, [x1,y1,z1,w1] is a second matrix column, etc.

Here's the code:

import SwiftUI
import RealityKit
import Combine

struct ARViewContainer : UIViewRepresentable {
    @Binding var arView: ARView

    func makeUIView(context: Context) -> ARView { return arView }
    func updateUIView(_ view: ARView, context: Context) { }
}

struct ContentView : View {
    
    @State private var arView = ARView(frame: .zero)
    @State private var subs: [AnyCancellable] = []
    @State private var array: [[[Float]]] = [[ [1,0,0,0], [0,1,0,0],
                                               [0,0,1,0], [0,0,0,1] ]]
    @State private var boolean = false
    
    let url = FileManager.default.urls(for: .documentDirectory,
                                        in: .userDomainMask)[0]
                                 .appendingPathComponent("toMaya.txt")
                                 
    var body: some View {
        ZStack {
            ARViewContainer(arView: $arView).ignoresSafeArea()
            VStack {
                Button("Record Transform Values") {
                    boolean.toggle()

                    DispatchQueue.main.async {
                        arView.scene.subscribe(to: SceneEvents.Update.self) { _ in
                                                        
                            let col = arView.cameraTransform.matrix.columns
                            
                            let mtx: [[Float]] = [
                                        [col.0.x, col.0.y, col.0.z, col.0.w],
                                        [col.1.x, col.1.y, col.1.z, col.1.w],
                                        [col.2.x, col.2.y, col.2.z, col.2.w],
                                        [col.3.x, col.3.y, col.3.z, col.3.w]
                            ]
                            if boolean {
                                array.append(mtx)
                            }
                            if let data = try? JSONEncoder().encode(self.array) {

                                guard let str = String(data: data, encoding: .ascii)
                                else { return }
                                
                                do {
                                    if boolean {
                                        try str.write(to: url, atomically: true,
                                                                 encoding: .ascii)
                                    }
                                } catch {
                                    print(error.localizedDescription)
                                }
                                print(url)
                            }
                        }.store(in: &subs)
                    }
                }
                Spacer()
            }
            VStack {
                Spacer()
                Text("\(array.count)").foregroundColor(.white)
            }
            HStack {
                VStack {
                    Text("\(array.last![0][0])").foregroundColor(.white)
                    Text("\(array.last![0][1])").foregroundColor(.white)
                    Text("\(array.last![0][2])").foregroundColor(.white)
                    Text("\(array.last![0][3])").foregroundColor(.white)
                }
                VStack {
                    Text("\(array.last![1][0])").foregroundColor(.white)
                    Text("\(array.last![1][1])").foregroundColor(.white)
                    Text("\(array.last![1][2])").foregroundColor(.white)
                    Text("\(array.last![1][3])").foregroundColor(.white)
                }
                VStack {
                    Text("\(array.last![2][0])").foregroundColor(.white)
                    Text("\(array.last![2][1])").foregroundColor(.white)
                    Text("\(array.last![2][2])").foregroundColor(.white)
                    Text("\(array.last![2][3])").foregroundColor(.white)
                }
                VStack {
                    Text("\(array.last![3][0])").foregroundColor(.white)
                    Text("\(array.last![3][1])").foregroundColor(.white)
                    Text("\(array.last![3][2])").foregroundColor(.white)
                    Text("\(array.last![3][3])").foregroundColor(.white)
                }
            }
        }
    }
}

My toMaya.txt file is waiting for me in the following debugging directory:

file:///var/mobile/Containers/Data/Application/7C675F52-C78B-4252-98B5-3EBD37A3F832/Documents/toMaya.txt

After that you can export your toMaya.txt file to Mac using UIActivityViewController.

Monastery answered 20/2, 2023 at 13:17 Comment(1)
Thanks for this! It isnt completely what i am looking for, but i bet i can figure something out that will make this work : )Braddock

© 2022 - 2024 — McMap. All rights reserved.