[Closed] Collect…. where….why…?
for g in geometry where g.material == meditmaterials[1] collect g
print g.count
g is undefined…
Brain’s stopped working… why is this wrong?
It is wrong because a FOR loop creates a local G variable which ceases to exist the moment the loop is done. The ‘print g.count’ is wrong for two reasons – it tries to access the variable g which is local to the loop AFTER the loop has finished, and it tries to get a .count property which g cannot have because g is ONE object per loop iteration, not the collected array returned as result of the for…collect construct.
So the right ways is
theResultArray = for g in geometry where g.material == meditmaterials[1] collect g
print theResultArray.count
or
theResultArray = for g in geometry where g.material == meditmaterials[1] collect g
theResultArray.count
or
(for g in geometry where g.material == meditmaterials[1] collect g).count
in case you don’t intend to USE the objects but just want to know how many there were…
Thanks for this – it helped me too! But on a related query, why does this return an empty collection even though one of the selected objects IS a group head?
bitGroups = for obj in selection where obj == isGroupHead collect obj
If I change == to != then it returns a collection of everything in the selection, so I think the syntax is right.
You are comparing a node with the maxscript primitive, and that comparison is always going to fail.
You want:
bitGroups = for obj in selection where isGroupHead obj collect obj