4
$\begingroup$
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()()

$\endgroup$

3 Answers 3

2
$\begingroup$

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

$\endgroup$
1
  • 2
    $\begingroup$ 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) $\endgroup$
    – batFINGER
    Commented Sep 19, 2018 at 14:21
1
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ I wouldn't call it a limitation - it's just how things work in Blender. The results can still be achieved the way they are intended to be achieved the way I see it. $\endgroup$ Commented Sep 19, 2018 at 10:21
  • $\begingroup$ Blender is awesome $\endgroup$ Commented Sep 22, 2018 at 8:52
0
$\begingroup$

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.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .