MAXScript supports a limited form of persistent global variables. You declare a particular global to be persistent and the value it contains is always saved to and restored from scene files as they are opened and closed. Therefore, you can keep direct references to objects in the scene in variables and those references will persist across scene save and reload.
You declare a global variable as persistent by prefacing its declaration with the reserved word persistent. For example:
persistent global foo, baz, bar
declares the variables foo, baz, and bar to be persistent. From then on, the values in foo, baz, and bar at the time of a scene save are stored in the .max scene file. When that file is reopened, the values in foo, baz, and bar are restored, with an implicit declaration as persistent globals if they are not so already declared.
The current limitation with persistent global variables is that only certain kinds of value are storable in a scene file, and only those types of values are saved and restored across scene saves and loads. The supported value classes are: Integer, Float, String, Color, Time, Interval, Array, Point3, Ray, Quat, AngleAxis, EulerAngles, Matrix3, Point2, Undefined, OK, Boolean, and all the MAXWrapper classes (nodes, modifiers, controllers, materials, and so on). All other types of values restore as the value undefined.
In the case of Array, only those values in the array that are in the previous list are correctly saved and restored; others appear as undefined in the restored array.
Persistent globals are removed when a File > Reset, File > New, or File > Open is performed, so persistent globals added to one file do not "stick around" and become part of every other file opened and saved. If needed, you can install persistent globals in each new file in a #filePreSave callback. See General Event Callback Mechanism for more information.
The following methods are used to show and delete persistent globals variables:
Displays a list of persistent globals and their value.
Removes the named persistent global from the persistent global pool. The variable remains as a global variable and retains its value.
Removes all persistent globals from the persistent global pool. The variables remain as global variables and retain their value.
Script
a="Hello world"
persistent global a
persistent global b=box()
persistents.show()
persistents.remove #a
persistents.removeall()
a
b
Output
"Hello world"
OK
$Box:Box02 @ [0.000000,0.000000,0.000000]
a: "Hello world"
b: $Box:Box02 @ [0.000000,0.000000,0.000000]
OK
OK
OK
"Hello world"
$Box:Box02 @ [0.000000,0.000000,0.000000]
Note:
If a persistent global contains a MAXWrapper value, but the class instance is not in the scene, that value is not stored/restored on a file save/load.
Example:
-- persistents - node only
persistent global global_array = #()
global_array[1] = b= box()
global_array[2] = bm= bend()
global_array[3] = sm= standard()
global_array[4] = fog()
global_array[5] = area()
global_array[6] = bezier_float()
global_array[7] = br= bricks()
global_array[8] = lookat()
global_array[9] = blur()
global_array[10] = MapScaler()
--b.material=sm
--addmodifier b bm
--sm.diffusemap=br
persistents.show()
max hold
max fetch
persistents.show()
Remarks:
Only the box is in the array after running this script. If the commented lines are run, the material, modifier, and map are also in the array. MAXScript does not do a full reference-tree dump when it outputs its persistent variables, it just outputs RefIDs for MAX objects and so depends on them being dumped as references by the main file save code.