1
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – batFINGER Nov 23 '15 at 8:27
  • $\begingroup$ I don't quite understand, but will this solve the problem with saving file with two identical groups with different objects arrangement in them? $\endgroup$ – Rumen Belev Nov 23 '15 at 23:22
  • $\begingroup$ 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. $\endgroup$ – lukas_t Nov 24 '15 at 8:36
  • $\begingroup$ 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. $\endgroup$ – Rumen Belev Nov 24 '15 at 10:53
1
$\begingroup$

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

def add_key(group):
    # 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:
            group.objects.link(o)
        add_key(group)
    elif name == "Group2":
        sl = group.orderred_group
        # reorder by unlink link
        for o in sl:
            group.objects.unlink(o)
            group.objects.link(o)

    print(group.objects.keys())
| improve this answer | |
$\endgroup$
  • $\begingroup$ I've tested your script by creating simple scene with few objects in it. Immediately after running the script, I saved the file and reopenned it. Then checked the groups in the Python console with the commands: list(D.groups[0].objects) list(D.groups[1].objects) The print displayed the objects in both groups in the same order i.e. there is no reversed order of the objects in the second group. So I don't think this script works for me. $\endgroup$ – Rumen Belev Nov 24 '15 at 10:57
  • $\begingroup$ 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 ... $\endgroup$ – batFINGER Nov 24 '15 at 11:36
  • $\begingroup$ As a practical use of reversed group, that I have in mind is using those groups as Dupli Group input for particles. Here is an example: youtu.be/PV-TA1EYL8Y How can I use the script that you've proposed to keep the order of the second particle group reversed? $\endgroup$ – Rumen Belev Nov 24 '15 at 14:18
  • $\begingroup$ Use the ordered list to repopulate the group.objects collection (after clearing) would be one suggestion, this could be done in an onload handler. $\endgroup$ – batFINGER Nov 24 '15 at 14:33
  • $\begingroup$ Edited code to preserve order in group2's objects collection, was simply a matter of unlinking and relinking objects in sorted order. Out of interest what difference does it make re the order of group objects in a particle dupli group? $\endgroup$ – batFINGER Nov 25 '15 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.