Skip to content

ExamplesMayaFileDialogs

Ben Toogood edited this page Jun 13, 2013 · 6 revisions

Introduction

In Cortex, the IECoreMaya.FileDialog class provides an alternative to maya.cmds.fileDialog. More details here:

IECoreMaya.FileDialog

IECoreMaya.FileBrowser

Here's a really quick example of how to pop up a dialog box from the right-click Parameter menu, that asks the user where to save the value for the parameter, enforcing that they specify the extension '.cob'.

Details

You may wish to put this code in one of your in-house modules, or somewhere equally suitable.

__all__= []
def__saveParameterValue( parameter, node ) :
# For more on why were making a class for this, see the bottom of this pageclassExporter :
def__init__( self, node, parameter ) :
self.__node=nodeself.__parameter=parameterdefdoExport( self, paths ) : # Paths will be empty if the user has cancelledifnotpaths :
return# Make sure that the value in the Parameterised object is up to datefn=IECoreMaya.FnParameterisedHolder( self.__node )
fn.setParameterisedValue( self.__parameter )
data=self.__parameter.getValue()
IECore.Writer.create( data, paths[0] ).write()
# Make an instance of the file extension filter, so we can# make the dialog more pickytools=IECoreMaya.FileBrowser.FileExtensionFilter( [ "cob" ] )
exporter=Exporter( node, parameter )
IECoreMaya.FileDialog(
title="Enter a file name (.cob) to save to",
buttonTitle="Export",
saveMode=True,
key="parameterValueExportTool",
filter=tools.filter,
validate=tools.validate,
# Register the method of our Exporter instance as the callbackcallback=exporter.doExport, )
def__menuCallback( definition, parameter, node ) :
active=FalsewithIECore.IgnoredExceptions( KeyError ) :
active=parameter.userData()["maya"]["useValueExportTool"].valueifnotactive :
returndefinition.append( "useValueExportTool", { "divider" : True } )
definition.append( "Export Value to disk...", { "command" : IECore.curry( __saveParameterValue, parameter, node ) } )
IECoreMaya.ParameterUI.registerPopupMenuCallback( __menuCallback )

Then, for each parameter you wish this to be used on, wherever this parameter is defined, you can do something like this

IECore.ObjectParameter(
name="geometry",
description="The geometry to render.",
defaultValue=IECore.Group(),
types= [ IECore.VisibleRenderable.staticTypeId(), IECore.ObjectVector.staticTypeId() ],
userData= {
"maya" : {
"useValueExportTool" : IECore.BoolData( True ),
},
},
)

Other tricks

The FileDialog call takes all the keyword arguments of the FileBrowser class. One of these is the options kw. This allows you to provide a callable with the following signature

f( <FileBrowser instance>, uiParent )

Making Exporter a class instead of just using a curried function becomes more relevent then. You could create something like this should you wish, which adds a text field to the dialog, and stores the text in the header of the resulting file.:

importmaya.cmds
...
def__saveParameterValue( parameter, node ) :
ClassExporter :
...
defdrawOptions( self, fileBrowserInstance, uiParent ) :
self.__description=maya.cmds.textField( parent=uiParent )
defdoExport( self, paths ) :
...
w=IECore.Writer.create( data, paths[0] )
description=maya.cmds.textField( self.__description, query=True, text=True )
w["header"].getValue()["description"] =IECore.StringData( description )
w.write()
...
IECoreMaya.FileDialog(
...
options=exporter.drawOptions, )	

Clone this wiki locally