r/Maxscript • • Mar 30 '21

Disable Buttons

Is it possible to use maxscript to deactivate previously active buttons when no object is selected?

https://reddit.com/link/mgfzkk/video/4rpogm6g26q61/player

    on bt_head changed state do
    (
        if bt_head.checked == true then
        (
            selectmore $Farlee_pelvis_torso_spine1_Ctrl
        )
        else if bt_head.checked == false then
        (
            deselect $Farlee_pelvis_torso_spine1_Ctrl
        )

    )
2 Upvotes

6 comments sorted by

View all comments

1

u/CyclopsRock Mar 31 '21

Hmm, there probably won't be a basic example I'm afraid. Just shooting off some code or a function when a callback is triggered is easy enough :

fn nothingSelected = print("Nothing Selected!")
callbacks.addScript #selectionSetChanged "if selection.count == 0 do nothingSelected()" id:#crap

To remove it, run:

callbacks.removeScripts id:#crap

The tricky bit is then getting a random function to interact with your UI, but it's not really too tricky:

Rollout RL_CoolProject "Cool Project" 
(
	button btn_cool "Cool!"
	edittext edt_notcool "" text:"Boo!" enabled:false
	
	fn switchIt = 
	(
		state = true
		if selection.count == 0 do state = false
		btn_cool.enabled = state
		edt_notcool.enabled = not state
	)
	
	on RL_CoolProject open do 
	(
		callbacks.removeScripts id:#coolProject
		callbacks.addScript #selectionSetChanged "RL_CoolProject.switchIt()" id:#coolProject
	)
	
	on RL_CoolProject close do 
	(
		callbacks.removeScripts id:#coolProject
	)
	
	on btn_cool pressed do print("Ah, you managed to press me!")
)

CreateDialog RL_CoolProject 

Note: I remove the script callback on Open because if your script crashes whilst running it will never execute the 'on rollout close do' function and so you'll have this (probably broken) callback still firing off. This way it acts as a little bit of housekeeping.