Selecting Objects in Order



  • I encountered what I believed was a bug in doc.SetActiveObjects(object, c4d.SELECTION_ADD). The objects that I selected would return a different order than I expected when I used doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_SELECTIONORDER|c4d.GETACTIVEOBJECTFLAGS_CHILDREN).


    Here's the bug report I wrote up:

    Issue

    doc.SetActiveObject() does not seem to track object selection order.

    To Reproduce

    1. Create a new project file.
    2. Add 3 cubes: A, B, C
    3. Select them in order: A, B, C
    4. Create a new script, and paste this in:
    """Name-en-US: Print and Reverse Selection
    Description-en-US: Prints the names of all objects in the order they were selected. Then attempts to reverse selection order.
    """
    
    import c4d
    from c4d import gui
    
    def main():
        # Get Active Objects
        active_objects = doc.GetActiveObjects(c4d.GETACTIVEOBJECTFLAGS_SELECTIONORDER|c4d.GETACTIVEOBJECTFLAGS_CHILDREN)
        if not active_objects:
            return
        
        # Collect the names of all objects, deselect as you go.
        names = []
        for obj in active_objects:
            names.append(obj.GetName())
            doc.SetActiveObject(obj, c4d.SELECTION_SUB)
        
        # Print the name of the objects based on their initial selection order.
        print names
        
        # Reselect the objects in reverse order
        for obj in reversed(active_objects):
            doc.SetActiveObject(obj, c4d.SELECTION_ADD)
        
        # Let C4D know something has changed
        c4d.EventAdd()
    
    if __name__=='__main__':
        main()
    
    1. Run this script.

    The console prints: ['a', 'b', 'c']

    1. Run this script again.

    BUG: The console again prints: ['a', 'b', 'c']

    EXPECTED: The console prints the new reverse selection order. ['c', 'b', 'a']

    Reference

    Documentation

    Related Posts


    Turns out, it isn't a bug, but is instead a documentation issue. By calling doc.GetActiveObject() after each selection (as described here) you can update the selection caches and the selection order will appropriately update.

        # Reselect the objects in reverse order
        for obj in reversed(active_objects):
            doc.SetActiveObject(obj, c4d.SELECTION_ADD)
            
            # Update selection caches, needed if you want to use SELECTIONORDER
            # Reference: https://plugincafe.maxon.net/topic/9307/12387_adding-to-document-selection-issue/3
            doc.GetActiveObject()
    

    Request

    Please add this information to the doc.SetActiveObject, doc.SetSelection, and doc.GetActiveObjects sections of the Python and C++ documentation, and mention it as a possible fix for the Shift Select bug which I've encountered previously.

    Thank you,

    Donovan



  • Hi @dskeithbuck thanks for reaching us.

    In fact, it's written in the C++ BaseDocument - Selection Manual but I agree maybe a note in the function will be welcome.

    IN any case, I will add a note for the python doc.
    Thanks again.

    Cheers,
    Maxime.