# How to get the transform edited in 3D views interactively in Python Interacter

**URL:** https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322
**Category:** Support
**Tags:** segmentation
**Created:** [October 24, 2021, 1:20pm UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322 "2021-10-24T13:20:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Springle](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/springle/32/12901_2.png) [@Springle](https://discourse.slicer.org/u/Springle)
#### Post date: [October 24, 2021, 1:20pm UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/1 "2021-10-24T13:20:38Z")

</div>

Operating system: win10 64 20H2  
Slicer version: 4.11.20200930  
Expected behavior: get the transform edited in 3D views interactively in Python Interacter  
Actual behavior:

I am trying to automate my segmentation process using Python Interacter, but sometimes still needs manual opreation. After the ROI drawing, i need to rotate the segmentation label, so i create a new transformnode and link it to the segmentation node. But i can not find a way to get the transform edited in 3D views interactively, as the “interation in 3d view” function do in the GUI. The following is the code patch:

SegNode = slicer.mrmlScene.GetFirstNodeByClass(“vtkMRMLSegmentationNode”)  
transformNode = slicer.vtkMRMLTransformNode()  
slicer.mrmlScene.AddNode(transformNode)  
SegNode.SetAndObserveTransformNodeID(transformNode.GetID())  
SegNode.CreateClosedSurfaceRepresentation()

and the next code is to get the transform edited in 3D views interactively.

Is there a way to do it?

Any help is much appreciated!

---

<div class="post-metadata">

### Author: ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)
#### Post date: [October 25, 2021, 2:56am UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/2 "2021-10-25T02:56:28Z")

</div>

Hi, I used your code, then edited the transform interactively, and then printed the edited transformation matrix with this code in the python interactor:  
`print(transformNode.GetMatrixTransformToParent())`  
The printed transform matrix was the same as what I see in the Transforms module. So I guess you can use “transformNode” to get the results of your interactions.

---

<div class="post-metadata">

### Author: ![Springle](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/springle/32/12901_2.png) [@Springle](https://discourse.slicer.org/u/Springle)
#### Post date: [October 25, 2021, 3:45am UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/3 "2021-10-25T03:45:53Z")

</div>

Hi, Tamas,

Thank you very much for you reply. What i need is to get a boundary or bounding box of my segmentation in 3D view window and then to drag or rotate the segmentation boundary to modify the position of my segmentation. This can be achieved in the GUI as this post did:

> [@Moving segmentation](https://discourse.slicer.org/t/moving-segmentation/14886/3):
>
> You can do all this in the Data module, by right-clicking on the transforms column: [interactive\_transform] Transform is rotated by left-click-and-drag, and translated by Shift+left-click-and-drag. “Harden transform” in the end if for permanently applying the transformation to the selected node, which is probably not needed for what the use case described above.

But i can not find a code way to do it.

---

<div class="post-metadata">

### Author: ![Springle](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/springle/32/12901_2.png) [@Springle](https://discourse.slicer.org/u/Springle)
#### Post date: [October 25, 2021, 9:57am UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/4 "2021-10-25T09:57:55Z")

</div>

According to the FAQ:  
[https://slicer.readthedocs.io/en/latest/developer\_guide/python\_faq.html#how-to-find-a-python-function-for-any-slicer-features](https://slicer.readthedocs.io/en/latest/developer_guide/python_faq.html#how-to-find-a-python-function-for-any-slicer-features)

I found this action is defined in qSlicerSubjectHierarchyTransformsPlugin.cxx file, and the connected method is: void qSlicerSubjectHierarchyTransformsPlugin::toggleInteractionBox(bool visible). It is defined in “[Slicer/qSlicerSubjectHierarchyTransformsPlugin.cxx at 0094e9a35bd266cc8b0e677858dabce797c9928f · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/0094e9a35bd266cc8b0e677858dabce797c9928f/Modules/Loadable/Transforms/SubjectHierarchyPlugins/qSlicerSubjectHierarchyTransformsPlugin.cxx)” in line 443.

However i still fail to emulate this “interaction in 3d view” action. I thought i need to access the qSlicerSubjectHierarchyTransformsPlugin object, but i fail to access it using slicer.qSlicerSubjectHierarchyTransformsPlugin().toggleInteractionBox.

I also failed to emulate it trying to using the code inside the “toggleInteractionBox” function.

Anyone has some suggestions?

---

<div class="post-metadata">

### Author: ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)
#### Post date: [October 25, 2021, 6:34pm UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/5 "2021-10-25T18:34:43Z")

</div>

You were on the right track. In the toggleInteractionBox code in [Slicer/qSlicerSubjectHierarchyTransformsPlugin.cxx at 0094e9a35bd266cc8b0e677858dabce797c9928f · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/blob/0094e9a35bd266cc8b0e677858dabce797c9928f/Modules/Loadable/Transforms/SubjectHierarchyPlugins/qSlicerSubjectHierarchyTransformsPlugin.cxx#L473) I saw displayNode.SetEditorVisibility(visible), which helped me find the following

`transformNode.GetDisplayNode().SetEditorVisibility(True)`

to show the interactive controls (setting visibility to False would hide them). I think that’s what you were looking for, if not, please clarify and ask again.

---

<div class="post-metadata">

### Author: ![Springle](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/springle/32/12901_2.png) [@Springle](https://discourse.slicer.org/u/Springle)
#### Post date: [October 26, 2021, 12:23am UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/6 "2021-10-26T00:23:13Z")

</div>

Thanks Mike,

i tried what you told me. But `transformNode.GetDisplayNode()` returns NULL, and can not call `SetEditorVisibility` method.

The newly create transform do not have transform display node. Maybe i should create a new display node and try to link it to the transform node.

---

<div class="post-metadata">

### Author: ![Springle](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/springle/32/12901_2.png) [@Springle](https://discourse.slicer.org/u/Springle)
#### Post date: [October 26, 2021, 12:31am UTC](https://discourse.slicer.org/t/how-to-get-the-transform-edited-in-3d-views-interactively-in-python-interacter/20322/7 "2021-10-26T00:31:50Z")

</div>

Thank you very much, Mike.

I finally made it.

The code should be

```auto
SegNode = slicer.mrmlScene.GetFirstNodeByClass('vtkMRMLSegmentationNode')
transformNode = slicer.vtkMRMLTransformNode()
slicer.mrmlScene.AddNode(transformNode)
SegNode.SetAndObserveTransformNodeID(transformNode.GetID())
SegNode.CreateClosedSurfaceRepresentation()
transformNode.CreateDefaultDisplayNodes()
transformNode.GetDisplayNode().SetEditorVisibility(True)

```
