Skip to content

Instantly share code, notes, and snippets.

@oglops
Forked from mottosso/README.md
Created July 8, 2016 21:08
Show Gist options
  • Save oglops/c90b131d12675655f11635cfcf4011ec to your computer and use it in GitHub Desktop.
Save oglops/c90b131d12675655f11635cfcf4011ec 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.

from PySide import QtCore, QtGui
class RestoreScriptEditorFocus(QtCore.QObject):
def __init__(self):
super(RestoreScriptEditorFocus, self).__init__()
QtGui.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 QtGui.qApp.topLevelWidgets() if w.objectName() == "scriptEditorPanel1Window")
script_editor.activateWindow()
return True
except StopIteration:
# Perhaps the script editor was docked
pass
return super(RestoreScriptEditorFocus, self).eventFilter(obj, event)
f = RestoreScriptEditorFocus()
QtGui.qApp.installEventFilter(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment