Unable to get `vtkCamera*` from `vtkMRMLCameraNode*` (from C++ only)

Hi,

From module widget (qSlicerMyModuleWidget.cxx) I was trying to get active camera of a 3D view and apply transform to it.

The only two working ways to do that were:

  qMRMLThreeDWidget* threeDWidget = app->layoutManager()->threeDWidget('View1');
  vtkCamera* camera =
      threeDWidget->threeDView()->renderer()->GetActiveCamera();

and

  qMRMLThreeDWidget* threeDWidget = app->layoutManager()->threeDWidget('View1');
  vtkCamera* camera =
     threeDWidget->threeDView()->activeCamera();

But there are other high level ways to get camera from vtkMRMLCameraNode.
For example:

  vtkMRMLCameraNode* cameraNode =
      threeDWidget->threeDView()->cameraNode();

  if (!cameraNode)
    qCritical() << Q_FUNC_INFO << ": No active camera node found";

  vtkCamera* camera = cameraNode->GetCamera();  // there I get an exception "read access violation" even the exception abovev was not triggered

I get an exception on line where I’m trying to get vtkCamera*. Is it normal behaviour?

Also I’ve tried to get comeras’s logic and then retrieve from that camera and apply transform to camera (preliminary I linked to the vtkSlicerCamerasModuleLogic):

  vtkSlicerCamerasModuleLogic* camerasLogic =
    vtkSlicerCamerasModuleLogic::SafeDownCast(this->moduleLogic("Cameras"));
  if (!camerasLogic)
    qCritical() << Q_FUNC_INFO << ": Cameras module is not found";

  vtkMRMLCameraNode* cameraNode =
      camerasLogic->GetViewActiveCameraNode(viewNode);

  if (!cameraNode)
    qCritical() << Q_FUNC_INFO << ": No active camera node found";

  vtkCamera* camera = cameraNode->GetCamera();

  if (!camera)
    qCritical() << Q_FUNC_INFO << ": No active camera found";

  vtkNew<vtkTransform> transform;
  transform->Scale(2, 1, 1);
  camera->SetModelTransformMatrix(transform->GetMatrix());    // here I gen an exception read access violation: `vtkObject::GetDebug()`

I get an exception now only when I try to apply transform.

All of this seemed to me strange so I decided to ask here… perhaps I don’t understand how vtkMRMLCameraNode is initialized?

By the way python example script works fine

All the code snorts above shows work. Probably your try to access the wrong objects, or access them to early, or from the wrong thread. Can you send a Github link to your module so that we can see the complete context of the method calls?

Yes, here is the link to an extension I just uploaded to github.
I work with SlicerCAT but the module Zoom depends on the native Slicer (you don’t need other modules).

The module is in progress.
You can find three GainWidgets (slider+spinbox). Try to use this widget for 3D view (only 3D view widgets are connected for now). I think you will understand what this widget is aimed for and if the community found it useful I could later add this widget to CTK library

The Zoom module works well for me. Probably you have memory corruption because of using outdated DLLs - most likely due to either an incomplete build or because you have copied DLLs around.

There is already a slider+spin widget in CTK: ctkSliderWidget. Instead of creating a new widget maybe you could improve this existing one.

1 Like

You are right, I have rebuilt the solution and now there is no problem to get the camera.
I didn’t have an idea that the reason is outdated libs…
Thank you!