# Reverse objects order in group

I am trying to reverse the order of the object listed in a group using Python Console. I do the following: 1. Create a group from few selected objects as normal in Blender. 2. In python console I create a list from the objects in the group. 3. I reverse the list 4. Create new group and add the objects from the reversed list. When testing the group in the console the objects seem to be added in reverse order as expected. But after I save the file and reopen it the order of the objects in the "reversed" group was reset to the order of the original group.

So my question is - Is it possible to have two groups with the same objects but arranged differently in both groups?

• As a workaround you could add an ID prop to each group object and use it as a sort key. Give the prop the same name as the group. – batFINGER Nov 23 '15 at 8:27
• I don't quite understand, but will this solve the problem with saving file with two identical groups with different objects arrangement in them? – Rumen Belev Nov 23 '15 at 23:22
• Groups have no ordering. The objects are stored internally in a linked list, so the order in which they are printed appears semi-stable (depending on order of adding to the group), but there is no guarantee whatsoever about any actual ordering in later uses. You can of course use an additional sorting key as described by batFINGER. – lukas_t Nov 24 '15 at 8:36
• The objects in the group have ordering and I think it is the order of the objects creation in the scene, in this case - the last object created will be the first in the group list. And that is not changeable. If you modify the order of the objects through pyhton, after saving and reopening, the order is reset to default. That is why I would like to figure out if it's possible to have two groups with the same objects, but with different orders in the respective group. – Rumen Belev Nov 24 '15 at 10:53

ID property sortkey

Test script to demonstrate adding an ID prop as a sort key. Creates two groups from scene objects "Group1", the second, "Group2", in reverse order and adds a sort key add_key(group)

Saving and reopening the file and group.orderred_list returns "Group2" still in reverse order, as created.

import bpy
context = bpy.context  # for testing

# simple integer key
key = 0
# add an IDProp key for each group object
for o in group.objects:
o[group.name] = key
key += 8

return None

def sorted_group(group):
def key(obj):
# key is groupname prop if set else 0
return obj.get(group.name, 0)

g = [o for o in group.objects]
g.sort(key=key)
return g

bpy.types.Group.orderred_group = property(sorted_group)

# test calls
scene = context.scene

obs = [o for o in scene.objects]

for name in ["Group1", "Group2"]:
print(name)
group = bpy.data.groups.get(name)
if not group:
group = bpy.data.groups.new(name)
if name == "Group2":
obs.reverse()
for o in obs:
elif name == "Group2":
sl = group.orderred_group

• In the console have a look at D.groups["Group2"].orderred_group which is the collection as a list in sort key order, or also the list after "Ordered" is printed. I haven't extended or changed the way the groups objects collection is orderred, which will be inconsistent as lukas_t mentioned in his comment. which is exactly what you are printing ... – batFINGER Nov 24 '15 at 11:36