Problem with very simple script

AgustinLanus
Contributor
Contributor

Problem with very simple script

AgustinLanus
Contributor
Contributor

So I'm making this very simple UVW script which just applies a box UVW map with an inputted value, here it is in all of it's glory:

v = getKBValue prompt:"Enter object count:"

modPanel.addModToSelection (Uvwmap ())
$.modifiers[#UVW_Map].maptype = 4
$.modifiers[#UVW_Map].length = v
$.modifiers[#UVW_Map].width = v
$.modifiers[#UVW_Map].height = v

This works just fine from the script editor but when I make it a macro script so I can set a hotkey to it I can only use it once and then I get this error:

AgustinLanus_0-1620159934560.png

 

Does anyone know what may be happening?

 

0 Likes
Reply
Accepted solutions (1)
806 Views
7 Replies
Replies (7)

AgustinLanus
Contributor
Contributor

Also if I try to run it with a hotkey on more than one object at the same time I get this error every time:

AgustinLanus_0-1620163516775.png

Which also doesn't appear when I run it from the script editor

0 Likes

denisT.MaxDoctor
Advisor
Advisor

In MXS, a single $ signifies the current selection. In the case where only one node is currently selected, it returns the actual node, but in the case of multiple selection, it returns a set of objects, which in general cannot provide access to the node's parameters (e.g. #modifiers in your particular case).

 

Therefore, it is the good practice to never use the $ character in max scripts, but only use it for quick and dirty typing in the Listener.

Instead of using $ use selection[1], which is always the first node selected, or undefined if nothing is selected.

 

The  code in your case might look as:

 

 

(
	if isvalidnode (n = selection[1]) do
	(
		m = n.modifiers[#UVW_Map]
		if m != undefined do
		(
			m.maptype = 4
			m.length = m.width = m.height = v
		)
	)
)

 

       

denisT.MaxDoctor
Advisor
Advisor
Accepted solution

If you want to add one (the same) instance of the UVW Map modifier to a multiple selection, the correct way to do it is:

 

(
	m = UVWMap maptype:4 length:v width:v height:v

	modPanel.addModToSelection m
	-- or
	addmodifier selection m
)

 

0 Likes

AgustinLanus
Contributor
Contributor

That certainly looks a lot cleaner!

I'm probably missing something really basic as I barely understand maxscript yet but should I be getting "undefined" when I run that code as it is?
I just added v = getKBValue prompt:"Enter object count:" at the top to input the value

0 Likes

AgustinLanus
Contributor
Contributor

Yes! this does the trick to add the modifier to any amount of objects, so that's one down but for some reason I still get get errors when I run it with a hotkey every other use.
What's the correct way to make a script go from the script editor into a hotkey? I use the macroScriptCreator script to turn them into macroscripts which I can then easily access but that seems to be breaking something here.

0 Likes

denisT.MaxDoctor
Advisor
Advisor

I would do something more intuitive and visually more accessible ... Something like:

(
	result = false
	if selection.count > 0 do
	(
		rollout type_a_number_Rollout "UVW size:" width:134
		(
			local value
			
			edittext text_input width:130 pos:[0,5]
			on text_input entered text do
			(
				v = try(text as float) catch()
				if isnumber v then 
				(
					value = v
					destroydialog type_a_number_Rollout 
				)
				else
				(
					text_input.text = ""
					setfocus text_input
				)
			)
			on type_a_number_Rollout open do setfocus text_input
		)
		createdialog type_a_number_Rollout modal:on

		if isnumber (v = type_a_number_Rollout.value) do undo "Add UVW Modifier" on
		(
			uvw_mod = uvwmap width:v length:v height:v
			addmodifier selection  uvw_mod
			
			result = true	
		)
	)
	
	format (if result then "SUCCEEDED!\n" else "FAILED!\n")
	
	result
)



 

AgustinLanus
Contributor
Contributor

I love the idea of having that messagebox appear for the input but sadly for some reason that code isn't compiling for me, after typing a number and pressing enter I just get errors.

0 Likes