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

**URL:** <https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523>\
**Category:** Development\
**Created:** [September 6, 2021, 1:55am UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523 "2021-09-06T01:55:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [September 6, 2021, 1:55am UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523/1 "2021-09-06T01:55:50Z")

</div>

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:

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

```

and

```cpp
  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:

```cpp
  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`):

```cpp
  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](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#access-vtk-views-renderers-and-cameras)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 6, 2021, 12:53pm UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523/2 "2021-09-06T12:53:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [September 6, 2021, 1:29pm UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523/3 "2021-09-06T13:29:01Z")

</div>

Yes, here is the [link to an extension I just uploaded to github](https://github.com/tierra-colada/Seismic).  
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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [September 8, 2021, 3:41am UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523/4 "2021-09-08T03:41:27Z")

</div>

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.

> [@keri](#):
>
> 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

There is already a slider+spin widget in CTK: [ctkSliderWidget](http://commontk.org/index.php/Documentation/ImageGallery). Instead of creating a new widget maybe you could improve this existing one.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [September 8, 2021, 12:40pm UTC](https://discourse.slicer.org/t/unable-to-get-vtkcamera-from-vtkmrmlcameranode-from-c-only/19523/5 "2021-09-08T12:40:23Z")

</div>

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!
