PDA

View Full Version : You are subscribed to this thread what's the common practice for refreshing GUI


whisperwing
03-09-2011, 01:09 AM
say, I have a node and a GUI that display/update information on this node. The GUI will certainly make it easy for the users to modify the node, but user can still do setAttr to the node in scripts. Right now I'm doing the most inexpensive thing, which is a refresh button that will force an update to the GUI no matter attributes on the node have changed or not. What if I want to only update when necessary, is scriptJob the only option here?

pritishd
03-09-2011, 03:59 AM
What if I want to only update when necessary, is scriptJob the only option here?

That's how I implemented the scriptJob. Remember to delete it if the window closes.

NaughtyNathan
03-09-2011, 09:14 AM
I wouldn't like to confidently state that scriptJob is the ONLY solution possible in this case, but this is basically the primary purpose of scriptJobs... why look for a different option when you have the ideal* solution in a scriptJob?
Having said that, there is also attrControlGrp though if you are only talking about modifying standard attributes on your node. If your node only has basic bool, int based and float(3) based attributes and you built your UI using attrControlGrp you may not need any scriptJobs.

(btw, never remember to delete scriptJobs when you close a window, just -parent the job to the UI and forget about it.)

:nathaN

* as "ideal" as anything ever is in Maya.. ;)

whisperwing
03-09-2011, 05:40 PM
Thanks everyone. Wasn't able to reply to the post for a while, sorry for the email Nathan !

So this is my followup question on using the scriptJob. I use pyqt for the GUI and I'm doing something like this:

from PyQt4 import QtCore, QtGui

dlg = QtGui.QDialog()
dlg.setWindowTitle("testing scriptJob")
dlg.show()

# a simple function
def foo():
print "scriptJob executing!!!"

# create a scriptJob that
jobId =
MayaCmds.scriptJob(attributeChange=['pCube1.ty', foo], parent="dlg")


And I got

# Error: Could not find UI element named "dlg"
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# RuntimeError: Could not find UI element named "dlg" #

If I change it to "parent=dlg", it will give the error that

# Error: Could not find UI element named "<PyQt4.QtGui.QDialog object at 0x327e050>" # Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# RuntimeError: Could not find UI element named "<PyQt4.QtGui.QDialog object at 0x327e050>" #

So what is the right way to specify this UI element to this scriptJob?
Thanks !

uiron
03-10-2011, 06:25 AM
instead of parent="dlg", try parent=dlg. you need value of the variable. although i don't know if scriptjob accepts qt windows as parents, so this might error a bit as well.
you will also need a mel procedure to handle script job, python is not accepted.

whisperwing
03-10-2011, 07:03 AM
well I tried parent = dlg, I guess it didn't work because it couldn't recognize the python object given.

Using Mel procedure probably won't work for me since everything is written in pyqt and if scriptJob doesn't accept python function, I'll need to define a MEL procedure that does a python call to functions from the dialog. And to do that I need access to the dialog as a global variable in the current Maya session I guess?

OK I'm so confused, please, anyone could explain the workflow to me ? Really appreciated !

instead of parent="dlg", try parent=dlg. you need value of the variable. although i don't know if scriptjob accepts qt windows as parents, so this might error a bit as well.
you will also need a mel procedure to handle script job, python is not accepted.

uiron
03-10-2011, 08:03 AM
if scriptJob doesn't accept python function, I'll need to define a MEL procedure that does a python call to functions from the dialog. And to do that I need access to the dialog as a global variable in the current Maya session I guess?

pretty much that. except from my code where i'm doing just that. it accepts python procedure as an argument, and returns string, representing a mel procedure. you would then have to write "MayaCmds.scriptJob(attributeChange=['pCube1.ty', createMelProcedure(foo)])" and it should be fine.


def createMelProcedure(pythonMethod,args=[]):
'''
creates a valid mel procedure to be called that invokes pythonMethod
two procedures with a temporary name are created in global MEL and Python spaces for this reason
'''

# generate procedure name
procName = "ngSkinToolsProc%i%s" % (id(pythonMethod),pythonMethod.__name__)

# create link to method in global python space
sys.modules['__main__'].__dict__[procName] = pythonMethod

# create mel procedure
melArgs = ",".join(map(lambda a:"%s $%s" % (a[0],a[1]),args))
pythonArgs = ','.join(map(lambda a: '\'"+$%s+"\'' %a[1],args))
melCode = 'global proc %s(%s) { python ("%s(%s)"); }' % (procName,melArgs,procName,pythonArgs)
mel.eval(melCode)

return procName

whisperwing
03-16-2011, 08:04 AM
Actually I was able to use a python function directly with scriptJob, at least when I tried in Maya2011.

And some Maya expert suggested a way to solve my other question, namely, how to refer to a PyQT GUI in a scriptJob.

Here's the example:


# I think a good approach would be to create a normal maya window at the top-level,
# then wrap it in PyQt and continue as normal using PyQt to build the UI.
#
import sip
from maya import cmds as MayaCmds
import maya.OpenMayaUI as OpenMayaUI
from PyQt4 import QtCore, QtGui

winname = MayaCmds.window('testWin')
ptr = long(long(OpenMayaUI.MQtUtil.findWindow(winname)))
dlg = sip.wrapinstance(ptr, QtGui.QDialog)
lay = QtGui.QVBoxLayout()
lay.addWidget(QtGui.QPushButton("pyqt"))
dlg.setLayout(lay)
dlg.show()

def foo():
print "hello world"

MayaCmds.polyCube()
jobId = MayaCmds.scriptJob(attributeChange=['pCube1.ty', foo], parent=winname)
MayaCmds.scriptJob(exists=jobId)

uiron
03-16-2011, 09:32 AM
jobId = MayaCmds.scriptJob(attributeChange=['pCube1.ty', foo], parent=winname)


i don't know how i overlooked this, but apparently yes, python callable works as a script job handler, and even on 2009:]

CGTalk Moderation
03-16-2011, 09:32 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.