MAXScript Language Reference > Names, Literal Constants, and Expressions > Context Expressions >

# undo

The undo <boolean> context provides control over whether scripted scene changes will be undoable.

By default, the operations performed in MAXScript are undoable.

This means that scene-modifying commands typed in the Listener are undoable by default. Making this a context-controlled activity allows you to decide when the undo overhead is to be taken.

EXAMPLE:
    undo on
    (
    delete $box*
    delete $sphere*
    clearUndoBuffer()
    )

The undoable operations appear as items labeled "MAXScript" in the 3ds Max Undo menu.

It is important to note that the granularity of the undoable operations is at the level of entire undo clauses.

In the previous example, one entry is added to the Undo stack containing both deletes as a single operation.

Choosing undo for this entry in the Undo menu causes both deletes to be undone.

Therefore, you can control the granularity of an undo by choosing the appropriate undo block expression groupings.

You can invoke an undo from MAXScript with the max command:

    max undo

When you script very large scene changes, you should consider turning undo off as it is quite easy to fill up the Undo stack and consume substantial amounts of memory.

Disabling undo can improve performance in this situation.

If the actions your script performs could interfere with entries already in the Undo stack, you should explicitly clear the Undo stack before enabling undo.

An entry in the Undo stack generally expects the scene to be in the state it was when the entry was placed in the Undo stack.

If you interrupt the storing of Undo stack entries, and modify the scene in an incompatible way (such as deleting an object that an entry assumes is still around), a 3ds Max crash is likely if you then try to perform an undo.

Using the following functions you can control the undo and scene save states in 3ds Max:

    clearUndoBuffer()

Empties the Undo stack, both providing a way to reset the undo state and another way to control the save-changes requester.

    setSaveRequired <boolean>

Lets you set the 3ds Max system "dirty" flag. If this flag is set or there are entries in the Undo stack, you are asked if you want to save the scene file on a File > New or File > Reset.

    getSaveRequired()

Returns true if the 3ds Max system "dirty" flag is set to true or if the Undo buffer is not empty. If the Undo buffer if not empty, this function will still return true , even if you just called setSaveRequired false .

Normally, if there are entries in the Undo stack when the scene is closed, 3ds Max will prompt with a save-changes requester. In some cases, where you are controlling undo in MAXScript, you may want to force the need for a save-changes prompt.

MAXScript provides some control over the labels displayed in the 3ds Max undo/redo dropdown list for undoable MAXScript operations.

This is an improvement to just displaying "MAXScript" in all cases, as it did in versions prior to 3ds Max 4.

Any undoable macroScript execution, such as via a toolbar button, menu item, hotkey or quad-menu item, will be displayed in the undo/redo list with the name of the macroScript.

The 'undo on' context prefix accepts an optional string literal (text in double quotes) after the 'undo' keyword, which will be used as the label for the undo block in the 3ds Max undo/redo lists.

### Syntax

    undo ["undo_item_label" | label:<string_operand> | variable_name ] <bool_expr> <expr>

where

"undo_item_label" must be a string literal,

<string_operand> evaluates to a string at runtime,

variable_name is a global or local variable containing a string value.

EXAMPLES:
    rollout test "test"
    (
    local undoString = "Button Press"
    local buttonPress = 0
    button b "press me"
    on b pressed dowith undo label:(undoString + (buttonPress +=1) as string) on box()
    )
    createdialog test
    undo "add background" on
    (
    ...
    )
    my_context_name = "An Undo Context"
    undo my_context_name on
    (
    ...
    )

These examples will generate an undo-block for all the operations in the block-expression following the prefix, which will display in the undo/redo lists with the given string as its name.

The name must be a literal string and is optional, and will default to "MAXScript".