Selecting Objects in Order
dskeithbuck last edited by dskeithbuck
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
Here's the bug report I wrote up:
doc.SetActiveObject() does not seem to track object selection order.
- Create a new project file.
- Add 3 cubes: A, B, C
- Select them in order: A, B, C
- 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()
- Run this script.
The console prints:
['a', 'b', 'c']
- Run this script again.
BUG: The console again prints:
['a', 'b', 'c']
EXPECTED: The console prints the new reverse selection order.
['c', 'b', 'a']
- active Objects SELECTIONORDER | PluginCafé
- Adding to document selection issue | PluginCafé
- selection order and shift-select | PluginCafé
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()
Please add this information to the
doc.GetActiveObjectssections of the Python and C++ documentation, and mention it as a possible fix for the Shift Select bug which I've encountered previously.
m_adam last edited by
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.