# Subscribe to 'Finished' event of modal operator and get result of the operator

I am currently writing a python script and call a modal operator of an external addon like this: bpy.ops.uvpackmaster2.uv_pack("INVOKE_DEFAULT")

My problem now is, that this call only returns {'RUNNING_MODAL'} but I need to wait till this is finished before I run other tasks. Furthermore this operator can also fail - or let's not say fail, it can require further action and a further run.

I am looking for something like the 'addEventListerner' known from JavaScript or anything like that. I am very known to programming but kind of new to python. Is there something similar I can use?

Also, the second part of the question: I use the addon UVPackmaster2. If I invoke the uv_pack function the addon at first checks if there are some problems with the uv_map, if so the problematic faces get selected. If there are no problems the packing process starts and ends after a few seconds/minutes. It looks like there is no specific output I can use to determine if the process requires further investigation or if it run properly. I only thing I saw, was that the plugin puts output on the report via self.report(...). But I really don't like the idea to parse the report output to get the information in which state the process is. Do you have any better ideas? Or how I could patch the addon to support me with useful output? But I don't know how a modal operator should communicate with my script.

I allready found this Running code after a modal operator finished its job but I really don't understand how the example works. And how I could adapt this to my problem.

thanks and best regards!

After a week of reasearch and discussion on blenderartist iceythe helped me finding a solution writing a wrapper for the uv packmaster modal of the pack operator. (bpy.ops.uvpackmaster2.uv_pack())

Wrapper

import bpy
from uvpackmaster2 import UVP2_OT_PackOperator as op
from .bake_operator import callback

def modal_wrap(modal_func):

def wrap(self, context, event):
ret, = retset = modal_func(self, context, event)
if ret in {'FINISHED'}:
print(f"{self.bl_idname} returned {ret}")
callback(ret)
return retset
return wrap

op._modal_org = op.modal
op.modal = modal_wrap(op.modal)


And an operator I tried it with :

import bpy

retval = None

def callback(ret):  # <- Modal wrapper calls this.
global retval
retval = ret
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.bake('INVOKE_DEFAULT', type="AO")

class BakteTexureOperator(bpy.types.Operator):
bl_idname = "object.bake_texture"
bl_label = "Bake Texure"

def invoke(self, context, event):
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.reveal()
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.uv.smart_project()
bpy.ops.uv.select_all(action='SELECT')
bpy.ops.uvpackmaster2.uv_pack()

print("after invoke")
wm = context.window_manager
return {'RUNNING_MODAL'}

def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
return {'CANCELLED'}

elif retval is not None:
return self.execute(context)

return {'PASS_THROUGH'}

def execute(self, context):
global retval
val = retval
retval = None  # Reset.

if 'CANCELLED' in val:
print("operator was cancelled")
return {'CANCELLED'}

else:
print("operator finished")
return {'FINISHED'}


To try it out just install the addon, select an image you want to bake to in the node editor and click the bake button in the bake testing panel.

It will unrwap and pack the uv's and the best thing is that you get a progress bar and you can cancel the baking. It's all comming from blender by running the bake operator as a modal.

The code for the full test bake addon can be found here :

Bake Testing

• I wish I was able to extract a generic solution from this for all other cases where things need to be waited to finish for. My python skills are not enough for that, I guess :( – Nikita Fuchs Feb 4 at 19:18