Re: [Maya-Python] parent ScriptJobs to a PyQT GUI in Maya

677 views
Skip to first unread message

John Patrick

unread,
Apr 20, 2011, 2:00:43 PM4/20/11
to python_in...@googlegroups.com
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 <spat...@bluecastlegames.com> 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
jspa...@gmail.com
http://www.canyourigit.com

Shawn Patapoff

unread,
Apr 20, 2011, 2:01:59 PM4/20/11
to python_in...@googlegroups.com
Hey John,

That's a good idea, I think I'll look into that. Seems way cleaner.
Thanks.


Shawn Patapoff

unread,
Apr 20, 2011, 1:40:53 PM4/20/11
to python_inside_maya

John Patrick

unread,
Apr 20, 2011, 2:54:37 PM4/20/11
to python_in...@googlegroups.com
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()

Shawn Patapoff

unread,
Apr 20, 2011, 5:03:19 PM4/20/11
to python_in...@googlegroups.com
Hey John,

Thanks a bunch for the recipe. Don't 100% understand it, but I'll look over it a bit more to get a better grasp. Pretty much just hacked it into what I was doing and all it working great.

Thanks again,
Shawn


Reply all
Reply to author
Forward
0 new messages