Using Reformat module from Python

I’m trying to use the Reformat module from a Python scripted module. I’m having trouble using the setSliceNormal and setWorldPosition methods. Looking at qSlicerReformatModuleWidget, both setSliceNormal and setWorldPosition have slots that take double*, and setSliceNormal has a public method that takes three arguments: setSliceNormal (double x, double y, double z). I tried to use it both ways, but I keep getting ValueError. Sample code:

reformatWidget = slicer.modules.reformat.widgetRepresentation()
slice = slicer.app.layoutManager().sliceWidget('Red')
sliceNode = slice.mrmlSliceNode()
reformatWidget.setEditedNode(sliceNode,"","")
reformatWidget.setSliceNormal(1.0,0,0)

The result is: “ValueError: Called setSliceNormal(double normal) -> void with wrong number of arguments: (1.0, 0.0, 0.0)”.
I also tried a list, tuple, and a ctypes double array.
Is there a way I can accomplish this with the Reformat module? Is there Python compatibility code that’s missing from this module?

Thanks

Is there a reason why you not simply modify the slice node?

I used (1,0,0) in the example, but I would like to be able to set a normal vector that’s off of the principal axes. Also a centre point of the rotation that’s not the centre of the image. I didn’t see a straightforward way to do that from vtkMRMLSliceNode.

In Github, I received a suggestion from @jcfr for how to improve the Python compatibility of the Reformat module by adding:

Q_PROPERTY(QVector3D worldPosition READ worldPosition WRITE setWorldPosition)

[...]

QVector3D worldPosition() const;
void setWorldPosition(const QVector3D& position); 

I’m not sure how the Q_PROPERTY macro works, but I’m willing to give this a try.

Slice position and orientation are specified by SliceToRAS matrix. You can get it using GetSliceToRAS method and update it. You can also use SetSliceToRASByNTP to set slice orientation by slice normal and X axis and position.

2 Likes

Thank you, that’s exactly the method I was hoping to find. I missed it when I was looking at the API docs. Looking at the documentation for it, and at the code, I’m still unclear what the final parameter, “Orientation”, should be set to. In the switch statement in the code, it refers to the cases 0,1,2 as para-Axial, para-Sagittal, and para-Coronal, respectively. Does that mean that if the view I want is close to axial, I should set Orientation to 0?

SetSliceToRASByNTP’s orientation parameter can be used to generate orthogonal views corresponding to an axial view. It just saves you from figuring out how to change the normal vector for orthogonal views.

Ok. My concern is that I don’t see a way that it will set the elements of the SliceToRAS matrix if I don’t supply an Orientation value of 0, 1, or 2. I couldn’t tell if that meant I was limited to the standard orthogonal views.

N specifies the slice normal direction if you set orientation to Axial.

Ok, thanks. I wanted to make sure I was interpreting it correctly.