In this tutorial, I am going to show you how to use arrays in 3ds max MaxScript. We will be discussing some of its most common properties and functions with proper examples. Its a complete and easy guide for beginners to understand arrays. Lets get started.
What is an Array?
An array is a collection of values. Unlike simple variables, we can save multiple values in an array. You can save any type of value in MaxScript array.
With the help of an array we can manipulate multiple objects simultaneously without using much lines of code.
Creating an array in MaxScript
You can create an array by writing the variable name you want for the array equals hash followed by open and close parenthesis.
Array_1 = #()
Currently this array doesn’t have any value stored in it.
Array_2 =#(1,2,3,$Box001,[5,5,5])
An array can also have sub-arrays inside it.
Array_3 = #(1,2,3,#(4,5,6))
Here, Array_1 is an empty array and Array_2 has 5 values or items stored in it, and Array_3 contains a sub-array #(4,5,6).
How to get the number of items in an Array?
You can output the number of values stored in an array using .count property.
For the array named My_Array, we should write – My_Array.count to get the item count of the array. We can also set the number of items of an array.
My_Array = #(5,10,15,20,25) My_Array.count = 2
Since, we have reduced the number of array items to 2, the resulting array would be #(5,10)
How to add values to an existing array?
We can add any type of value to an array by simply writing it inside the array but this doesn’t work every time.
A better and quicker way to input values into an array is by using the Append function.
Write down append followed by the name of the array and the value you want to add.
MyArray = #() --creates an empty array. append MyArray 10 – add 10 to the existing array.
The resulting array would be: #(10)
To add multiple values at a time you can use Join function.
MyArray = #() Join MyArray #(1,2,3)
Resulting array: #(1,2,3)
Note: If we use append instead of join then it will append it as a whole array creating a sub-array inside MyArray
.
Using ‘Insert Item’ method to add values in the array
You can insert any value at specific index through InsertItem. Here is the example:
MyArray = #(1,2,3,4) Insertitem 33 MyArray 3
Resulting Array: #(1,2,33,3,4)
In the above example, 33 has been added to the array at position 3.
Appending values based on uniqueness
You can also append values to an array based on their uniqueness. The value will be added only if it is unique to the array but if array already contains that value then the value will be ignored. This is a good way to prevent any value to be added multiple times in the array.
Deleting items from an array
You can delete any item from an array through DeleteItem function. Let’s say we want to remove the 4th item in the given array.
MyArray = #(10,20,30,40) DeleteItem MyArray 4
Where Arr_1
is the name of the array and 4 is the index of the item that we want to remove.
Note: You don’t have to write the values sequentially. You can arrange the values of an array in ascending order through sort
.
For eg.:
a = #(2,4,5,1,3) sort a
Resulting array would be: #(1,2,3,4,5)
Creating array based on selection
If you want to create an array from the selected objects, you can do it by using selection as array command. Apart from the selection, you can also get other objects such as geometry, cameras, lights etc. into the collection.
Here, I have 3 boxes, 3 cylinders and 3 teapots. If I write,
a = $Box*** as array -- creates an array of Boxes select a -- selects the objects in array a
3ds max will select boxes named Box001, Box002 and Box003. Please keep in mind that this command selects objects according to their name so if a sphere is named as Box005 then it will also get selected regardless of its class.
Manipulating the objects stored in an array
We can also change any other property of the objects such as height, radius, visibility etc.
If you want to change the height of the objects in an array named MyArray
then you can do it through:
MyArray.height = 20
This changes the height of all the three items in the array but you can also make changes to the array items individually. All you need to do is writing the index number of the item that you want to change inside square brackets next to the name of the array.
MyArray[3].height = 20
The above given line will change the height of the third item present in the array.
Using For loop to manipulate objects of an array
We can also use For
loop to make changes to the items of an array. I have 10 boxes of height 10 in the scene for this example.
With the help of For loop, we will iterate i from 1 to the number of items present in the array or array.count which is 10 and modify the height of the boxes.
MyArray = $Box*** as array for i = 1 to MyArray.count do ( MyArray[i].height = i^2 )
So, let’s say if i = 4 then the height of the item [4] would be, 4^2 = 16.
Eliminating duplicate values from an array
If you have same value more than once in an array then you can get a unique array through makeuniquearray method.
MyArray= #(1,1,2,2,3,3) MakeUniqueArray MyArray
Resulting Array: #(1,2,3)
Creating copy of an array
Now, let’s learn how to copy array values from one to another. There are few ways that can be used to copy array values.
FirstArray = #(1,2,3) SecondArray = First_Array
If we print the contents of the SecondArray the output would be #(1,2,3)
But the problem in using this is that whenever we make any change to SecondArray, FirstArray will get that change too and vice versa. It’s similar to creating an instance of an object in 3ds max.
Using Copy method to duplicate the arrays
Another way of copying array values is with copy function.
One = #(1,2,3,#(8,9,10)) Two = Copy One #NoMap
Here, we have an array named One with some values and a sub array. The second line of code creates another array by copying the values of array One inside Two.
The copy created by this method is called shallow copy as it copies the top level or upper level values only.
Things to be noticed while using shallow copy of an array
Let’s make some change to the array Two
and see what happens.
One = #(1,2,3,#(8,9,10)) Two = Copy One #NoMap Two[1] = 50 Format “Array One contains %\n” One Format “Array Two contains %\n” Two
As you can see that in array Two
, the value at position [1] has been changed from 2 to 50 while there is no change in array One
.
Let’s try one more time but this time, we will change the sub-array and notice the change.
Two[4][1] = 50 Format “Array One contains %\n” One Format “Array Two contains %\n” Two
Notice that the sub-array has been changed from #(8,9,10) to #(50,9,10) and this time this change takes place in both the arrays as it’s a case of shallow copy.
Using Deep Copy for duplicating array
You can avoid the above condition by using DeepCopy function instead of Copy
.
One = #(1,2,3,#(8,9,10)) Two = DeepCopy One
With DeepCopy, you can create copy of an array without referencing or connection between the array values.
How to search inside an array?
If you want to check if a certain value has been added to the array you can do it easily through finditem
. It’s just like searching a value inside an array.
MyArray = #(10,20,30,40,50) finditem MyArray 20
finditem
will check for the value inside the array and return the index of the value. Since, 20 is already in there, it will return 2 in the Maxscript Listener.
How to Empty an array
Free allows you to free the memory consumed by values stored in the array. Here is the example.
MyArray = #(5,10,15,20,25) Free MyArray
Here is an alternate method.
MyArray = #(5,10,15,20,25) MyArray.count = 0
I hope you enjoyed this tutorial and learned something new from it. I will see you next time with some more tutorials.