3
$\begingroup$

In my addon, I have a button that launches me an operator, and I want the same button gives me another operator (modal operator). It is possible to launch two operators with the same button? My modal operator :

def modal(self, context, event):
    if event.type == 'RIGHTMOUSE':
       ...code...
    return {'PASS_THROUGH'}

def execute(self, context):
    wm = context.window_manager
    wm.modal_handler_add(self)
    return {'RUNNING_MODAL'}      
$\endgroup$
4
$\begingroup$

Check Text Editor Templates: Operator Simple and Operator Modal

Imagine the modal operator has bl_idname = "object.some_generic_modal". In the execute function of your other (non modal) operator you could make a direct operator call:

def execute(self, context):
    # your stuff

    # call the other operator
    bpy.ops.object.some_generic_modal()

    return {'FINISHED'}

However, to avoid the separation and extra boilerplate, you might want to stick your one-off code in the invoke of the modal operator and call that directly instead.

To clarify: with the term one-off code I meant the code which just needs to be executed once, prior to the continuous modal operator.

$\endgroup$
  • $\begingroup$ Thank you, it works. For cons, I did not understand well understood the second method : stick one-off code in the invoke … Here is my modal operator: def modal(self, context, event): if event.type == 'RIGHTMOUSE': ..... my code ..... return {'PASS_THROUGH'} def execute(self, context): wm = context.window_manager wm.modal_handler_add(self) return {'RUNNING_MODAL'} $\endgroup$ – Logiquefloue Feb 17 '15 at 9:32
  • $\begingroup$ please edit your own original post and add your code to clarify it. Code fragments aren't formatted nicely in answer-comment section. $\endgroup$ – zeffii Feb 17 '15 at 9:57
  • $\begingroup$ OK, at this moment you seem to have two operators, (one calls the other). Consider using just one, the modal operator, it can execute code before going into modal mode $\endgroup$ – zeffii Feb 17 '15 at 10:09
  • $\begingroup$ OK, now I understand better the "Modal Operator." Now I think this is not the right operator I need. I want to start an update operator when you select an object (such as the fact Blender to update the names of objects). So I need to detect the mouse click and call operator MyUpdate (self). You think I have to go through a modal operator? $\endgroup$ – Logiquefloue Feb 18 '15 at 8:56
  • $\begingroup$ @Logiquefloue with your new understanding I suggest you start a new question specifying in more detail what you want to be able to do (the bigger picture of the addon), if it can be done then people will suggest strategies or example code. But modal operator sounds reasonable, it will stay alive until ended and it is intended to receive events like keyboard and mouse. $\endgroup$ – zeffii Feb 18 '15 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.