Notifications
Clear all

[Closed] Detecting selection change

ok i have the following code in a timmer

            myCurrentSelection = selection as array
            if myLastSelection != myCurrentSelection do --selection changed!
            (
                print "selection changed"
                myLastSelection = myCurrentSelection
            )

I would expect this to print “selection changed” only when my selection changes. However according to max even tho they equal it always enters this loop.

Any ideas?

3 Replies
1 Reply
(@bobo)
Joined: 1 year ago

Posts: 0

Comparing two arrays ALWAYS returns FALSE unless we are talking about the SAME array.
== does not compare the CONTENT of the two arrays, it only sees whether the variables you are comparing are pointing at the same array in memory.

So for example


  a = #(1,2,3) --define an array
  b = #(1,2,3) --define the same array once again
  a == b --> false, according to MXS they are NOT the same
  a == a --> true
  b == b --> true
  a = b --assign the one to the other - now both contain a pointer to the same memory
  a == b -->true
  

To check whether two arrays contain the exactly same elements, you would have to do per-element checks yourself by looping through the one array and comparing with the elements of the other. Since the selection as array will contain the objects in the order of selecting, you might want to check them disregarding the order.

For example


myCurrentSelection = selection as array 
  same = if myLastSelection.count != myCurrentSelection.count then --if count is different,
  false --they are obviously different, so we return false to assign to var. 'same'
  else --otherwise we check if each element in the one has a match in the other
  (for i in myCurrentSelection where findItem myLastSelection i == 0 collect i).count == 0
  if not same do 
  (
                       print "selection changed"
                       myLastSelection = myCurrentSelection
  )
  

ok makes sence.

So the selection array actally stores the selection order of the objects?
Is there a way to do this with subobject selections in the same way(ie get the selection order of faces)?

Thanks

1 Reply
(@bobo)
Joined: 1 year ago

Posts: 0

Nope, sub-object selections are generally bitarrays, so the order is always ascending.
Of course, you could keep track of what is being added or removed in the case of SO selections by finding the difference between the two bitarrays (the old and the new selection ones)