Re: [Maya-Python] Can a script wait for a custom prompt UI?

1,207 views
Skip to first unread message

Justin Israel

unread,
Nov 2, 2012, 6:02:49 PM11/2/12
to python_in...@googlegroups.com
There are two ways you can go about this, depending on how you see this fitting into your actual workflow.
  1. create() shows a modal dialog that blocks until the user responds, and then the program can continue and the value can be checked
  2. create() shows a non-modal dialog that uses callbacks (similar to what you have now), but the calling script needs to provide the callback that should be run when the value is ready. This means the calling script needs to just stop and leave the function after it shows the window, and wait to do something else by having its callback called.
I will just show the easier one, doing a modal dialog. Your custom UI example looks exactly like a stock Confirm dialog, so I am going to replace it with that, because it is one of Maya's blocking UI's


========
class UserQueryUI(object):

    def __init__(self, query, affirmativeReply, negativeReply):
        self.name = 'userQueryUI'
        self.title = 'userQueryUI'
        self.query = query
        self.affirmativeReply = affirmativeReply
        self.negativeReply = negativeReply
        
        self.reply = None
    
    def create(self):
        print '--Creating userQueryUI with query', self.query
        
        yes = self.affirmativeReply
        no = self.negativeReply
        
        ret = cmds.confirmDialog( 
            title = self.title, 
            message = self.query, 
            button = [yes, no], 
            defaultButton = yes, 
            cancelButton = no, 
            dismissString = no )
        
        print "Reply was", ret
        
        self.reply = (ret == yes)
        
        return self.reply
========

You no longer need those callbacks inside the create() method, and I would also recommend making "reply" an instance attribute instead of a class attribute. Your callbacks before were not actually setting the class attribute. They were just creating a local variable that is garbage collected. 

If you need it to be even more custom, you would have to use layoutDialog to have it show the custom UI code, but still block until it is dismissed.

-- justin



On Tue, Oct 30, 2012 at 1:53 PM, Chad Fox <chad...@skyboxdigital.com> wrote:
Hello

I would like to create a ubiquitous gui class called "UserQueryUI". The idea is that this will be a go-to user prompt for any script with a question or announcement to the user and their choice of an affirmative, negative or "cancel op" reply . What I am currently trying to work out is how I can call this class in any script and ‘wait’ for an answer.

My snag is that when I call the class and it's .create method, the script doesn't stop and wait for the user to reply before moving on resulting in the script/tool completing before the user even replies.. 

This approach was a shot in the dark so let me know if what I ask is not possible or if there is a better approach, but if there is a solution, I would love to learn about it.

Cheers

#example script and user query class
import maya.cmds as cmds

#External Script
def testFunction():
    q=UserQueryUI('Replace with query text' ,'affirmativeButtonTxt','negativeButtonTxt')
    q.create()
    
    #Script needs to wait for a reply before calling the reply from the class.
    print 'Users reply', q.reply


#UserQueryUI Class

class UserQueryUI:

    reply = None
    
    def __init__(self, query ,affirmativeReply,negativeReply):

        self.name = 'userQueryUI'
        self.title = 'userQueryUI'
        self.query = query
        self.affirmativeReply = affirmativeReply
        self.negativeReply = negativeReply
    
    def create(self):
    
        def affirmative(x):
            print 'Positive reply'
            reply=True
            cmds.deleteUI('userQueryUI',window=True )
    
        def negative(x):
            print 'Negative reply'
            reply=False
            cmds.deleteUI('userQueryUI',window=True )
    
    
        def cancelTool(x):
            print 'Cancel reply'
            reply=None
            cmds.deleteUI('userQueryUI',window=True )


        print '--Creating userQueryUI with query',self.query
        
        if cmds.window(self.name, exists=True):
            cmds.deleteUI(self.name)
            
        self.window = cmds.window(self.name, title=self.title)
        self.form = cmds.formLayout()

        self.queryText = cmds.text(self.query, w=400, align='left' )
        height = 1
            
        cmds.formLayout(self.form, edit=True, attachForm=[(self.queryText, 'top', 5),(self.queryText,'left',5)])

        self.affirmativeButton = cmds.button(l=self.affirmativeReply, al='center', w=100,h=25, c = affirmative)
        self.negativeButton = cmds.button(l=self.negativeReply, al='center', w=100,h=25, c = negative)
        self.cancelButton = cmds.button(l='Cancel Tool', al='center', w=100,h=25, c = cancelTool)

        cmds.formLayout(self.form, edit=True, attachForm=[
                                                                    (self.affirmativeButton, 'top', height+50),
                                                                    (self.affirmativeButton,'left',50),
                                                                    (self.negativeButton, 'top', height+50),
                                                                    (self.negativeButton,'left',150),
                                                                    (self.cancelButton, 'top', height+50),
                                                                    (self.cancelButton,'left',250)
                                                                    ])

        cmds.showWindow(self.window)
        cmds.window(self.window, edit=True, s=True, wh=(400,height+80))
        
testFunction()

--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings: http://groups.google.com/group/python_inside_maya/subscribe

Chad Fox

unread,
Nov 7, 2012, 3:41:08 PM11/7/12
to python_in...@googlegroups.com
Thank Justin. 

I need to pass data along with the prompt so the confirmDialog() is out, but you gave me the information I needed to find the answer I was looking for. I searched for modal dialog and found the modal layoutDialog() within maya's own commands. This function does exactly what I need.

Cheers
Reply all
Reply to author
Forward
0 new messages