One thing I would worry about with that method is what happens in the case
of an exception somewhere in the UI's __init__ after creating the
scriptJob.  This is overkill in this case (try/except would probably be just
fine), but I'm a fan of contextManagers for this type of 'i really need to
make sure something happens' behavior.  Maybe something like this:

from __future__ import with_statement
import maya.cmds as MC
from PyQt4 import QtGui, QtCore

class ScriptJobCreator(object):
    """A context manager for creating script jobs"""
    def __init__(self):
        self.id  = None

    def createJob(self, *args, **kwargs):
        self.id = MC.scriptJob(*args, **kwargs)
        return self.id

    def killJob(self):
        if self.id:
            MC.scriptJob(kill=self.id)
            self.id = None

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type is not None:
            self.killJob()


class DummyDialog(QtGui.QDialog):

      def __init__(self, parent=None):
          super(DummyDialog, self).__init__(parent=parent)

          with ScriptJobCreator() as sjc:
              self.id = sjc.createJob(e=['SelectionChanged', self.updateUI])

              #THISWILLHOSEIT if uncommented.  Give it a shot.

              layout = QtGui.QGridLayout()
              dummyObj = QtGui.QPushButton("Dummy")
              layout.addWidget(dummyObj,0,0)
              self.setLayout(layout)




      def updateUI(self):
          print "I'm Updaing!"

      def closeEvent(self, e):
          MC.scriptJob(kill=self.id)
          super(DummyDialog, self).closeEvent(e)


def initUI():
    dialog = DummyDialog()
    dialog.show()

initUI()


On Wed, Apr 20, 2011 at 11:01 AM, Shawn Patapoff <
[email protected]> wrote:

> Hey John,
>
> That's a good idea, I think I'll look into that. Seems way cleaner.
> Thanks.
>
> On Wed, Apr 20, 2011 at 11:00 AM, John Patrick <[email protected]>wrote:
>
>> Could you override the closeEvent in whatever QWidget destroys the UI, and
>> have it kill the scriptJob instead?
>>
>>
>> On Wed, Apr 20, 2011 at 10:40 AM, Shawn Patapoff <
>> [email protected]> wrote:
>>
>>> Hey,
>>>
>>> Has anyone found any other way to parent a scriptJob to a PyQt created
>>> GUI in maya without having to attach a maya created dummy object?
>>>
>>> This is what we're doing:
>>> dummyObj = cmds.helpLine(visible=False, width=100,
>>> parent='mainVerticalLayout')
>>> cmds.scriptJob( e=['SelectionChanged', self.updateUI],
>>> parent=dummyObj )
>>>
>>> mainVerticalLayout being the PyQt object. If I parent the scriptJob
>>> directly to mainVerticalLayout maya doesn't seem to see it.
>>>
>>> Cheers,
>>> Shawn
>>>
>>> --
>>> http://groups.google.com/group/python_inside_maya
>>>
>>
>>
>>
>> --
>> John Patrick
>> 404-242-2675
>> [email protected]
>> http://www.canyourigit.com
>>
>>  --
>> http://groups.google.com/group/python_inside_maya
>>
>
>  --
> http://groups.google.com/group/python_inside_maya
>



-- 
John Patrick
404-242-2675
[email protected]
http://www.canyourigit.com

-- 
http://groups.google.com/group/python_inside_maya

Reply via email to