# Any way to have multiple ID masks?

It is possible to have multiple objects share the same ID mask in the compositor. It does not seem possible to have objects possess multiple ID mask values though. Is there any way around this? I want to use ID masks somewhat like groups, and for some objects to be in multiple ones.

• The not very flexible way would be to add masks created by Object ID pass to each other, subtract them as needed with Math node. This is more like a workaround, of course and will increase the node tree, although this should be easy. – Mr Zak Dec 17 '16 at 16:06

You could use a 'binary encoding' technique to allow you to assign each object to a limited number of groups.

To achieve this, allocate each group a power of 2 :

Group 1 = 2^0 = 1
Group 2 = 2^1 = 2
Group 3 = 2^2 = 4
Group 4 = 2^3 = 8
Group 5 = 2^4 = 16
...etc.


You can combine these 'groups' by simply adding them together :

Groups(1,3,4) = 1 + 4 + 8 = 13

So setting the ID to 13 would indicate that that object is in groups 1, 3 and 4.

So '0' is in no groups, '1' is in Group 1, '2' is in Group 2, '3' is in Group 1 and Group 2, '4' is in Group 3, '5' in Group 3 and Group 1, etc.

ie, in binary and each column representing a group (with the first being the right-most digit)

000000000000000 = 0
000000000000001 = 1
000000000000010 = 2
000000000000011 = 3
000000000000100 = 4
000000000000101 = 5
000000000000110 = 6
000000000000111 = 7
000000000001000 = 8
000000000001001 = 9
000000000001010 = 10
...etc....
111111111111110 = 32766
111111111111111 = 32767


Once you've assigned the IDs using the above scheme, you can use Math compositor nodes to determine which groups any particular ID represents by splitting off each binary digit in turn using Modulo 2 (which will give the value of the rightmost digit), subtracting the Modulo 2 to remove that digit from the result, and divide by 2 to shuffle the remaining digits to the right - then repeat for the next group and the next, etc. This can be repeated until you've split off all of the groups/digits into individual outputs to indicate the grouping.

The above image shows splitting to give 3 group outputs. Simply duplicate each block of three nodes (Modulo, Subtract, Divide) and continue the chain linked to the previous one in the same way.

It's quite cumbersome but can work for up to 15 individual groups (the ID has an upper limit of 32767 which is 111111111111111 in binary (15 digits)) but any object can be assigned to any or all of the groups as required.