Notifications
Clear all

[Closed] Problem to filter a selection

Hi,

I’m trying to filter a selection but i’m facing a strange problem :

When using this code…

for i in selection where superclassof i == light do deselect i

or this one

for i in selection where superclassof i != geometryclass do deselect i

or even

for i in selection where iskindof i light do deselect i

… I have to run it multiple times to achive the result. I’ve tried it directly in the listener with 3 spheres and some lights and the first time i run it it deselects some lights, the 2nd time its deselects some other lights ans so on…

Do someone have an idee ?

6 Replies
1 Reply
(@shibumenon)
Joined: 11 months ago

Posts: 0

hmmm … quite an interesting one this is !
And I guess, this is the right situation to evaluate the difference between
selection and selection as array

'selection' is kind of dynamic, so if I say 
var1 = selection
then, var1 becomes a dynamic array, and it'll always contain nodes that are currently

selected

but if you say var1 = selection as array
then var1 will always contain the same nodes which were selected when you executed
the line.

To understand why this happens, you could try this :
An fresh, empty scene, make 3 nodes : a sphere, and 2 omni lights

Now type the same code that you started with … ie

     for i in selection where superclassof i == light do deselect i
CASE 1 : you select omni1, sphere, omni2 .... (in this order) and evaluate your script

for loop 1st iteration : omni1, sphere, omni2     
checks the 1st element, (omni1) ..... expression true, so deselects it

for loop 2nd iteration : sphere, omni2        
checks the 2nd element, (omni2) ..... expression true, so deselects it

for loop 3rd iteration : sphere            
ideally, the for loop doesn't have a 3rd element this time to check, 
so doesn't evaluate the where expression.
But since the object remaining in the selection is a sphere, your purpose is solved.
--------------------------------------------------------------------------------


CASE 2 : you select sphere, omni1, omni2 ... (in this order) and evaluate your script

for loop 1st iteration : sphere, omni1, omni2     
checks the 1st element, (sphere) ..... expression false, so doesn't deselect anything

for loop 2nd iteration : sphere, omni1, omni2    
checks the 2nd element (omni1) ..... expression true, so deselects it

for loop 3rd iteration : sphere, omni2        
no 3rd element to check, so you are left with a sphere and omni selected in scene, 

though omni2 is a light !

--------------------------------------------------------------------------------hmmm 

So the different results that you obtained should be because of the order of selection,
where the selection keeps dynamically changing.
But instead of ‘selection’ , if you use selection as array, you will surely get the result that you were looking for.

  for i in (selection as array) where superclassof i == light do deselect i
selectArray = for i in selection where superclassof i != light collect i
 select selectArray

Thanks, that do the trick

But i still wondder why the others code didn’t work…mostly because of the deselect() i used, but i don’t know why.

It’s not deselect, it’s because the seletion array changes thru each iteration of the loop, it works if you collect the elements in a seperate array then run the code:

newArray = for i in selection collect i
 for i in newArray where superclassof i == light do deselect i

Got it, thanks again

The demonstration is obvious…

Thanks we learn every day here !