Passing var value from rollout to macro script

Hi!

I'm having a problem with a script I wrote. My main script is a macro script which I have assigned to a keyboard shorcut. The other script is a simple rollout with a pickbutton which stores an object in a global variable.

My macroscript should be able to read this global variable, but it only happens if I open a macro editor and evaluate an entire script (instead just using a shortcut). Otherwise it will just keep reporting undefined.

Once the macroscript is evaluated in script editor, I can use the pickbutton in the rollout to change the picked object and macroscript will respond correctly when activated by a keyboard shortcut.

Why does it work only if evaluated once an object is picked? Is there a way around it?

Many thanks in advance for any help or suggestions!

>>>>>>EDIT<<<<<<

Finally found a solution that made my script "usable"! I've added:
fileIn "DropDown.ms"
at the beginning of the UI part, so a macro script gets automatically run every time a UI is started.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
miauu's picture
Alen's picture

I've stripped the code to

I've stripped the code to essential parts. I have the UI part in startup and macro binded to a keyboard shortcut. If I open the scene, select one object using UI and then call macro function, it will report dropDownTerrain as undefined. But if I run macro script first (using MAXScript/Run Script...) and then invoke it with a shortcut, it will work (in case presented, it will give no warning at all)

macro:

macroScript DropDownTest
category: "Alen'sScript"
(
	if dropDownTerrain == undefined then 
	(
		messagebox "no terrain selected"
	)
)

UI:

if ((dropDownUI != undefined) and (dropDownUI.isDisplayed)) do
	(destroyDialog dropDownUI)
 
rollout dropDownUI "Drop Down UI"
(
 
	pickButton pickTerrain "Pick object for Drop Down collision" autoDisplay: true
	 on pickTerrain picked terrain do
	(
		global dropDownTerrain=terrain
	)
)
createDialog dropDownUI 200 50
global dropDownTerrain = undefined
miauu's picture

if ((dropDownUI != undefined)

if ((dropDownUI != undefined) and (dropDownUI.isDisplayed)) do
	(destroyDialog dropDownUI)
 
rollout dropDownUI "Drop Down UI"
(
 
	pickButton pickTerrain "Pick object for Drop Down collision" autoDisplay: true
	 on pickTerrain picked terrain do
	(
		global dropDownTerrain=terrain
	)
)
createDialog dropDownUI 200 50
-- remove next line and try again
-- global dropDownTerrain = undefined 
Alen's picture

Thanks, but it didn't work.

Thanks, but it didn't work. In fact, I've put that there in an attempt to solve the problem.

Would it be possible to run macro script from the UI part?

miauu's picture

What the macro do? Check only

What the macro do? Check only if the terrain is undefined?
You can have only one script - with the UI. With the Pick object button you get the terrain object, and with another button you will execute the code that is in the macro, but you will not have a separate macro. Doing this you will have only local variables.

(
	if ((dropDownUI != undefined) and (dropDownUI.isDisplayed)) do
	(destroyDialog dropDownUI)
 
	rollout dropDownUI "Drop Down UI"
	(	 
		local dropDownTerrain = undefined
		pickButton pickTerrain "Pick object for Drop Down collision" autoDisplay: true
		button btn_DO "DO"
 
		on pickTerrain picked obj do
		(
			dropDownTerrain = obj
		)
 
		on btn_DO pressed do
		(
			if isValidNode dropDownTerrain do
			(
				--	put the code from the macro here
			)
		)
	)
	createDialog dropDownUI 200 50
)
Alen's picture

Macro takes a current

Macro takes a current selection of objects and aligns them on a surface selected by UI. I really wanted a macro script that could be activated by a keyboard shortcut instead of having a button to click. If nothing else works, i'll have to fall back to that option.

Thanks again for the effort! :)

miauu's picture

Try this. No need to press

Try this. No need to press another button - pick the node and the code from the macro will be executed.

(
	if ((dropDownUI != undefined) and (dropDownUI.isDisplayed)) do
	(destroyDialog dropDownUI)
 
	rollout dropDownUI "Drop Down UI"
	(	
		pickButton pickTerrain "Pick object for Drop Down collision" autoDisplay: true
		button btn_DO "DO"
 
		on pickTerrain picked obj do
		(
			--	use obj in the code bellow
			--	put the code from the macro here
		)
	)
	createDialog dropDownUI 200 50
)
Alen's picture

Yeah, I understand what you

Yeah, I understand what you mean... I'll try that, thanks!

miauu's picture

And this is from maxscript

And this is from maxscript help file. :) Almost what you want without UI.

macroscript MoveToSurface category:"HowTo"
(
	fn g_filter o = superclassof o == Geometryclass
	fn find_intersection z_node node_to_z =
	( 
		local testRay = ray node_to_z.pos [0,0,-1] 
		local nodeMaxZ = z_node.max.z 
		testRay.pos.z = nodeMaxZ + 0.0001 * abs nodeMaxZ 
		intersectRay z_node testRay
	)
 
	on isEnabled return selection.count > 0 
	on Execute do
	(
		target_mesh = pickObject message:"Pick Target Surface:" filter:g_filter
		if isValidNode target_mesh then
		(
			undo "MoveToSurface" on
			(
				for i in selection do
				(
					int_point = find_intersection target_mesh i
					if int_point != undefined then i.pos = int_point.pos
				)--end i loop
			)--end undo
		)--end if
	)--end execute
)--end script
Alen's picture

Heh, I remember now reading

Heh, I remember now reading this code. I wonder what on isEnabled ... on Execute does. I understand its an event of sorts, but what happens if selection count returns false? And what causes those vents to occur?

I'll have to look into this. :D

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.