Notifications
Clear all

[Closed] Help to undrestand a snip

Hi

I took this snip from Branko Živković ( gazybara ) but i didn’t undrestand some part of it

    fn setPivotPos mode:#top = if (cnt = selection.count) != 0 do
    (
        if cnt > 1 then (messageBox "Select an object first!" title:"Warning" beep:off) else
        (
            if mode == #center then selection[1].pivot = selection[1].center else
            (
                value = [0,0,0] ; obj = selection[1]
                array = if mode == #bottom then #(4,7) else #(0,3)
                tm = obj.objecttransform ; bb = nodeGetBoundingBox obj tm
                for i = array[1] to array[2] do
                (
                    e = copy bb[2]
                    for k = 1 to 3 where (bit.get i k) do e[k] = bb[1][k]
                    value += (e*tm)/4.
                ) 
                if isKindOf value point3 do obj.pivot = value
            )
        )
    )
------
/*pivot on Top*/      setPivotPos mode:#top    
/*pivot on center*/      setPivotPos mode:#center
/*pivot on bottom*/      setPivotPos mode:#bottom

1_ what are these Arrays for ? ” #(4,7) and #(0,3) “
2_ how does this ” bit.get ” help the script to work like that ?

Thanks in advance

1 Reply

Mmm! The line between clever and convoluted is thin with this one…

  1. As you can see, the ‘array’ is only used for the loop start and end; so it’s just a convenience: you can set both start and end using one if-statement.
    In the case of mode == #bottom, the loop variable ‘i’ will change from 4 to 7, otherwise it will change from 0 to 3.

  2. To understand this one you should really google a bit on how binary integer numbers work.
    The k-loop will only run when the ‘where’ condition is true. For now, just printout and see what the values are when:

format "Case 0 to 3:
"
for i = 0 to 3 do for j = 1 to 3 do format "bit.get % % == %
" i j (bit.get i j) 
format "
Case 4 to 7:
"
for i = 4 to 7 do for j = 1 to 3 do format "bit.get % % == %
" i j (bit.get i j) 

The whole bit.get line replaces certain parts of the copied bounding box high value (bb[2]) with the values from bounding box low value (bb[1]). Afterwards that value is used in calculating an average.
The only difference between #bottom and not is that in #bottom case (4 to 7) the .z part of the vector is always set to that of the bounding box low .z value.
Why not just calculate the average the same way and only replace .z of the calculated average in the #bottom case? No reason. Tunnel vision happens to the best of us.