Skip to content

Instantly share code, notes, and snippets.

@martin-chatterjee
Forked from mottosso/README.md
Last active September 1, 2016 14:31
Show Gist options
  • Save martin-chatterjee/7759bdc0a877424542afc7bc1db06b07 to your computer and use it in GitHub Desktop.
Save martin-chatterjee/7759bdc0a877424542afc7bc1db06b07 to your computer and use it in GitHub Desktop.
Restore Script Editor focus

Problem

image

On Windows systems using Autodesk Maya, the text input field of the Script Editor doesn't regain focus after having restored focus to the main window. This event handler explicitly restores focus, if it turns out to have been the last active panel at the time of leaving the application.

Usage

Place the full contents of the script below into your userSetup.py and never again lose focus.

Tested on Maya 2013-2016+ and is safe to use on other platforms where it simply does nothing.

# work around the PySide/PySide2 mess ------------------
try:
from shiboken2 import wrapInstance
from PySide2 import QtCore, QtGui, QtWidgets
except ImportError:
from shiboken import wrapInstance
from PySide import QtCore, QtGui
import PySide.QtGui as QtWidgets
# ------------------------------------------------------
class RestoreScriptEditorFocus(QtCore.QObject):
def __init__(self):
super(RestoreScriptEditorFocus, self).__init__()
QtWidgets.qApp.focusChanged.connect(self.on_focuschanged)
self.restore = False
def on_focuschanged(self, old, new):
self.restore = "cmdScrollFieldExecuter" in old.objectName() if old else False
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.ApplicationActivate and self.restore:
try:
script_editor = next(w for w in QtWidgets.qApp.topLevelWidgets() if w.objectName() == "scriptEditorPanel1Window" or w.accessibleName() == "Script Editor")
script_editor.activateWindow()
return True
except StopIteration:
# Perhaps the script editor was docked
pass
return super(RestoreScriptEditorFocus, self).eventFilter(obj, event)
f = RestoreScriptEditorFocus()
QtWidgets.qApp.installEventFilter(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment