Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Forked from BigRoy/blender28_run_qt_ui.py
Created December 11, 2020 13:01
Show Gist options
  • Save stephensmitchell/5fd71e6324bff103ae8df33ee2832140 to your computer and use it in GitHub Desktop.
Save stephensmitchell/5fd71e6324bff103ae8df33ee2832140 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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment