Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Selecting Objects in Order

    Cinema 4D SDK
    python
    2
    2
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dskeithbuckD
      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 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

      • c4d.documents.BaseDocument — Cinema 4D SDK 21.026 documentation

      Related Posts

      • 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://developers.maxon.net/forum/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

      1 Reply Last reply Reply Quote 1
      • M
        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.
        Thanks again.

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • First post
          Last post