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

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!

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.

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:

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

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

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” 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?

You were on the right track. In the toggleInteractionBox code in Slicer/qSlicerSubjectHierarchyTransformsPlugin.cxx at 0094e9a35bd266cc8b0e677858dabce797c9928f · Slicer/Slicer · GitHub 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.

1 Like

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.

Thank you very much, Mike.

I finally made it.

The code should be

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