# Python script to automate rotation

**URL:** <https://discourse.slicer.org/t/python-script-to-automate-rotation/33275>\
**Category:** Support\
**Tags:** transforms, python, scripted, screen-capture\
**Created:** [December 6, 2023, 11:41pm UTC](https://discourse.slicer.org/t/python-script-to-automate-rotation/33275 "2023-12-06T23:41:43Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Michael\_Allen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michael_allen/32/66371_2.png) [@Michael\_Allen](https://discourse.slicer.org/u/Michael_Allen)\
**Post date:** [December 10, 2023, 7:55pm UTC](https://discourse.slicer.org/t/python-script-to-automate-rotation/33275/3 "2023-12-10T19:55:15Z")

</div>

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.

```auto
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.](https://discourse.slicer.org/t/higher-resolution-for-screen-captures-of-3d-view/8880/30)

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

```auto
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()

```

---

_[View the full topic](https://discourse.slicer.org/t/python-script-to-automate-rotation/33275)._
