-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcore.py
80 lines (61 loc) · 2.36 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sys
import os
import logging
import bpy
qt_binding = os.environ.get('QT_PREFERRED_BINDING') # correct qt bindings
if qt_binding:
if qt_binding == 'PySide2':
from PySide2 import QtWidgets, QtCore
if qt_binding == 'PyQt5':
from PyQt5 import QtWidgets, QtCore
else:
from PySide2 import QtWidgets, QtCore
logger = logging.getLogger('qtutils')
class QtWindowEventLoop(bpy.types.Operator):
"""Allows PyQt or PySide to run inside Blender"""
bl_idname = 'screen.qt_event_loop'
bl_label = 'Qt Event Loop'
def __init__(self, widget, *args, **kwargs):
self._widget = widget
self._args = args
self._kwargs = kwargs
def modal(self, context, event):
# bpy.context.window_manager
wm = context.window_manager
if not self.widget.isVisible():
# if widget is closed
logger.debug('finish modal operator')
wm.event_timer_remove(self._timer)
return {'FINISHED'}
else:
logger.debug('process the events for Qt window')
self.event_loop.processEvents()
self.app.sendPostedEvents(None, 0)
return {'PASS_THROUGH'}
def execute(self, context):
logger.debug('execute operator')
self.app = QtWidgets.QApplication.instance()
# instance() gives the possibility to have multiple windows
# and close it one by one
if not self.app:
# create the first instance
self.app = QtWidgets.QApplication(sys.argv)
if 'stylesheet' in self._kwargs:
stylesheet = self._kwargs['stylesheet']
self.set_stylesheet(self.app, stylesheet)
self.event_loop = QtCore.QEventLoop()
self.widget = self._widget(*self._args, **self._kwargs)
logger.debug(self.app)
logger.debug(self.widget)
# run modal
wm = context.window_manager
self._timer = wm.event_timer_add(1 / 120, window=context.window)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def set_stylesheet(self, app, filepath):
file_qss = QtCore.QFile(filepath)
if file_qss.exists():
file_qss.open(QtCore.QFile.ReadOnly)
stylesheet = QtCore.QTextStream(file_qss).readAll()
app.setStyleSheet(stylesheet)
file_qss.close()