(SOLVED) Set a button strip parm in python

   443   2   2
User Avatar
Member
33 posts
Joined: Dec. 2014
Offline
Hello,

I know how to convert a button strip bitfield to an array but I have no clue how to do the opposite.

So for example I have a button strip with 3 menu items (x, y, z), I have a list of (1, 0, 1). How can I convert that array to a bitfield to set the parm python expression so that the first and last menu item is checked by returning an integer?
Edited by JohnDoe777 - March 10, 2024 11:26:54
User Avatar
Member
209 posts
Joined: Jan. 2013
Offline
Not the most efficient, but the easiest way to do it.

int("".join(str(x) for x in [1,0,1]), 2)
User Avatar
Member
143 posts
Joined: May 2017
Offline
You can also iterate through the list, each time an element is true, calculate the binary number with the corresponding index and sum it, the final result is the bitfield.
def list_to_bitfield(list):
    bitfield = 0
    for i in range(len(list)):
        if list[i]:
            bitfield += (1 << i)
    return bitfield
  • Quick Links