Skip to content

Commit

Permalink
Merge pull request #269 from tokejepsen/modal_mode
Browse files Browse the repository at this point in the history
Modal mode
  • Loading branch information
tokejepsen authored Nov 14, 2017
2 parents e5a2801 + 1fe94e7 commit a6e00d7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
7 changes: 5 additions & 2 deletions pyblish_qml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
)


def show(parent=None, targets=[]):
def show(parent=None, targets=None, modal=None):
from . import host
return host.show(parent, targets)

if targets is None:
targets = []
return host.show(parent, targets, modal)


_state = {}
Expand Down
58 changes: 38 additions & 20 deletions pyblish_qml/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def current_server():
return _state.get("currentServer")


def install():
def install(modal):
"""Perform first time install
Attributes:
Expand All @@ -55,7 +55,7 @@ def install():
uninstall()

install_callbacks()
install_host()
install_host(modal)

_state["installed"] = True

Expand All @@ -66,17 +66,21 @@ def uninstall():
sys.stdout.write("Pyblish QML shutdown successful.\n")


def show(parent=None, targets=[]):
def show(parent=None, targets=[], modal=None):
"""Attempt to show GUI
Requires install() to have been run first, and
a live instance of Pyblish QML in the background.
"""

# Get modal mode from environment
if modal is None:
modal = bool(os.environ.get("PYBLISH_QML_MODAL", False))

# Automatically install if not already installed.
if not _state.get("installed"):
install()
install(modal)

# Show existing GUI
if _state.get("currentServer"):
Expand Down Expand Up @@ -109,7 +113,7 @@ def on_shown():

try:
service = ipc.service.Service()
server = ipc.server.Server(service, targets=targets)
server = ipc.server.Server(service, targets=targets, modal=modal)
except Exception:
# If for some reason, the GUI fails to show.
traceback.print_exc()
Expand All @@ -124,8 +128,11 @@ def on_shown():
print("Success. QML server available as "
"pyblish_qml.api.current_server()")

server.listen()

return server


def publish():
# get existing GUI
if _state.get("currentServer"):
Expand All @@ -139,6 +146,7 @@ def publish():
# The running instance has already been closed.
_state.pop("currentServer")


def validate():
# get existing GUI
if _state.get("currentServer"):
Expand All @@ -152,6 +160,7 @@ def validate():
# The running instance has already been closed.
_state.pop("currentServer")


def install_callbacks():
pyblish.api.register_callback("instanceToggled", _toggle_instance)
pyblish.api.register_callback("pluginToggled", _toggle_plugin)
Expand Down Expand Up @@ -202,7 +211,7 @@ def register_pyqt5(path):
_state["pyqt5"] = path


def install_host():
def install_host(modal):
"""Install required components into supported hosts
An unsupported host will still run, but may encounter issues,
Expand All @@ -216,7 +225,7 @@ def install_host():
_install_hiero,
_install_nukestudio):
try:
install()
install(modal)
except ImportError:
pass
else:
Expand All @@ -240,7 +249,7 @@ def _on_application_quit():

