Linking Maya scriptJobs to Qt widgets

The scriptJob command has a ‘parent’ attribute that links the scriptJob lifetime to the UI lifetime, this is great when building UI that needs to e.g. monitor selection changes.

With the introduction of Qt based interfaces this gained some abmiguity, the parent needs to have a name, and the widget lifetime needs to be managed by maya. Here is a very minimal example of the steps required:

Give the Qt widget a name
I use uuid because name clashes are detrimental.

Actually delete the UI when the widget is closed.
This will actually delete the qt object and clean up nicely, I was surprised this wasn’t default behaviour.

Now when using a custom widget, of course it is possible to implement show & hide events to connect and suspend script jobs manaully, but this seems more integrated and allows hooking scriptjobs to parent dialogs instead of whichever custom widget inside.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import uuid
from maya import cmds
from PySide2.QtWidgets import *
def cb():
    print cmds.ls(sl=True)
d = QDialog()
guid = uuid.uuid4()
d.setObjectName(str(guid))
d.show()
d.closeEvent = lambda event: cmds.deleteUI(d.objectName())
id = cmds.scriptJob(e=('SelectionChanged', cb), parent=d.objectName())

Leave a Reply

Your email address will not be published. Required fields are marked *