SOLVED Python "undo" changes the original selection order

Hello! My script relies on selection order and works fine. however, when I undo and run the script again immediately, the selection order is changed (somewhat reversed), and it toggles the reversal consistently. Why is it doing this? And is there a way to make sure the selection order stays constant when re-invoking the script?

You can see a clearer explanation of my problem:
in this brief screen recording

import c4d
from c4d import gui

def main():


    doc.StartUndo()

    selected = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_SELECTIONORDER)

    #print order of object names to console
    print ("start:")
    names = []
    for obj in selected:
        names.append(obj.GetName())
    print (names)

    #distribute objects
    for idx, ob in enumerate(selected):
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, ob)
        print (ob.GetName())

        ob[c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.VECTOR_X] = 0
        ob[c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.VECTOR_Y] = 0
        ob[c4d.ID_BASEOBJECT_GLOBAL_POSITION,c4d.VECTOR_Z] = 0 + (idx * 1000)


    c4d.EventAdd()
    doc.EndUndo()

if __name__=='__main__':
    main()
    c4d.EventAdd()

Hi,

I've open a bug report for this.

Cheers,
Manuel

@m_magalhaes Excellent thank you! Will you post the results here? Or is there a another link I should follow?

hi,

well, first it needs to be fixed, then release on a cinema4D's update (hotfix, service pack, release)

Cheers,
Manuel

@m_magalhaes ah ok thanks for the clarification. So, at this point, you can confirm that this is a bug and not a mistake in implementation on my part?

yes exact, it's a bug not an error from your implementation.