Python Blender scripts trying to prevent UI blocking when doing big calculations

I work in blender, make a script for N number of objects. When running my script, it blocks the user interface while it does its work. I want to write something that will prevent this, so that I can see what is happening on the screen, and also use my user interface to show a progress bar. Any ideas on how this can be done in a python or blender? Most calculations take only a few minutes, and I know that this query can make them take longer than usual. Any help would be appreciated.

The function that does most of the work is a loop for a to b .

+7
source share
2 answers

If you want to do big calculations in Blender and still have a responsive interface, you can check the model statements with python timers.

It will be something like this:

class YourOperator(bpy.types.Operator): bl_idname = "youroperatorname" bl_label = "Your Operator" _updating = False _calcs_done = False _timer = None def do_calcs(self): # would be good if you can break up your calcs # so when looping over a list, you could do batches # of 10 or so by slicing through it. # do your calcs here and when finally done _calcs_done = True def modal(self, context, event): if event.type == 'TIMER' and not self._updating: self._updating = True self.do_calcs() self._updating = False if _calcs_done: self.cancel(context) return {'PASS_THROUGH'} def execute(self, context): context.window_manager.modal_handler_add(self) self._updating = False self._timer = context.window_manager.event_timer_add(0.5, context.window) return {'RUNNING_MODAL'} def cancel(self, context): context.window_manager.event_timer_remove(self._timer) self._timer = None return {'CANCELLED'} 

You need to take care of the proper import of modules and registration of operators yourself.

I have an implementation of the modal operator Conway Game Of Life to show how this can be used: https://www.dropbox.com/s/b73idbwv7mw6vgc/gol.blend?dl=0

+14
source

I suggest you use greenlets or create a new process . Greens are generally easier to use because you don’t need to worry about castles and race conditions, but they cannot be used in any situation. Using the multiprocess module or threading , deal with this problem.

+1
source

All Articles