# Real Time Orientation Changing Through Python

**URL:** <https://discourse.slicer.org/t/real-time-orientation-changing-through-python/32648>\
**Category:** Development\
**Tags:** python\
**Created:** [November 7, 2023, 6:25pm UTC](https://discourse.slicer.org/t/real-time-orientation-changing-through-python/32648 "2023-11-07T18:25:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![floppygator45](https://avatars.discourse-cdn.com/v4/letter/f/779978/32.png) [@floppygator45](https://discourse.slicer.org/u/floppygator45)\
**Post date:** [November 7, 2023, 6:25pm UTC](https://discourse.slicer.org/t/real-time-orientation-changing-through-python/32648/1 "2023-11-07T18:25:29Z")

</div>

Relatively new to 3D slicer. I have been looking at the reference guide that 3dslicer provided and found 3D object orientation can be changed through python. Currently this is the code I have where it prints a random yaw value and sends it to 3D slicer to change the orientation. The issue is that the orienation does not update but waits for the “while” loop to complete before updating the yaw orientation in 3D slicer. I would like to see the 3D object updated for each new yaw value if that is possible. Here is my code for reference. Any help would be appreciated.

```auto
import random
from time import sleep
count = 0
while True:
    yaw1 = random.randrange(90)
    layoutManager = slicer.app.layoutManager()
    threeDWidget = layoutManager.threeDWidget(0)
    threeDView = threeDWidget.threeDView()
    threeDView.yawDirection = threeDView.YawRight
    threeDView.setPitchRollYawIncrement(yaw1)
    threeDView.yaw()
    sleep(2)
    print(yaw1)
    count += 1
    if count >= 5:
        break

```

---

<div class="post-metadata">

**Author:** ![mau\_igna\_06](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mau_igna_06/32/9056_2.png) [@mau\_igna\_06](https://discourse.slicer.org/u/mau_igna_06)\
**Post date:** [November 7, 2023, 6:35pm UTC](https://discourse.slicer.org/t/real-time-orientation-changing-through-python/32648/2 "2023-11-07T18:35:01Z")

</div>

Maybe you are looking for something like this inside your for loop:

```auto
threeDView.setPitchRollYawIncrement(yaw1)
threeDView.yaw()
slicer.app.processEvents() # let's updates on the slicer scene happen

```

Hope it helps

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [November 7, 2023, 9:46pm UTC](https://discourse.slicer.org/t/real-time-orientation-changing-through-python/32648/3 "2023-11-07T21:46:50Z")

</div>

In addition to Mauro’s suggestion, I’d add that you shouldn’t use `sleep` in your Slicer python code. Instead you should use [`qt.QTimer`](https://doc.qt.io/qt-5/qtimer.html) related functions with callbacks so that your code integrates cleanly with the event loop like [this example](https://github.com/Slicer/Slicer/blob/main/Modules/Scripted/Endoscopy/Endoscopy.py#L64-L66).
