[Closed] How to check a class of selection?
You can use a for loop and check each object in the selection.
How you handle the results though is up to you, here is one way to do it:
(for o in selection where classof o == Box collect o).count == selection.count
This loops through all selected objects and collect those that match the desired class (in this case Box). If the number of objects collected is equal to the number of objects selected, then all objects are of that class and you get true, otherwise you get false.
Keep in mind that this will returns true if NOTHING is selected because 0 == 0, so you might want to add ‘selection.count > 0 AND ’ in front of it…
There are also other ways to do the same, for example
result = selection.count > 0
for o in selection while result where classof o != Box do result = false
result
We initialize the return value to true if anything is selected, or to false of selection is empty. Then we loop through all selected objects and while the result variable is true, check the class of the objects and if not Box (or whatever), we set the result variable to false. This will exit the for loop prematurely and return false. If nothing was selected, the loop won’t even loop once. If all objects match the class, the for loop will go through all objects and never set result to false, thus returning true in the end.
Thus, with many thousands of objects, this approach can be faster since it won’t loop through all objects if it encounters even one object of a different class.
But for elegance, I would pick the the first approach…
thank you very very much ,I kept searching for properties of arrays to use but I never considered count, I don’t know why:blush:.