return value

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Search this Thread Display Modes
  03 March 2013
return value


I built a little class that builds a window with a textScrollList and two buttons (ok and cancel).
I would like to use this to let the user chose the value of a variable during another procedure, so this window should pop up and then return a value that is used outside the class.
I am still quite new to Classes and probably I am missing something, could someone help me on this?

class NamespaceChooser:
    def __init__(self, *args):

        spacer = 16 = "NamespaceChooser"
        self.title = "Chose namespace"

        # Begin creating the UI
        if (mc.window(, q=1, exists=1)): mc.deleteUI(
        self.window = mc.window(, title=self.title)

        self.form = mc.formLayout()
        self.textScroll = mc.textScrollList( numberOfRows=16, allowMultiSelection=False, append=['cat', 'dog', 'bee', 'horse'])
        self.okButton = mc.button(label='Ok', width=142, command=self.okButton)
        self.cancelButton = mc.button(label='Cancel', width=142, command=self.cancelButton)
        mc.formLayout(  self.form,
                                    (self.textScroll, 'top', spacer),
                                    (self.textScroll, 'left', spacer),
                                    (self.textScroll, 'right', spacer),
                                    (self.cancelButton, 'top', spacer, self.textScroll),
                                    (self.okButton, 'top', spacer, self.textScroll),
                                    (self.okButton, 'left', spacer, self.cancelButton)

        mc.window(self.window, e=1, w=300, h=280)
    def okButton(self, *args):
        myNamespace = mc.textScrollList(self.textScroll, q=True, si=True)
        print myNamespace
        return myNamespace
    def cancelButton(self, *args):
        print False
  03 March 2013
I think you need to use something like one of the modal dialogs so that it forces your calling process to wait for a response. (Sorry if I misunderstood your question). Your class window that you showed above, will sit there, and you could query the last button clicked in various ways, but your process will not wait while you click something.

Within standard maya ui commands you could use the layoutDialog. You'll need to look it up, but it goes something like this

import pymel.core as pm
 from functools import partial
 def my_process():
 	# prompt for something
 	returnCode = pm.layoutDialog(ui=partial(my_dialog, my_arg))
 	# now do something with the return code
 def my_dialog(my_arg):
 	# here is where you can create your layout and buttons
 	# This could be your class
 	parent = pm.setParent(q=True)
       # etc, etc...
       # and somewhere you need a button to dismiss the dialog
 	BTN = pm.button(p=parent)
 	BTN.setCommand(partial(my_dismiss_cmd, 'hello'))
 def my_dismiss_cmd(returnString):

Anyway thats the gist of it. Sorry about the pymel... cmds is probably similar, but I never use it.

  03 March 2013
Thanks David,

You are completely right. I was looking for a modal dialog
I was a little triky to make it work but now seems ok!

Thanks again.

Last edited by nookie : 03 March 2013 at 01:19 PM.
  03 March 2013
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 01:21 PM.

Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.