Notifications
Clear all

[Closed] checkButton affected by object selection?

Is it possible to have a UI element – say, a checkButton – affected by the selection state of an object in the Viewport?

Thanks,

Alec

3 Replies

Yes.

You could have a timer that monitors the object’s selection state ($.isSelected), but better is to register a callback that monitors the object’s selection more directly.


-- create a sphere for demonstration purposes
sphere name:"mySphere"

callbacks.addScript #selectionSetChanged "
	if ($mySphere.isSelected) then ( print \"mySphere is selected\" )
	else ( print \"mySphere is NOT selected\" )
" id:#test

Note that the above

  • will print a message; you’ll want to change that to toggling your checkbox
  • will monitor for the object’s name; you might want to change that to use .inode.handle

Alternatively, starting with 3ds Max 2009, you could use the node event callbacks instead:


sphere name:"mySphere"

-- the event handler
fn selectionHandler e ids = (
	for id in ids do (
		obj = getAnimByHandle id
		if (obj.name == "mySphere") then (
			if (obj.isSelected) then ( print "mySphere is selected" )
			else ( print "mySphere is NOT selected" )
		)
	)
)

-- the event callback setup
myNodeEvent = nodeEventCallBack mouseUp:true selectionChanged:selectionHandler

Which you end up using really is more of a question of which max versions you need to support / which you prefer. The node event system might be faster for some tasks.

e.g.


rollout test "test" (
	local myNodeEventCallback
   
	pickbutton pb_obj "pick an object"
   
	checkbox chk_callback "general callback-driven"
	checkbox chk_event "node event-driven"
   
	fn generalCallbackHandler = (
		chk_callback.checked = pb_obj.object.isSelected
	)
	fn nodeEventCallbackHandler ev ids = (
		-- not using the event name / ids, as we're only tracking this one object anyway
		chk_event.checked = pb_obj.object.isSelected
	)
   
	on pb_obj picked obj do (
		pb_obj.caption = obj.name
		
		callbacks.removeScripts id:#test
		myNodeEventCallback = undefined
	   
		callbacks.addScript #selectionSetChanged "test.generalCallbackHandler()" id:#test
		myNodeEventCallback = nodeEventCallback mouseUp:false selectionChanged:nodeEventCallbackHandler
	)
	
	on test close do (
		myNodeEventCallback = undefined
		callbacks.removeScripts id:#test
	)
)
createDialog test

Very cool. Thanks for the descriptive answer! I’ll give it a go tomorrow morning. It’ll be great if I can get this working without it slowing down the overall scene.

Thanks again,

Alec