Does exist a simple way to remove variables?

Maybe somebody know a simple way to remove variables what was created during the script testing for example if you create rollout

rollout rl_test "Some Rollout" width:100 height:100
(
  checkbutton myButton "My Button" width:80 height:24
)
createDialog rl_test

if I execute code bellow before rollout is created

rl_test.myButton.state

then I get
-- Unknown property: "myButton" in undefined
and this is fine because max doesn't know about such a rollout and button

but after rollout was created it always returns some value even if rollout was closed. Maybe are some smart way to clear this data from Max memory without restarting Max.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
3dwannab's picture

.

In your case replace:
toolDeleteGlobal "myGlobalGlobal"

with:
toolDeleteGlobal "rl_test"

Setting them to undefined does nothing but create the global and set it to undefined.

The only true method to remove them is by using:
globalvars.remove "rl_test"

/*
https://help.autodesk.com/view/3DSMAX/2016/ENU/?guid=__files_GUID_E7584E...
*/
 
-- Uncomment on 1st run and comment out on the 2nd run. You'll see that it will be deleted.
-- global myGlobalGlobal = "A String"
 
fn toolDeleteGlobal theName formatOn:off =
(
  try(
    varsGlobal = for i = 1 to ((globalVars.gather()).count) where (matchPattern (globalVars.gather())[i] pattern:(theName)) collect (globalVars.gather())[i]
    if varsGlobal.count > 0 then
    (
      for i = 1 to varsGlobal.count do (
        local varName = varsGlobal[i]
        local varNameStr = varName as string
        local varGet = globalVars.get varNameStr
        local varIsGlobal = globalVars.isglobal varName
        local varTypeTag = globalVars.getTypeTag varName
        local varValueTag = globalVars.getValueTag varName
        if formatOn do (
          format "Name of global:\t\t%\n" varName
          format "index:\t\t\t\t%\n" i
          format "get:\t\t\t\t%\n" varGet
          format "isglobal:\t\t\t%\n" varIsGlobal
          format "getTypeTag:\t\t\t%\n" varTypeTag
          format "getValueTag:\t\t%\n" varValueTag
          )
        globalvars.remove varNameStr -- Do not set them to undefined. This is the only method that will remove them.
        if formatOn do (format "GLOBAL REMOVED:\t\t%\n\n" varNameStr)
        )
      ) else (
      if varsGlobal.count == 0 do (format "No global variable(s) with the name \"%\" exist.\n" theName)
      )
      ) catch(print (getcurrentexception()))
  ) -- End toolDeleteGlobal fn.
 
  -- You can add an asterisks anywhere here to delete them. Handy if you have a unique global naming scheme.
toolDeleteGlobal "myGlobalGlobal" formatOn:on

I'm starting to put all my fns in structs lately so here's a struct version.

/*
 
globalVars Structure: https://help.autodesk.com/view/3DSMAX/2016/ENU/?guid=__files_GUID_E7584E...
 
*/
 
clearListener()
 
actionMan.executeAction 0 "40472"
 
(
 
  global _3db_Tools -- Declare a global variable to hold the Structure.
  local _3db_ToolsStruct -- The Structure itself doesn't need to be global.
  struct _3db_ToolsStruct (
 
    fn deleteGlobals theName formatOn:on =
    (
      try(
        varsGlobal = for i = 1 to ((globalVars.gather()).count) where (matchPattern (globalVars.gather())[i] pattern:(theName)) collect (globalVars.gather())[i]
        if varsGlobal.count > 0 then
        (
          for i = 1 to varsGlobal.count do (
            local varName = varsGlobal[i]
            local varNameStr = varName as string
            local varGet = globalVars.get varNameStr
            local varIsGlobal = globalVars.isglobal varName
            local varTypeTag = globalVars.getTypeTag varName
            local varValueTag = globalVars.getValueTag varName
            if formatOn do (
              format "Name of global:\t\t%\n" varName
              format "index:\t\t\t\t%\n" i
              format "get:\t\t\t\t%\n" varGet
              format "isglobal:\t\t\t%\n" varIsGlobal
              format "getTypeTag:\t\t\t%\n" varTypeTag
              format "getValueTag:\t\t%\n" varValueTag
              )
            globalvars.remove varNameStr -- Do not set them to undefined. This is the only method that will remove them.
            if formatOn do (format "GLOBAL REMOVED:\t\t%\n\n" varNameStr)
            )
          ) else (
          if varsGlobal.count == 0 do (format "No global variable(s) with the name \"%\" exist.\n" theName)
          )
          ) catch(print (getcurrentexception()))
      ) -- End deleteGlobals fn.
 
    )
  _3db_Tools = _3db_ToolsStruct() -- Declare the struct.
 
  --   Uncomment on 1st run and comment out on the 2nd run. You'll see that it will be deleted.
  --   global myCreatedOnTheFlyFunction = "A String"
 
  -- You can add an asterisks anywhere here to delete them. Handy if you have a unique global naming scheme.
  _3db_Tools.deleteGlobals "myCreatedOnTheFlyFunction*" formatOn:on
 
  )
miledrizk's picture

globalvars.remove

It's an old post i know, but in case someone else has this problem and came to this thread.

To remove a global variable, you need to use globalvars.remove

for example if your global variable is called my_number, then to remove it you just type

globalvars.remove "my_number"

Anubis's picture

see globalVars Structure

Free() Method is nice to clean up memory BUT it NOT revome global variables(!), it just reinit their value. For example:

str = "The big power mean big responsibility"
free str
str == "" --> true
str == undefined --> false

You can check in the help for globalVars Structure but read carefully all the warning notes.

In the best case, a.k.a. good practice, use only Local variables.

Cheers!

my recent MAXScripts RSS (archive here)

kproudfoot_zynga's picture

You can also try free()

This has been in Max since version 2009, but they say it was undocumented until version 2012.

Free() Method

The free() method has been implemented since 3ds Max 9 but has remained undocumented. This method frees the memory used by the value without waiting for a garbage collection to occur. It is implemented for the following classes:

String Value
StringStream Value
FileStream Value
BitMap Value
Array Value
BitArray Value
Mesh Value

Principal Technical Artist - Zynga, Inc.

jos's picture

you can simply type this

you can simply type this line

rl_test = undefined
Arahnoid's picture

Thanks Jos For rollout

Thanks Jos
For rollout elements it will work perfect :)
However for another kind of variables what are cashed will be more work to undefine all of them.

jos's picture

dont think it's possible

dont think it's possible without restart max. you can try yo run the garbage collector. (Maxscript help : Manual Garbage Collection) but it doesnt clear variables in use..

Arahnoid's picture

gc() - doesn't help

You are right garbage collect doesn't help :(
any way thank you for help :) at list now I know how to destroy variables in rollout :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.