4
import bpy


 from time import sleep
 x = 1
 for x in range(10):
...     print("Done")
...     sleep(1)

If run this script, the blender hangs for a while and restores again with 10 Messages 'Done'

But what I need is it should print the string 'Done' one by one. but it hangs for a while and prints all the 10 strings named Done altogether at once.

Same thing happens with bpy.ops.transform.translate()()

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187

3 Answers3

2

This is how scripts are intended to be run in Blender. You should not attempt to update the interface while running a script - that can cause problems with other processes in Blender.

If you need some functionality that runs in Blender while the rest of Blender is functioning at the same time you should make a modal operator. There are a few examples included with Blender that you can reach from the Text Editor's header menu Templates - > Python -> Operator Modal... :

enter image description here

Martynas Žiemys
  • 24,274
  • 2
  • 34
  • 77
  • 2
    IMO The modal timer operator emulates sleep loop above the best. To have the timer event fire every second self._timer = wm.event_timer_add(1, context.window) – batFINGER Sep 19 '18 at 14:21
1

That is a known limitation of how blender works.

One option is to run a task in a separate process as shown here.

Another option is to use a modal operator. You can find a few example modal operator templates in blenders text editor.

sambler
  • 55,387
  • 3
  • 59
  • 192
0

I found a better answer with the help of you all awesome guys, first off Thanks so much.

and the answer is python template "operator_modal_timer.py"

and little of changing the code in "if event.type == 'TIMER':" section worked for me.