This demo comes from the official Maya Python API documentation.
⚠️ Important: read this first. Thescript.pyfile in this folder is literally one line:print("Hello world!"). That is not a typo and not a mistake — it's the entire official Autodesk example. The real lesson is thefileDialogworkflow around the file, not the file itself.
Autodesk ships this tiny script.py so you have a harmless "do-nothing" file
to pick with the file dialog. The value here is learning the workflow:
"How do I let a user choose a file on disk, then do something with the path they chose?"
That workflow uses the fileDialog / fileDialog2 command — not the API.
This script.py is just the target the user picks. Knowing that upfront
saves you scratching your head looking for hidden meaning in one print line.
maya.cmds.fileDialog2— the modern Maya command that pops a native OS file picker and returns the chosen path(s). (OlderfileDialogis deprecated; always preferfileDialog2.)- The "pick → get path → act" pattern that every file-driven Maya tool follows (importers, exporters, library loaders, batch processors…).
- File dialog flags —
fileMode,caption,fileFilter, and how they shape the dialog the user sees. - Why
script.pyis the way it is: the world's smallest placeholder file, used purely as something safe to point the dialog at.
| Concept | Where to learn it |
|---|---|
maya.cmds basics |
introduction/helloCube.py |
| Python strings / paths | standard Python tutorial |
| Maya's command syntax | Maya docs → Python Commands |
You do not need the OpenMaya API for this demo — it is pure cmds.
fileDialog/
├── README.md ← this file
└── script.py ← the demo (one line: print("Hello world!"))
The official script.py content is:
print("Hello world!")That's the whole thing. The point of this README is to make that useful.
Here's how you would use fileDialog2 to pick script.py (or any file) and
do something with it:
import maya.cmds as cmds
# 1. Pop the native file dialog and let the user pick a .py file
path = cmds.fileDialog2(
fileMode=1, # 1 = "select existing file"
caption="Pick a Python script", # window title
fileFilter="Python Files (*.py)", # filter shown in the dropdown
)
# 2. fileDialog2 always returns a LIST (even for a single pick), or None
if path:
path = path[0]
print("You picked:", path)
# 3. Do something with the chosen path.
# Here we just exec the script the user picked (this is what makes
# script.py print "Hello world!" when selected).
with open(path) as f:
code = f.read()
exec(code)
else:
print("User cancelled.")Run this in the Maya Script Editor, then pick script.py from this folder.
The Script Editor will print:
You picked: D:/2026MayaPython/fileDialog/script.py
Hello world!
The Hello world! is script.py running — that's the whole demo.
fileMode is the most important flag — it controls what the dialog lets the
user do. Memorize these four:
fileMode |
Meaning |
|---|---|
0 |
Save file (asks for a name, may overwrite) |
1 |
Open existing file (single select) |
2 |
Open existing file (multi-select, returns a list) |
3 |
Select a directory |
fileMode=4 also exists (multi-dir select) but is rarely used.
You'll see both names floating around. Use fileDialog2.
fileDialog (deprecated) |
fileDialog2 (recommended) |
|
|---|---|---|
| Return type | a single string | a list of strings, or None |
| Multi-select | not supported | supported |
| Status | kept for old scripts | actively maintained, new features |
Always wrap the result of fileDialog2 with a length check:
result = cmds.fileDialog2(fileMode=1, ...) or []The or [] saves you from NoneType has no len() crashes when the user cancels.
- Make it print a real list of files. Switch to
fileMode=2and loop over every picked path, printing each one. Confirm you now get a list with more than one item. - Add a real filter. Try
fileFilter="Maya Files (*.ma *.mb);;All Files (*.*)"and confirm the dropdown lets you switch between filters. - Build a tiny "run script" tool. Wrap the workflow above in a function
run_picked_script()and add a shelf button for it. Now you have a one-click "load and run any .py" tool. - Pick a directory, not a file. Use
fileMode=3, then list the folder contents withos.listdir(path)and print every.pyfile inside. - Save a file. Use
fileMode=0, write the current scene name to the chosen path withopen(...).write(...), and read it back. You've just built the skeleton of a config-file save/load tool. - Pair it with a UI. Combine with
cmds.window/cmds.buttonto make a small panel with a "Browse…" button that fills a text field with the picked path. This is the standard look of every Maya export tool.
fileDialog2returns a list, not a string. Even for a single file. If you docmds.fileDialog2(...).endswith(...)you'll crash. Alwayspath[0].- Always handle cancellation. If the user clicks Cancel, you get
None. Guard withif path:oror []before indexing. - Use forward slashes. Maya accepts
/on all platforms; mixed\paths on Windows can break string formatting.path.replace('\\', '/')is a cheap safety net. fileDialog(no 2) is deprecated. Don't start new code with it. It returns a bare string and can't multi-select.- Don't
execuntrusted code. The demo above runs whatever the user picks — fine for a personal tool, dangerous if shipped to others. For shared tools, preferimportliboverexecso the source is at least traceable.
Because Autodesk's point isn't to teach fileDialog2 itself (that belongs in
the fileDialog2 command docs). This example exists in the API reference so
that other examples that say "select a script file" have something to
point at. Treat script.py as a fixture, not a lesson — the lesson is the
dialog workflow you write around it.
Autodesk, Maya Python API 2.0 Reference — fileDialog/script.py, Maya 2027 (ENU). https://help.autodesk.com/cloudhelp/2027/ENU/MAYA-API-REF/py_ref/file_dialog_2script_8py-example.html