Global variables in MAXScript are "top level" variables that have global scope. They can be explicitly declared (using the global
keyword), or implicitly declared outside of any scope. See the "Scope of Variables" topic in the MAXScript Help.
Persistent 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
# As of 3ds Max 2021, we can implictly create variables from Python:
rt.a = 'hello'
# Pre-2021, you need to use pymxs.runtime.execute
# this is one way to explicitly declare a global variable:
rt.execute('global b = "hello again"')
# Any variable declared outside of a scope is implictly considered global,
# so this also works:
rt.execute('c = "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 ('a={}'.format(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 variable, and
# is preferable because it does not require pymxs.runtime.execute():
rt.persistents.make(rt.name('b'))
# We can get all the global persistent variables with persistents.gather
val = rt.persistents.gather()
print ('gather={}'.format(val))
The output is:
#(#NodeColorPicker, #getModContextBBoxMax, #Summed_area, #MAXCurveCtl, #setSplitMesh, #arnold_spherical_camera, #joinSurfaces, #StereoCameraRig, #log, #ai_fraction, #PxStepSimulation, #renderers, #PFBoxMeshWrapper, #Light__Area, #fetchMaxFile, #getSplitMesh, #Color_RGB, #Brightness_and_Contrast, #MSPluginClass, #g_ep, ...)
a=hello
gather=#(#b, #CAT_UINum, #a)