Notifications
Clear all

[Closed] Learning MaxScript – Move Selected to Origin

Hi all, just getting started in MaxScript 2009. Can someone please provide me with an example script that would take all of the selected mesh items in a scene (those selected only), and move them say to 0,0,0? That would be helpful for me to see some basic commands and how the logic is done (if…then…else) kind of stuff. Thank you kindly for any help you are able to provide, or suggestions!

8 Replies

No one…not even suggestions…?

Have a look at my blog below…

for i in selection do i.pos = [0,0,0]

Simples!

For command line use, you can go even simpler (but I do not recommend it for actual programming, just for quick Listener typing):

$.pos = [0,0,0]

If you want to bring the meshes to the world origin and also reset their rotation and scale, you can say

$.transform = matrix3 1

‘$’ is a the equivalent of ‘selection’, but it behaves differently depending on whether the selection is a single object or multiple objects.

Also note that you can use ‘$’ to filter objects by name pattern, for example if you want to affect all objects called “Box*” in the scene, you can say

$Box*.pos = [0,0,0]

This is the same as

for o in $Box* do o.pos = [0,0,0]

but the loop is performed implicitly (internally) in the first case, and explicitly via the ‘for … do’ expression in the latter case.

These two have different behavior though if you want to assign a random value to your objects’ position. For example, if you say

$.pos = random [-100, -100, -100] [100, 100, 100]

the right-hand side of the = assignment will be evaluated once, then the result will be assigned to all selected objects, resulting to all of them going to the same random point somewhere between the two coordinates.

But if you say

for o in selection do o.pos = random [-100, -100, -100] [100, 100, 100]

then each of the selected objects will get its own different random position between the two coordinates…

All this is of course covered in the documentation

This also works:


selection.pos = [0,0,0]

also you can use selection instead of $ (it works in case of empty selection as well)

selection.pos = [0,0,0]
 selection.transform = matrix3 1

but of course it cannot guaranty that all objects will be set to Zero. some objects might have locked controller or not settable controller (constraint, script, expression, etc.)

Wow, talk about a good response rate now, thanks very much to everyone who took their time out to post and reply!

I had no idea I had received so many replies, but in the meantime did my own homework and came up with this below. Note that I decided I wanted to retain the original object’s z-axis setting, and believe this is the correct way to do it. Your helpful responses will certainly allow me to learn more about scripting, thanks again for your time and any further suggestions.

For objectselected in $Selection do
objectselected.position=[0,0,objectselected.pos.z]