Global and Persistent Variables in pymxs

Global variables in MAXScript are "top level" variables that have global scope. They can be explicitly declared (using the global keyword), or implictly declared outside of any scope. See the "Scope of Variables" topic in the MAXScript Help.

Peristent variables are global variables that are attached to a scene and are retained on scene close and open, and between 3ds Max sessions. They are created either by using the persistent keyword when declaring the variable, or by using peristents.make() on the name of an existing global variable. See the "Persistent Global Variables" topic in the MAXScript Help.

In this example, we create a few global variables, and then make them persistent.

# globals and persistents

from pymxs import runtime as rt

# This is one way to explicitly declare a global variable:

rt.execute('global a = "hello"')

# any variable declared outside of a scope is implictly considered global,
# so this also works

rt.execute('b = "goodbye"')

# To view all globals currently declared:
globals = rt.globalVars.gather()
print globals

# getting and setting values requires a MAXScript Name 
val = rt.globalVars.get(rt.name('a'))
print val

# Persistent variables can be created on declaration, or made persistent later on
# re-declare a
rt.execute('persistent global a = "hello"')

# persistents.make() takes a name value of an existing global var
rt.persistents.make(rt.name('b'))

# We can get all the global persistent variabls with persistents.gather
val = rt.persistents.gather()
print val