MAXScript Context Expressions

The pymxs module exposes MAXScript context expressions that can be used in conjunction with the Python with statement. These expressions are:

For example, to create a teapot and animate it:

import pymxs
at = pymxs.attime
rt = pymxs.runtime
t = rt.Teapot()

with pymxs.animate(True):
with at(1):
    t.pos = rt.Point3(2,2,2)
with at(12):
    t.pos = rt.Point3(10, 10, 10)
with at(21):
    t.pos = rt.Point3(20,20,20)

Undo / Redo and Exceptions

The way an undo block in pymxs handles exceptions is different than in MAXScript. When an exception is raised, code executed before the exception is undone. The exception is not raised beyond the scope of the undo block, as the undo operation is considered to be the exception handler. Here is an example to illustrate this behavior:

# undo with exception

import pymxs
from pymxs import runtime as rt

def make_box_and_raise():
    with pymxs.undo(True, 'Making box'):
        rt.Box()
        # this will undo the box, and the error will not show up
        # in the listener, because it is handled:
        raise RuntimeError('This is an error')
        
def make_sphere_and_raise():
    rt.Sphere()
    # this will show up in the lister as an unhandled exception
    raise RuntimeError('This is an error')

make_box_and_raise()
make_sphere_and_raise()