Python script to automate rotation

Here is the final code for anyone interested.

First I load up the repository script for rotation around a point as linked above. Then I input my code below to setup the manual increment of the transform.

from slicer import qMRMLLinearTransformSlider

increment = 1
transformNode = getNode('LinearTransform')
w = slicer.qMRMLLinearTransformSlider()
w.setMRMLTransformNode(transformNode)
w.TypeOfTransform = w.ROTATION_IS
w.applyTransformation(increment)

To automate and export a high resolution image, I use the script from here.

Adjust the range, image resolution, and the output directory/filename structure as needed:

for x in range (0, 61):
    w.applyTransformation(increment)
    vtk.vtkGraphicsFactory()
    gf = vtk.vtkGraphicsFactory()
    gf.SetOffScreenOnlyMode(1)
    gf.SetUseMesaClasses(1)
    rw = vtk.vtkRenderWindow()
    rw.SetOffScreenRendering(1)
    ren = vtk.vtkRenderer()
    rw.SetSize(3000, 3000)

    lm = slicer.app.layoutManager()
    ren3d = lm.threeDWidget(0).threeDView().renderWindow().GetRenderers().GetItemAsObject(0)
    # actors = ren3d.GetActors()
    # for index in range(actors.GetNumberOfItems()):
    #    ren.AddActor(actors.GetItemAsObject(index))
    # lights = ren3d.GetLights()
    # for index in range(lights.GetNumberOfItems()):
    #    ren.AddLight(lights.GetItemAsObject(index))
    volumes = ren3d.GetVolumes()
    for index in range(volumes.GetNumberOfItems()):
        ren.AddVolume(volumes.GetItemAsObject(index))
    camera = ren3d.GetActiveCamera()
    ren.SetActiveCamera(camera)

    rw.AddRenderer(ren)
    rw.Render()

    wti = vtk.vtkWindowToImageFilter()
    wti.SetInput(rw)
    wti.Update()
    writer = vtk.vtkPNGWriter()
    writer.SetInputConnection(wti.GetOutputPort())
    writer.SetFileName("Out/%d.png" % (x-30))
    writer.Update()
    writer.Write()
    i = wti.GetOutput()
1 Like