ZBrushCentral

Managing UI States, MemBlocks and Global Variables

My latest plugin has brought up a number of issues and questions around initialization and maintenance of both global variables and UI switch and slider states.

I wanted to post a few questions / statements around that, and see if there are some recommended best practices based on previous info from Marcus and Mark. I’m basically storing a lot of memory blocks now.

Problems (when zscript looses focus):


  • User switch and slider states are lost
  • Global variables, switch / slider states, and memory blocks may be out of sync

Here is what I’ve started to do…

We know that the Zscript sort of re-initializes on re-focus, so I’m using memory blocks to store the users parameter values, and the switch states:

///// ----- Button Var ----- /////

[VarDef, gReFocusVar, 1]
[If, [MemGetSize,MB_FocusVar],
,
[MemCreate,MB_FocusVar,7,0]
[MemWrite,MB_FocusVar,#gReFocusVar]
]
[MemRead,MB_FocusVar,#gReFocusVar]

[If, [MemGetSize,MB_FocusVar],
[Note, [StrMerge, "Init Value: ", #gReFocusVar]]
,]

///// ----- Switch State ----- /////

[VarDef, gSwitchState, 0]
[If, [MemGetSize,MB_SwitchState],
,
[MemCreate,MB_SwitchState,7,0]
[MemWrite,MB_SwitchState,#gSwitchState]
]
[MemRead,MB_SwitchState,#gSwitchState]

When the script comes back into focus, the memory block value sets the switch or slider state.

[IButton, [StrMerge, #ZPLUGIN,":",“Set FocusVar”],"",

[MemWrite,MB_FocusVar,22] [MemRead,MB_FocusVar,#gReFocusVar]

,1.0]

[IButton, [StrMerge, #ZPLUGIN,":",“Read FocusVar”],"",

[MemRead,MB_FocusVar,#gReFocusVar] [Note, [StrMerge, "Cmd Value: ", #gReFocusVar]]

,1.0]

[ISwitch, [StrMerge, #ZPLUGIN,":",“Switch”], #gSwitchState, “Im a switch”,

[MemWrite,MB_SwitchState,1] , [MemWrite,MB_SwitchState,0]

,1.0]

In Summary:


  • On initialization I’m creating global variables AND memory blocks for all things I want persistent (values and ui states)
  • Im setting global variable values on initialization from memory blocks
  • UI Elements are responsible for setting MemBlocks, and get their state values from the initialization / re-initialization of global variables
  • Routines are (usually) responsible for getting MemBlocks and setting Global Variables


  1. Are there easier / better ways of making sure that all the memory blocks, gvars and ui elements are in sync?
  2. What I’m doing seems to be working, but I wonder if there are any gotchas?
  3. I plan on reducing the amount of code by using arrays, haven’t done that yet though
  4. To make things more complicated I want to save User Settings to a File next so that they can retrieve their states and values between ZBrush sessions
  5. [LIST=1]
  6. I’m assuming that will just a be a set / get routine called by a button in the plugin that just sets the values of all the memory blocks, variables and button states

[/LIST]

Is it possible to rename the thread to remove the double “s” in variabless?! :wink:

Thinking out loud here…

I’m guessing it would be wise to:


  • drop most of the individual memory blocks.
  • Use MVarDef instead.
  • Pair MVarDef index range and items with the VarLoad and VarSave variables.