Notifications
Clear all

[Closed] Maxscript Help

I’m new to MaxScript and I’m getting an error to a very simple sample of code.

for obj in $ do
(
move obj [0,-10,0]
)

– Syntax error: at ), expected <factor>
– In line: )

Can someone help me out here? I have a sphere selected.

6 Replies
2 Replies
(@bobo)
Joined: 11 months ago

Posts: 0

If you have a Sphere selected, it will not work anyway – $ represents a single object OR an array of objects depending on what is selected, so with a single sphere selected, FOR would not be valid (–no map for $Sphere01… would be the error).

Where did you type your code? I can get your error if I type in the Listener and press Enter after the ). This might work in Maya, but in Max you have to highlight the whole code and hit the numeric Enter, or better, open the awesome editor (MAXScript>New Script) and enter the code there, then press Ctrl+E to evaluate it all.

(@scryedzxp)
Joined: 10 months ago

Posts: 0

Thank you. I was getting the error when pressing Enter after the ) in the Listener. Why would I have to select that entire block of code for it to work?

Also, could you further elaborate on the first paragraph? Do you mean that FOR operates on multiple objects only and not only on one?

as you have it typed, the script works fine for me.

try formatting it so the entire expression is on one line & see if it works. if so, you had some weird typo somewhere.

for obj in $ do (move obj [0,-10,0])

Best to use getCurrentSelection() to save the user’s selection instead of ever using $


objSelected = getCurrentSelection()
for o in objSelected do (
	 move o [0,-10,0]
)

Pressing the Numeric Enter (or SHIFT+ENTER) in the Listener OR in the MAXScript Editor always evaluates the CURRENT LINE. This is because it was designed to work that way If you want to execute multiple lines, select them and press Numeric Enter or SHIFT+ENTER, so MAXScript knows what you want. If you want to evaluate ALL the lines, press Ctrl+E in the MAXScript Editor.

When you press the main ENTER key after each line, the line is evaluated but the code block is not executed immediately, so typing in the Listener

for o in getCurrentSelection() do [ENTER]
( [ENTER]
print o.name [ENTER]
) [ENTER]
“Sphere01”
OK

will evaluate automatically the FOR loop the moment you close the last bracket.
Hitting SHITF+ENTER or Numeric Enter after the ) itself will evaluate only the line ) and since it is not a full expression, you will get
– Syntax error: at ), expected <factor>
– In line: )
But if you type in
()
and press ENTER, SHIFT+ENTER or Numeric Enter, you will simply get ‘undefined’ and no error because () is a valid expression.

As for the FOR loop, it operates on COLLECTIONS of zero, one and more objects.
Unfortunately, the $ symbol does NOT represent a collection when a single object is selected. The $ symbol MORPHS its type based on how many objects are selected:

–Deselect all and evaluate:
$
undefined
–The result is undefined, which is VOID, nothing.

–Select one Teapot and evaluate:
$
$Teapot:Teapot01 @ [-4.150664,0.403866,0.000000]
–The result is a reference pointing at a single object

–Select two or more objects and evaluate:
$
$selection
–The result is the $selection system object set representing the current selection

–Try to print the selection:
print $
$Teapot:Teapot01 @ [-4.150664,0.403866,0.000000]
$Plane:Plane01 @ [5.925209,0.783644,0.000000]
OK

–Now compare the above with the call getCurrentSelection()
getCurrentSelection()
#()

getCurrentSelection()
#($Teapot:Teapot01 @ [-4.150664,0.403866,0.000000])

getCurrentSelection()
#($Teapot:Teapot01 @ [-4.150664,0.403866,0.000000], $Plane:Plane01 @ [5.925209,0.783644,0.000000])

As you can see, getCurrentSelection() ALWAYS returns an array (with 0, 1 or more elements) which is a SNAPSHOT of the current selection. Once called that snapshot does not change dynamically.

For example, select two objects and call

theSel1 = $
$selection
theSel2 = GetCurrentSelection()
#($Teapot:Teapot01 @ [-4.150664,0.403866,0.000000], $Plane:Plane01 @ [5.925209,0.783644,0.000000])

If you would change the selection now to select a third object, a Sphere01, and then try to print theSel1 and theSel2, the results will be DIFFERENT:

theSel1
$selection
print theSel1
$Sphere:Sphere01 @ [-179.996521,-53.775692,0.000000]
OK
theSel2
#($Teapot:Teapot01 @ [-4.150664,0.403866,0.000000], $Plane:Plane01 @ [5.925209,0.783644,0.000000])

As you can see, the variable theSel1 holds a reference to the symbolic path $selection which is dynamically updated to the CURRENT selection, while the variable theSel2 still holds a SNAPSHOT of the selection when the function getCurrentSelection() was called and does NOT update dynamically.

Thus, the symbol $ should be used ONLY when typing quick command lines operating on the current selection when you are sure what the content of the selection is, or in event handlers that have special handling for the selection count already (for example in an extended macroScript where the isEnabled() handler has already checked the selection count and the on Execute do() handler can assume what is in $. But in general it is not a good idea to use $.

The FOR loop should only be used with COLLECTIONS (either a system object set like $selection, or an array). If you try to call FOR on 'undefined' or a single node, you will get the --no map for... error.

Wow, what a nice info, Bobo! This has always been a mystery to me, now I understand. It’s always great reading your posts. Thank you!