BitArray - Count up?

BitArray - Count up?

Anonymous
Not applicable
876 Views
10 Replies
Message 1 of 11

BitArray - Count up?

Anonymous
Not applicable

theBits = #{2,3,4,5,6,7}
theArr = theBits as array
newBits = #{}
--Increase each value in theArr by +1 which results in
newBits = #{3,4,5,6,7,8}


I want to take a bitArray and count up and then apply those values to a new bitArray
0 Likes
877 Views
10 Replies
Replies (10)
Message 2 of 11

Steve_Curley
Mentor
Mentor
I have to ask - what are you trying to achieve here? A bitArray is an array of true/false values where the index of the value is (generally) more important than the value at that index, whereas a normal array is just a list of values where the value at a specific index is (generally) more important than the index which points to it.

All your code will result in is a bitArray where index 2 is false (where it was originally true) and index 8 (which didn't previously exist) is true.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 3 of 11

Anonymous
Not applicable
Using maxscript I've made a multilistview and I want the selection to update when the users hit the Up arrow. As of right now it does not move the selection with the change.
So the question was to help solve that problem.


You can test it out to better understand what I mean. As for the down arrow that has not been updated yet. So just test using the up arrow.
Thanks Steve.


(
local AnimObjs = #()

rollout rlD ""
(
multiListBox lbxAnimatedObjects "Animated Objects" pos: width:120 height:12
button btnAddAnimObj "+" pos: width:24 height:24
button btnRemoveAnimObjs "-" pos: width:24 height:24
button btnClearAnimObjs "X" pos: width:24 height:24
button btnMoveUpAnimObj"^" pos: width:24 height:24
button btnMoveDownAnimObj "v" pos: width:24 height:24

on lbxAnimatedObjects rightclick do lbxAnimatedObjects.selection = #{}

on btnAddAnimObj pressed do -- Add Objects
(
userSel = getCurrentSelection()
if userSel.count >= 1 do
(
for o in userSel do (appendIfUnique AnimObjs o)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)

on btnRemoveAnimObjs pressed do -- Removes Object fromt list
(
local currSel = lbxAnimatedObjects.selection
for i = lbxAnimatedObjects.items.count to 1 by -1 where currSel do (deleteItem AnimObjs i)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)

on btnMoveUpAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection as array
if itm.count == 0 then print "Nothing is selected" else
(
for i = 1 to itm.count do
(
itmNum = itm as integer
swap AnimObjs AnimObjs
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
)
)
)

on btnMoveDownAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection
if itm < lbxAnimatedObjects.items.count do
(
swap lbxAnimatedObjects.items lbxAnimatedObjects.items
lbxAnimatedObjects.items = lbxAnimatedObjects.items
swap AnimObjs AnimObjs
lbxAnimatedObjects.selection += 1
)
print AnimObjs
)

on btnClearAnimObjs pressed do lbxAnimatedObjects.items = (AnimObjs=#()) -- Clear List
)
createDialog rlD 140 225
)
0 Likes
Message 4 of 11

Steve_Curley
Mentor
Mentor
You like opening cans of worms, don't you 😛

This is a minefield, because you have to try and account for every stupid thing a user may try and do - and believe me they will.
For example, select only the top item and click "Up"... (maxscript exception)
Select the top 2 items. Top one can't move up because there's nowhere for it to go. Second one could, but that would move the top one down, so the 2nd one down can only move up if the top one is not selected, otherwise it can't move either.

I think that maybe a different approach is called for here - I'll have a play over the weekend, bit busy today.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 5 of 11

Swordslayer
Advisor
Advisor
This could get you started:

fn changeIndex i count dir =
(
if (i == 1 AND dir == -1) then count
else if (i == count AND dir == 1) then 1
else i + dir
)

on btnMoveUpAnimObj pressed do
(
local selectedItems = lbxAnimatedObjects.selection
local newSelection = #{} -- our new selection
local newItems = #()
newItems = undefined -- fill the array with undefined vals

if selectedItems.isEmpty then messageBox "Nothing is selected"
else
(
for oldIndex in selectedItems do
(
local newIndex = changeIndex oldIndex selectedItems.count -1
newItems = AnimObjs -- put object to its new location
newSelection = true -- this item will be selected
)

nonSelectedItems = -selectedItems -- get inverse of the selection

for i in nonSelectedItems do
newItems = AnimObjs -- replace empty items

AnimObjs = newItems

lbxAnimatedObjects.items = for i in AnimObjs collect i.name
lbxAnimatedObjects.selection = newSelection
)
)


It's a bit rough around the edges as it's more a proof of a concept (it would make more sense to make a function of it to be used also in the btnMoveDownAnimObj pressed event) but it works. Basically it goes through selected items and puts them in new positions in an array and then it fills the empty spaces in the array with leftover items. The changeIndex function can be used to change indices in the other direction as well, just pass it 1 as dir. The tompost item will move to the end of the list if selected, which to me feels quite natural (and makes moving things down easier than having to move them down the whole list).
0 Likes
Message 6 of 11

Steve_Curley
Mentor
Mentor
This, I think, does what you want, though as always with such things you need to test carefully, with lots of different selections, that it continues to do what is expected of it and that you can't "crash" it no matter what you do.

on btnMoveUpAnimObj pressed do
(
local itm = lbxAnimatedObjects.selection

if itm.count == 0 then print "Nothing is selected" else
(
for t = 2 to itm.count do
(
if (itm and not itm) then
(
swap animObjs animObjs
deleteItem itm t
append itm (t-1)
)
)
lbxAnimatedObjects.items = for i in AnimObjs collect i.name -- update list with array
lbxAnimatedObjects.selection = itm
)
)

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 7 of 11

Anonymous
Not applicable
Thank you Steve for helping out on this and coming up with a solution. I'll be sure to test this out and see what it does. I wish it was a simpler thing to do by default. Oh well I guess.
Great stuff.
Thanks
John
0 Likes
Message 8 of 11

Steve_Curley
Mentor
Mentor
Just realised there is a potential problem if there is only 1 item in the list (can't really go anywhere, can it?) - the loop starts from index 2 which wouldn't exist - script error...

I would probably add an "on <listbox> selected" event which tested both the number of items in the list and the number selected. If there's nothing selected, or the selection count = the item count (which covers the case of there being only 1 item), then disable the up/down buttons, else enable them. This would remove the need for the "if itm.count == 0" test because the button can't be clicked in the first place.

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 9 of 11

Steve_Curley
Mentor
Mentor
Latest version - includes the btnDown processing and the enabling/disabling of the up/down buttons.

updownrollout01.zip


Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes
Message 10 of 11

Anonymous
Not applicable
Great. This could work better. Thank you Steve for all the time you spent developing this and getting it to work. I would have never expected it to be such as much of a task as it was. I will greatly help what I am working on. Thanks again for the help.
I'll be sure to let you know how things go with it.

Thanks
JokerMartini
0 Likes
Message 11 of 11

Steve_Curley
Mentor
Mentor
Please do - always nice to see the finished result of something to which you have contributed, even in a small way 🙂

Max 2016 (SP1/EXT1)
Win7Pro x64 (SP1). i5-3570K @ 4.4GHz, 8Gb Ram, DX11.
nVidia GTX760 (2GB) (Driver 430.86).

0 Likes