ScriptSpot

Forum topic · General Scripting

Passing var value from rollout to macro script

By Alen · 2012-01-10

Description

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 (13)

miauu · 2012-01-11

Post the code.

Alen · 2012-01-12

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

if ((dropDownUI != undefined)

miauu · 2012-01-12


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 · 2012-01-12

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 · 2012-01-12

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 · 2012-01-12

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 · 2012-01-12

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 · 2012-01-12

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

miauu · 2012-01-12

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 · 2012-01-13

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

miauu · 2012-01-13

If isEnable return false On Execute will not be executed.

Just an idea, maybe it will help

Arahnoid · 2012-01-10

Try to declare your variable (in which you store object) as global outside rollout

 
global myObjectVar

--and after rollout
rollout rl_myrolout ...

Alen · 2012-01-10

Unfortunately that didn't make a difference. But I've discovered I don't need to evaluate macro script in script editor; if I run it again using run script, the keyboard shortcut will work...

Does that make sense?