Actually I gave chatgpt this code and asked it to rewrite it using a callback, seems to do what u want but cant guarantee it (and keeps original display eg. box/point cloud/etc.). Seems to have some error with deselection, ill take a look when i get time but keeps working even with the error. I noticed it does some things that need fixing, keep you posted. Ok should be fine now, take note when you enable/disable it will deselect everything so just do your selections after toggling the button.
Note: One thing I noticed is that if you duplicate the selected objects while enabled all those objects will remain in mesh display mode permanently.
global CPV_previzStore = #()
global CPV_callbackActive = false
global CoronaProxyVizEnabled = false
fn CPV_restorePreviz objs =
(
for obj in objs where isValidNode obj and classof obj == CProxy do
(
local storedObjs = for e in CPV_previzStore collect e[1]
local idx = findItem storedObjs obj
if (idx > 0 and idx <= CPV_previzStore.count) do
(
obj.previzType = CPV_previzStore[idx][2]
)
)
)
fn CPV_callbackFn =
(
local curSel = selection as array
local storedObjs = for e in CPV_previzStore collect e[1]
-- restore deselected objects
for sObj in storedObjs where (not isDeleted sObj and findItem curSel sObj == 0) do
(
CPV_restorePreviz #(sObj)
local i = findItem (for e in CPV_previzStore collect e[1]) sObj
if i > 0 do deleteItem CPV_previzStore i
)
-- process new selection
for obj in curSel where isValidNode obj and classof obj == CProxy do
(
if (findItem storedObjs obj) == 0 do
(
append CPV_previzStore #(obj, obj.previzType)
obj.previzType = 3 -- show full mesh
)
)
)
fn CPV_toggleCallback =
(
if CPV_callbackActive then
(
callbacks.removeScripts id:#CPV_selChanged
CPV_callbackActive = false
CoronaProxyVizEnabled = false
)
else
(
callbacks.addScript #selectionSetChanged "CPV_callbackFn()" id:#CPV_selChanged
CPV_callbackActive = true
CoronaProxyVizEnabled = true
)
)
try(cui.UnRegisterDialogBar CoronaProxyViz)catch()
try(DestroyDialog CoronaProxyViz)catch()
rollout CoronaProxyViz("CoronaProxyViz")
(
button btn_proxy "CoronaProxyViz Disabled" width:140
on btn_proxy pressed do
(
clearSelection()
CPV_toggleCallback()
if CoronaProxyVizEnabled then btn_proxy.text = "CoronaProxyViz Enabled" else btn_proxy.text = "CoronaProxyViz Disabled"
)
on CoronaProxyViz close do
(
callbacks.removeScripts id:#CPV_selChanged
CPV_callbackActive = false
CPV_previzStore = #()
CoronaProxyVizEnabled = false
)
)
CreateDialog CoronaProxyViz style:#(#style_titlebar, #style_sysmenu, #style_toolwindow)
cui.RegisterDialogBar CoronaProxyViz
If you want this as a button in your toolbar just copy/paste this into the Maxscript editor, select all the text and drag that text to somewhere in your toolbar. You can then right click on the button and change the icon/text.