Struct - Access buttons/fields

I am reworking a script for my company, and i was looking into ways of restructuring a very long, linear script (over 4k lines) into something more manageable, using Struct’s and “includes”.

so far, most of the script had been restructured using the workflow mentioned above, and works as expected, using hard-coded approach, where i call/set things directly. Now, this is time to try and hook it all up with my UI, that had also been separated into its own Struct.

I can create and show my Rollout, but i seem to struggle with understanding of how to get to my buttons, drop-down menus, etc… from my main script… Unfortunately, i cannot show majority of my code here, due to company’s policy, (sorry :no:).
here is stripped snippet:

(
	fn getRollout =
	(
		rollout rigRollout ("Rigger") width:200 height:575
		(
			edittext charName "Character name:" labelOnTop:false pos:[10,25] visible:true
			dropdownlist module "Add module: " items:#("Core","Spine","Head") pos:[10,60]
			label parent "Parent: " width:40 pos:[10,105]
			button pressMe "Press me" width:140 pos:[50,105]
		)
	return rigRollout
	),
	fn show =
	(
		try(destroyDialog rigRollout) 
		catch()
		newRollout = getRollout()
		createDialog newRollout	
	)
)

stripped version of my UI, i got rid of everything, except for a button, and left everything wrapped in a struct. COPY/PASTE should be fine to make this work.

Here is the code that i wrote to test and try to get something out of my button. In reality, i would getting some data from the user/scene, but in this test, i would like to atleast get this button to print something for me.

include @"ViewUtils.ms"
myUi = rigUi()
myRollout = myUi.getRollout()
myButton = myRollout.pressMe

on myButton pressed do (
	print "Press Me - Ok"
)

myUi.show()

I am coming from python, and therefore i understand that my button inside the ViewUtils should trigger/send some sort of callback to get INITed in once the UI-object was created, but not sure what should i do here, in maxscript.
anyone has a suggestion on what approach i should take? thanks for view this guys, appreciate any feedback.

The trick is to assign the result of the roll out function to a struct variable.


struct myStruct(
	myRollout,
	fn getRollout =
	(
		this.myRollout = rollout rigRollout ("Rigger") width:200 height:575
		(...)
	)
)


( It’s not at all obvious that you can do this in the MXS examples, dusty and outdated as they are. )
See my detailed answer at your CG talk thread

In your show function, the destroyDialog call won’t work unless rigRollout is defined in a global scope; you might either pass the rollout to the function to destroy the dialog and create a new one or move it inside the rollout definition (same for the event handlers), for example like this:

struct rigUi
(
	rigRollout = rollout rigRollout "Rigger" width:200 height:575
	(
		edittext charName "Character name:" labelOnTop:false pos:[10,25] visible:true
		dropdownlist module "Add module: " items:#("Core","Spine","Head") pos:[10,60]
		label parent "Parent: " width:40 pos:[10,105]
		button pressMe "Press me" width:140 pos:[50,105]
		
		fn show =
		(
			if rigRollout.open do destroyDialog rigRollout
			createDialog rigRollout	
		)

		fn pressMeHandler = undefined

		on pressMe pressed do pressMeHandler()
	);,
	fn someOtherStructFn = return OK
)

-- if NOT isStruct myUi do -- uncomment if you only want one instance (destroyDialog will work inside one, not globally)
	myUi = rigUi()
myUi.rigRollout.show()
myUi.rigRollout.pressMeHandler = fn newHandler = print 1234