Hi Eric, glad you liked it.

Thanks for posting your progress! With that in mind, I gave it another go
and came up with this.

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:
            script_editor = next(w for w in
QtGui.qApp.topLevelWidgets() if w.objectName() ==
"scriptEditorPanel1Window")
            script_editor.activateWindow()
            return True
        else:
            return super(RestoreScriptEditorFocus, self).eventFilter(obj, event)

f = RestoreScriptEditorFocus()
QtGui.qApp.installEventFilter(f)

In a nutshell, it listens for any change of focus, and records whether or
not the change was from the input field of the script editor, apparently
called something like “cmdScrollFieldExecuter”. Not sure how consistent
this is across versions of Maya.
​

On 20 October 2015 at 22:04, eRiC Werner <[email protected]> wrote:

> @Marcus Ottosson: Awesome!!!
>
> I was trying to hack a version that remembers if last window was actually
> the scriptEditor. Using QtCore.QEvent.ApplicationDeactivate you easily
> get when you leave Maya. But getting the last window seems tricky!
>
> QtGui.qApp.activeWindow() does not get you anything at that point.
> QtGui.qApp.topLevelWidgets() is not sorted to have topmost window last or
> something.
> cmds.getPanel(withFocus=True) is only sometimes giving you the
> 'scriptEditorPanel1' :( [wtf?] and
> cmds.getPanel(underPointer=True) is kind of unreliable :/
>
> my playground so far: http://pastebin.com/ibq7ZBVD
>
> I'll try If I'm more happy with brute force scriptEditorPanel activation
> like that already :]
> thanks a bunch!!!
> ëRiC
>
>
> On Friday, October 16, 2015 at 1:29:53 PM UTC+2, Marcus Ottosson wrote:
>>
>> Intrigued by the possibility eliminating this minor annoyance, I followed
>> Justin’s suggestion and wrote a solution.
>>
>> Run this, or put it in your userSetup.py, and it will restore focus to
>> the Script Editor, if it is available.
>>
>> from PySide import QtCore, QtGui
>> class RestoreScriptEditorFocus(QtCore.QObject):
>>     def eventFilter(self, obj, event):
>>         if event.type() != QtCore.QEvent.ApplicationActivate:
>>             return super(RestoreScriptEditorFocus, self).eventFilter(obj, 
>> event)
>>
>>         script_editor = next(w for w in QtGui.qApp.topLevelWidgets() if 
>> w.objectName() == "scriptEditorPanel1Window")
>>         script_editor.activateWindow()
>>         return True
>>
>> f = RestoreScriptEditorFocus()
>> QtGui.qApp.installEventFilter(f)
>>
>> It’ll also “restore” focus to it, even if it wasn’t the last thing to
>> have focus at the time of leaving the Maya window. Whether that’s a problem
>> or not is too soon to tell, but I’ll leave the implementation for that to
>> the reader.
>>
>> Enjoy!
>> ​
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/ff5b65a7-ef3d-4df8-885b-c150872baa24%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/ff5b65a7-ef3d-4df8-885b-c150872baa24%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>



-- 
*Marcus Ottosson*
[email protected]

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOBcoe5xj0gYSmtXBphOe-kyS2n4xgFGXrVeuRHBNbuX2A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
    • Re: [May... yury nedelin
      • Re: ... Chris Lesage
        • ... Marcus Ottosson
          • ... yury nedelin
            • ... Marcus Ottosson
              • ... yury nedelin
              • ... Justin Israel
              • ... Joe Weidenbach
              • ... Marcus Ottosson
              • ... eRiC Werner
              • ... Marcus Ottosson
              • ... Marcus Ottosson
              • ... Marcus Ottosson
              • ... eRiC Werner
              • ... 'Martin Chatterjee' via Python Programming for Autodesk Maya
              • ... Marcus Ottosson
              • ... 'Martin Chatterjee' via Python Programming for Autodesk Maya
              • ... Marcus Ottosson
  • [Maya-Python]... ctjanney

Reply via email to