MAXScript FAQ > Accessing Object Properties > How do I Sort a MultiMaterial By Material ID? |
Since 3ds Max 4, material IDs can be specified independently from the sub-material order inside a MultMaterial. The material IDs can get messy after a long process of adding sub-materials. Especially when giving a scene to other people, it might be a good idea to reorder the sub-materials by sorting the Material IDs in ascending order for easier access.
The following function performs this operation using a simple bubble sort algorithm without using the built-in sort or qsort functions provided by MAXScript.
A bubble sort algorithm compares two values and switches their places when their order is not right. It repeats this process until no changes are required anymore. The name of the method is based on the fact that entries with low values tend to float up "to the surface" while higher values fall down "to the bottom".
The function sets a flag variable and repeats a while loop until the flag is set to true. The flag will be set to false whenever two materials have switched places denoting that the sorting is not over yet. Inside the while loop, a for loop will run through the sub-materials and compare pairs of neighbor Material ID values. When the current value is higher than the following one, then their IDs, the respective sub-materials, the names and the enable switches have to change places.