0

I am trying to print the ARsession's parameters like the following. It works when I do it within a session.

func session(_ session: ARSession, didUpdate frame: ARFrame) {
          guard let arCamera = session.currentFrame?.camera else { return }
          print("ARCamera ProjectionMatrix = \(arCamera.projectionMatrix)")
    }

Result:

ARCamera ProjectionMatrix = simd_float4x4([[1.774035, 0.0, 0.0, 0.0], [0.0, 2.36538, 0.0, 0.0], [-0.0011034012, 0.00073593855, -0.99999976, -1.0], [0.0, 0.0, -0.0009999998, 0.0]])

However, I would like to access/print the parameters when a button is clicked. I tried the following:

@IBAction private func startPressed() {
    print(ARSession.init().currentFrame?.camera.projectionMatrix)
}

But the result above returns nil. Am I missing some obvious and can someone help me on how to correct this so I can properly access the camera parameters when the button is clicked?

Thanks in advance!

swiftlearneer
  • 324
  • 4
  • 17

1 Answers1

1

You are initializing a new session by calling ARSession.init(), so there is no wonder it returns nil. You should use an instance of currently running ARSession instead. Assuming you are using ARSCNView to render the AR experience, correct code would look like this:

@IBAction private func startPressed() {
    if let matrix = sceneView.session.currentFrame?.camera.projectionMatrix {
       print(matrix)
    }
}
Ivan Nesterenko
  • 939
  • 1
  • 12
  • 23
  • Just a follow up here, its possible to just print the matrix number? I am printing the type and optional as well. `Optional(simd_float4x4([[1.774035, 0.0, 0.0, 0.0], [0.0, 2.36538, 0.0, 0.0], [-0.0011034012, 0.00073593855, -0.99999976, -1.0], [0.0, 0.0, -0.0009999998, 0.0]])) ` – swiftlearneer Jul 27 '20 at 15:09
  • 1
    You can unwrap `matrix` property to get rid of "Optional" in the print result. I've updated the answer with an example. – Ivan Nesterenko Jul 27 '20 at 15:48
  • Thanks! I force-unwarpped it with ! and its gone! And also with the simd_float4x4 matrix type. Is there a way to not show it? – swiftlearneer Jul 27 '20 at 15:52
  • No way to do it as far as I know:) – Ivan Nesterenko Jul 27 '20 at 16:29