MaxScript Beginner - 02 Basics, selection

MaxScript Beginner - 02 Basics

Maxscript Help File

Beginner - 02 Select Object(s)

Getting the current selection the BAD way: To get the current selection in the viewport one CAN (but should not, unless for testing) use the “$” symbol

--This takes the current selection and stores it in a variable
MySelection = $

The Problem with this is…

  1. If you have nothing selected it returns undefined (this is 3dsmax version of null in other languages)

  2. If you have ONE object selected it returns $box001 --the selected object for example

  3. But if you have MORE then one selected you get an array of objects #( $box001, $box002, $sphere003)

So the resulting data type you get when using the $ symbol differs from undefined, node or array. Which makes it complicated to use in a user friendly script.


Getting the current selection the SMART way

--This takes the current selection and creates an array of it..
MySelection = selection as array 

--the important thing is the "as array" command
--it forces the selection to always be an array

The good thing with this is that it ALWAYS returns an ARRAY

  1. If you have nothing selected you get an empty array MySelection = #()

  2. If you have ONE object selected you get a array with one item MySelection = #( $box001 )

  3. If you have MORE then one you get an array of objects MySelection = #( $box001, $box002, $sphere003 )

Since you always get the same datatype (an array) every time it is easier to write the rest of the code since you know that you ALWAYS get an array back.


Select a certain object already in the scene

--if you know exactly the name of the object
--you want to select do:

select $myBox001 --If I have an object called myBox001 in the scene

--if you want to select many objects at the same time
--use an array instead
select #( $object01,$object02,$object03 )

Select all objects that has a name that begins with “box” you can use wildcard selection

--Select all object that has a name that begins with Box
select $Box* 

--Select all objects in the scene
select $* as array

--You can also use object sets..
--It is some predefined categories 
--that is handy to have when you 
--want to edit all of the same type

select objects --selects all objects
select cameras --selects all cameras

/*
objects  --all the objects
geometry  --the standard 3ds Max categories...
lights
cameras
helpers
shapes
systems
spacewarps
selection  --the current selection
*/

Use the 3ds max dynamic ObjectSet array when you need to select all objects from a certain type…

--do print the names of all lights in the scene
for myLight in lights do print myLight.name

--move all helpers aka dummies, up 10 units 
--from where they currently are
for myObj in helpers do myObj.pos += [0,0,10]

Select all objects that has the material with the name “MyGreenMaterial” (this version does not support multisub materials)

--This command collects all object that
--matches material.name == "MyGreenMaterial"

TheObjectsWithGreenMat = for item in objects where item.material.name == "MyGreenMaterial" collect item

--Then to select all objects I use the select command
--and pass in the array we created above

select TheObjectsWithGreenMat

Select the head of the group, and only the topmost grouphead if it is a group in another group…

--how to get the node that is the main group object... so that you can move a group around
--I also check that I am affecting the topmost object.. item.parent == undefined

myNodes = selection as array
TopNodeOfGroupArray = for item in myNodes where (isGroupHead item) == true and item.parent == undefined or isgroupmember item == false collect item

--move all nodes to the center
for MoveNode in TopNodeOfGroupArray do (
	MoveNode.pos = [0,0,0]
)