john john - 1 year ago 68
Python Question

Maya Pymel: pass fileDialog2's return to a UI textfield

I'm having trouble understanding how to use fileDialog2's "optionsUICommit" flag. When the user hit's "save" in the file dialog box, I want to run the command

. But from the help file, it seems to want me to use a MEL command.

MEL only. The string is interpreted as a MEL callback, to be called
when the dialog is successfully dismissed. It will not be called if
the user cancels the dialog, or closes the window using window title
bar controls or other window system means. The callback is of the
form: global proc MyCustomOptionsUICommit(string $parent)

The parent argument is the parent layout into which controls have been
added using the optionsUICreate flag

This seems...complicated.

Here's my code.

import pymel.core as pm

def on_save_dialog_file(myDialog):
print "Hello from file_dialog_save_file()!"

def file_dialog_save_file():
myDialog = pm.fileDialog2(ocm="on_save_dialog_file", fm=0, ff="Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)", dialogStyle=2)
print myDialog


Even trying as a weird Mel->Python command didn't work.
ocm="python \"on_save_dialog_file()\";"

Is there an easier/more straightforward way to run a command after setting a save file in the dialog?

Answer Source

You can just do it like this, without the callback. Anything other than a user selection will return None

c = cmds.fileDialog2(fm=0, ff="Maya Files (*.ma *.mb);;Maya ASCII (*.ma);;Maya Binary (*.mb);;All Files (*.*)", dialogStyle=2)
if c:
    print c
    print "user cancelled"