Notifications
Clear all

[Closed] Toggling viewport materials display

Finally. I have made it to work :bowdown:

macroScript NS_materialToggle
Category:"Nightshade"
toolTip:"Viewport Material Display Toggle"
buttontext:"Viewport Material Display Toggle"
(
	global toggle
	if toggle == undefined do toggle = false
	for o in objects where o.material != undefined do showTextureMap o.material toggle
	toggle = not toggle

)

why is “toggle” global?

2 Replies
(@arahnoid)
Joined: 11 months ago

Posts: 0

If I don’t make it global then toggle always will be false.

As I understand global variables stays in memory until max is closed, this way can access them at any time even if script finished. And next time when you run the script, you may access toggle value.

I know what is an option to save the variable value in the *.ini file but this is kind of quick and dirty way to do it.

(@deadlynightshade)
Joined: 11 months ago

Posts: 0

I know that in general, the use of global vars is bad practise.
But his code runs:

macroScript NS_materialToggle
Category:"Nightshade"
toolTip:"Viewport Material Display Toggle"
buttontext:"Viewport Material Display Toggle"
(
	-- Create toggle var
    global NS_matDispToggle
	if NS_matDispToggle == undefined do global NS_matDispToggle = false

	-- Turn on/off for all objects in scene
    for o in objects where o.material != undefined do showTextureMap o.material NS_matDispToggle

	-- Invert boolean
	NS_matDispToggle = not NS_matDispToggle
)

If you have a stricter way of writing this then please share – Im all ears!

global in this case is not necessary.
macroscript is global itself. so its local scope stays life in memory and can’t be flushed by the system.
so the good code is:

macroScript MaterialDisplayToggle
    category:"Display"
    toolTip:"Viewport Material Display Toggle"
    buttontext:"Mat Toggle"
    autoUndoEnabled:off
(
    local act = off
    on isChecked do act -- optional
    on isEnabled do geometry.count > 0 -- optional
    on execute do undo "Material Display Toggle" on
    (
        act = not act
        for node in geometry where node.mat != undefined do showTextureMap node.material act
        completeredraw() -- in case if render meshes cached it needs forcing redraw
        updateToolbarButtons() -- update state of macro button
    ) 
)

(i don’t have max so the code was written blind)

but i would not do a toggle action.
i would do OFF using default button click and ON using click + SHIFT

I’m speechless it is so cool :applause:

Page 2 / 2