Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created August 1, 2019 10:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save BigRoy/df3f063b8f7c8e4f307254e69278ac6e to your computer and use it in GitHub Desktop.
Save BigRoy/df3f063b8f7c8e4f307254e69278ac6e to your computer and use it in GitHub Desktop.
Simple example of running a Qt interface in Blender 2.8 without blocking Blender.
import bpy
from PyQt5 import QtWidgets
class QtModalOperator(bpy.types.Operator):
"""A base class for Operators that run a Qt interface."""
def modal(self, context, event):
if self._app:
self._app.processEvents()
return {'PASS_THROUGH'}
return {"FINISHED"}
def execute(self, context):
"""Execute the Operator.
The child class must implement execute() and call super to trigger this
class' execute() at the beginning. The execute() method must finally
return {'RUNNING_MODAL"}
Note that the Qt code should *not* call QApplication.exec_() as it
seems that magically the Qt application already processes straight
away in Blender. Maybe due to:
https://stackoverflow.com/questions/28060218/where-is-pyqt-event
-loop-running
"""
self._app = QtWidgets.QApplication.instance()
if not self._app:
self._app = QtWidgets.QApplication(["blender"])
class QtTestOperator(QtModalOperator):
"""Launch Avalon Creator.."""
bl_idname = "object.qt_test"
bl_label = "Test Qt UI.."
def execute(self, context):
# Initialize Qt operator execution
super(QtTestOperator, self).execute(context)
global widget
widget = QtWidgets.QPushButton("Hello")
widget.show()
return {'RUNNING_MODAL'}
bpy.utils.register_class(QtTestOperator)
bpy.ops.object.qt_test()
@BigRoy
Copy link
Author

BigRoy commented Aug 1, 2019

Another test snippet by Jasper van Nieuwenhuizen: https://gitlab.com/snippets/1881226
See discussion here.

@jasperges
Copy link

A note about this: the modal operator only finishes when self._app doesn't exist anymore. That will most likely only be the case when Blender quits. So it would be better to check widget.isVisible() and return {'FINISHED'} when it's not. Else the modal operator keeps running which at least prevents auto saves (and probably more unwanted behavior).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment