Hey there,
It’s my first post on this site I have a question/in need of some help. I have been wracking my brain trying to figure this one out for a while now.
So I have this tool that lists the stats of objects in the scene. The last column of my tool lists the number of materials you have assigned to the faces of each object. This works fine, however…the way in which I have done it does not allow me to sort them properly.
So how have I done it? Well, first I gather the objects in the scene into an array called mySelectedObjects, then I loop through each object selecting faces based on material ID’s, if the number of faces selected is more than 0 then the material count is increased, if it is 0 then it is not. I do this for however many sub materials are in the multi-material (if any) then I iterate through the two arrays and place them in the listview.
The problem comes when I try to sort by material ID. Sorting by verts, faces etc. is easily done because I just pinch the stats using object.verts.count and then rearrange it that way. But because the material count array is just a set of numbers, I am running into problems. I can qsort the numbers array but I need to be able to sort the objects array with it.
The way I am doing it at the moment is SO SO SO SLOW and isn’t really working for me…I am sorting the material array, then looping through it and then looping throuigh the objects array, counting the materials assigned and if it matches then put it in a temporary array in order and then use that to popuylate the list.
But it is getting very confusing and very slow. There must be an easier way!! Anyway here is the function for this:
Any help/tips would be greatly appreciated, this is the last thing I have to do before the tool is finished and I can release it!
fn sortByMaterial =
(
--Sorts through the (now sorted) master material array and matches objects from the MSO array to the value
-- If a match is found then the object is placed into a new temporary array
-- The object is removed from the MSO array so that duplicates are not placed into the new temp array
-- The MSO array is then remade using the order from the tempArray
local arrayTrack = 1 -- variable to hold the index number of the MSO array object
matId = 1
matNum = 0
count = masterMatArray.count
for i = 1 to count do
(
index = masterMatArray[i]
for j in mySelectedObjects do
(
mat = j.material
if(classof j != Editable_Poly and classof j != Editable_Mesh and classof j != Editable_Patch) then
(
if(mat != undefined) then
(
matNum = 1
)
else
(
matNum = 0
)
arrayTrack += 1
)
else
(
if(classof mat != MultiMaterial) then
(
if(mat != undefined) then
(
matNum = 1
)
else
(
matNum = 0
)
)
else
(
numberOfSubs = mat.numsubs
if(classOf j == Editable_Mesh or classOf j == Editable_Patch) then
(
-- print("Converted " + i as string)
copyOfObj = copy j
convertToPoly copyOfObj
j = copyOfObj
isCopied = true
)
for k = 1 to numberOfSubs do
(
j.selectbymaterial matID
faceArr = getFaceSelection j
--print("face array count is: " + faceArr.numberset as string)
if(faceArr.numberset != 0) then
(
matNum += 1
)
matID += 1
)
)
deselect j.faces
arrayTrack += 1
)
if(matNum == index) then
(
append tempArray j
)
matNum = 0
matID = 1
if(isCopied == true) then
(
delete j
isCopied = false
)
)
)
mySelectedObjects = #()
for l in tempArray do
(
append mySelectedObjects l
)
tempArray = #()
)