There are many ways to execute Python scripts and commands in 3ds Max.
You can write and execute Python scripts in the MAXScript Editor, and run Python commands in the Max Listener window.
The MAXScript Editor supports Python language syntax highlighting and code completion for imported libraries. By default, the MAXScript Editor is set to MAXScript syntax when it first opens. Select Language > Python to change this to Python.
Code completion is invoked when you type a symbol and dot, or can be force-invoked by pressing Ctrl+Space.
Python scripts can be evaluated from the Editor using Tools > Evaluate All (Ctrl+E), and a single line can be executed using Tools > Evaluate Line / Selection (Shift+Enter). Note, to execute multi-line code blocks, select the block before executing this command.
Some special attributes (specifically __name__
) and source code encoding declarations (\# -*- coding: <encoding name> -*-
, defined in PEP 0263) cannot be handled when a script is executed directly from the MAXScript Editor (using Tools > Evaluate All, Ctrl+E) or in the MAXScript Listener. Scripts that use these must be run from the Scripting > Run Script... command to execute properly. The Run Script command runs the file directly in the Python interpreter, while the Evaluate command runs the contents of the file.
You can edit one or more scripts side-by-side by dragging the document tab, and selecting "Clone to another view" or "Move to another view". This can be useful when editing very long scripts or comparing two scripts.
For more information about the MAXScript Editor's use and features, see Basics > Interface Overview > MAXScript Interface in the 3ds Max Help.
The MAXScript Listener can execute single and multi-line Python commands when in Python mode. Python mode is indicated by a command-line starting with three brackets: >>>. To switch to Python mode, select the Python radio button, or enter three angle brackets and a space. To execute a command, type the command and press Enter to type more lines, or Ctrl+Enter to execute the code.
The MAXScript Mini Listener can execute a single line Python command when the Listener is in Python mode. Commands are executed by pressing Enter.
As of 3ds Max 2022.2 Update, you can also execute .pyc (bytecode compiled) files from the command line, or using 3ds Max Batch. However, distributing code as .pyc is not generally recommended because it is then tied to the specific Python interpreter version that compiled it (meaning the code would break in future versions of 3ds Max that use a different version of Python). Using .pyc files is also not a very effective way to obfuscate Python code, as it can easily be decompiled.
To execute a .py script from the command line:
-U PythonHost
command line switch, for example as follows:3dsmax -U PythonHost demoBentCylinder.py
You can also execute Python scripts using 3ds Max Batch: 3dsmaxbatch.exe <script_file> [options]
. See the "Using 3ds Max Batch" topic in the 3ds Max Help for more information.
See Executing Python from MAXScript for a list of directories under which 3ds Max will look for the .py
file.