I have a modal operator that runs in the 3d viewport. Its basically the raytracer from the template. It can be canceled with escape.

But I have several other operators which can be executed from panels in the sidebar. This leads to errors if I havent remembered to cancel the modal operator manually.

Can I stop the modal operator automatically from the other operators at the start of the execute method?

Can I remove the modal handle? Or is that bad practice?

This is exactly the same question, but quite old and unresolved. Maybe there is a way now? https://blenderartists.org/t/immediately-cancel-running-modal-operators-from-outside-instance/615499/4

This post addresses the same issue too. Doesn't seem promising, though :-( https://devtalk.blender.org/t/is-there-any-way-to-detect-an-active-modal-operation/7184/4

EDIT: Is there a way to identify from the context or event in the modal method, where the mouse clicked? I guess that there must be some difference whether the viewport was clicked or the sidebar or a different region or space or something. Then I can stop the modal operator internally if I tried to do something I wasn't supposed to do.

  • $\begingroup$ You need an access to edit all operators for that. Set two internal BoolPropertys. The first one should be checked on every loop start of the modal operator and the operator should quit when finds it enabled (other operators should enable it on their start). Another one is for confirming that the modal operator has stopped. It should be enabled in the __del__ method of the modal operator while the first one - disabled back. When other operators find confirmation Property enabled, they disable it back and continue their work. The only thing is all operators have to be modal in this case. $\endgroup$ – Andrey Sokolov Jan 13 at 9:56
  • $\begingroup$ Also this thread may be helpful. $\endgroup$ – Andrey Sokolov Jan 13 at 10:00
  • $\begingroup$ Thanks. I did see the thread and it seems pretty advanced for me. All my other operators are execute, so it would take a lot of work to convert it all. But this means that I start them all from buttons in the 3dview sidebar panel. Is there a way to detect a click here from inside the modal operator and then cancel it internally? $\endgroup$ – DrDress Jan 13 at 10:06

Your Answer

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

Browse other questions tagged or ask your own question.