def _connect_host_event(app):
"""Connect some event from host to QML
Host will connect following event to QML:
QEvent.Show -> rise QML
QEvent.Hide -> hide QML
Expand All @@ -250,8 +259,12 @@ def _connect_host_event(app):

class HostEventFilter(QtWidgets.QWidget):

eventList = [QtCore.QEvent.Show, QtCore.QEvent.Hide,
QtCore.QEvent.WindowActivate, QtCore.QEvent.WindowDeactivate]
eventList = [
QtCore.QEvent.Show,
QtCore.QEvent.Hide,
QtCore.QEvent.WindowActivate,
QtCore.QEvent.WindowDeactivate
]

def getServer(self):
server = None
Expand Down Expand Up @@ -316,7 +329,7 @@ def eventFilter(self, widget, event):
pass


def _install_maya():
def _install_maya(modal):
"""Helper function to Autodesk Maya support"""
from maya import utils

Expand All @@ -325,7 +338,8 @@ def threaded_wrapper(func, *args, **kwargs):
func, *args, **kwargs)

sys.stdout.write("Setting up Pyblish QML in Maya\n")
register_dispatch_wrapper(threaded_wrapper)
if not modal:
register_dispatch_wrapper(threaded_wrapper)

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
Expand All @@ -337,7 +351,7 @@ def threaded_wrapper(func, *args, **kwargs):
settings.WindowTitle = "Pyblish (Maya)"


def _install_houdini():
def _install_houdini(modal):
"""Helper function to SideFx Houdini support"""
import hdefereval

Expand All @@ -346,7 +360,8 @@ def threaded_wrapper(func, *args, **kwargs):
func, *args, **kwargs)

sys.stdout.write("Setting up Pyblish QML in Houdini\n")
register_dispatch_wrapper(threaded_wrapper)
if not modal:
register_dispatch_wrapper(threaded_wrapper)

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
Expand All @@ -358,7 +373,7 @@ def threaded_wrapper(func, *args, **kwargs):
settings.WindowTitle = "Pyblish (Houdini)"


def _install_nuke():
def _install_nuke(modal):
"""Helper function to The Foundry Nuke support"""
import nuke

Expand All @@ -370,7 +385,8 @@ def threaded_wrapper(func, *args, **kwargs):
func, args, kwargs)

sys.stdout.write("Setting up Pyblish QML in Nuke\n")
register_dispatch_wrapper(threaded_wrapper)
if not modal:
register_dispatch_wrapper(threaded_wrapper)

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
Expand All @@ -382,7 +398,7 @@ def threaded_wrapper(func, *args, **kwargs):
settings.WindowTitle = "Pyblish (Nuke)"


def _install_hiero():
def _install_hiero(modal):
"""Helper function to The Foundry Hiero support"""
import hiero
import nuke
Expand All @@ -395,7 +411,8 @@ def threaded_wrapper(func, *args, **kwargs):
func, args, kwargs)

sys.stdout.write("Setting up Pyblish QML in Hiero\n")
register_dispatch_wrapper(threaded_wrapper)
if not modal:
register_dispatch_wrapper(threaded_wrapper)

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
Expand All @@ -407,7 +424,7 @@ def threaded_wrapper(func, *args, **kwargs):
settings.WindowTitle = "Pyblish (Hiero)"


def _install_nukestudio():
def _install_nukestudio(modal):
"""Helper function to The Foundry Hiero support"""
import nuke

Expand All @@ -419,7 +436,8 @@ def threaded_wrapper(func, *args, **kwargs):
func, args, kwargs)

sys.stdout.write("Setting up Pyblish QML in NukeStudio\n")
register_dispatch_wrapper(threaded_wrapper)
if not modal:
register_dispatch_wrapper(threaded_wrapper)

app = QtWidgets.QApplication.instance()
app.aboutToQuit.connect(_on_application_quit)
Expand Down
20 changes: 15 additions & 5 deletions pyblish_qml/ipc/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ class Server(object):
"""

def __init__(self, service, python=None, pyqt5=None, targets=[]):
def __init__(self,
service,
python=None,
pyqt5=None,
targets=[],
modal=False):
super(Server, self).__init__()
self.service = service
self.listening = False

# Store modal state
self.modal = modal

# The server may be run within Maya or some other host,
# in which case we refer to it as running embedded.
is_embedded = os.path.split(sys.executable)[-1].lower() != "python.exe"
Expand Down Expand Up @@ -179,7 +187,6 @@ def __init__(self, service, python=None, pyqt5=None, targets=[]):
kwargs["args"].extend(targets)

self.popen = subprocess.Popen(**kwargs)
self.listen()

def stop(self):
try:
Expand Down Expand Up @@ -251,9 +258,12 @@ def _listen():
sys.stdout.write(line)

if not self.listening:
thread = threading.Thread(target=_listen)
thread.daemon = True
thread.start()
if self.modal:
_listen()
else:
thread = threading.Thread(target=_listen)
thread.daemon = True
thread.start()

self.listening = True

Expand Down
4 changes: 2 additions & 2 deletions pyblish_qml/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

VERSION_MAJOR = 1
VERSION_MINOR = 4
VERSION_PATCH = 5
VERSION_MINOR = 5
VERSION_PATCH = 0

version_info = (VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
version = '%i.%i.%i' % version_info
Expand Down

0 comments on commit a6e00d7

Please sign in to comment.