Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush Python 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

    create a cube that disappears when you return?

    Cinema 4D SDK
    4
    10
    1.4k
    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.
    • JH23J
      JH23
      last edited by

      I have a question, this is that every time I use this type of script, when I try to return it stays there, and it is somewhat annoying, I do not know how to fix it, I would be very grateful if you could help me

      import c4d
      from c4d import gui
      def main():
          cube = c4d.BaseObject(c4d.Ocube)
          doc.InsertObject(cube)
          c4d.EventAdd()
      if __name__=='__main__':
          main()
      
      1 Reply Last reply Reply Quote 0
      • M
        m_adam
        last edited by

        Hi @JH23 your question is not clear for use what do you mean that disappears? Do you mean from the scene?

        You are inserting it into the document so the owner is the document.
        If you want to remove an object from the scene you need to call GeListNode.Remove.

        Cheers,
        Maxime.

        MAXON SDK Specialist

        Development Blog, MAXON Registered Developer

        1 Reply Last reply Reply Quote 0
        • JH23J
          JH23
          last edited by

          Hi @m_adam I mean that when creating an Object with BaseObject, when going back it remains in the environment.
          Cheers

          CairynC 1 Reply Last reply Reply Quote 0
          • CairynC
            Cairyn @JH23
            last edited by

            @jh23 I guess you mean undo by "return". But you have no undo functionality in your code at all, so undoing has no effect.

            To support Undo functionality, you need to include the commands StartUndo, EndUndo, and AddUndo, which you can look up in the official API documentation.

            1 Reply Last reply Reply Quote 0
            • JH23J
              JH23
              last edited by

              @Cairyn Thanks, I'll check it

              ferdinandF 1 Reply Last reply Reply Quote 0
              • ferdinandF
                ferdinand @JH23
                last edited by ferdinand

                Hello @JH23,

                thank you @Cairyn for answering this. Here is a brief snippet illustrating the use of the undo logic in Cinema 4D SDK.

                Cheers,
                Ferdinand

                """Example for pushing modifications onto the undo stack.
                
                As discussed in:
                    https://developers.maxon.net/forum/topic/13366
                
                References:
                    [1] https://developers.maxon.net/docs/py/2023_2/modules/
                     c4d.documents/BaseDocument/index.html#BaseDocument.AddUndo
                """
                
                import c4d
                
                def main():
                    """
                    """
                    # Create a cube object.
                    cube = c4d.BaseObject(c4d.Ocube)
                
                    # An undo block can contain multiple modifications, e.g., changing the 
                    # position and rotation of an object. Most undo operations have to be 
                    # invoked before the changes have been carried out on an object, e.g., 
                    # renaming it, changing its position, etc.
                    # 
                    # The only exception to that is the operation for inserting a new node 
                    # into the scene graph, e.g., inserting a cube object into the document.
                    # Here AddUndo has to be called after the operation is being carried out.
                    # 
                    # For details see c4d.documents.BaseDocument.AddUndo()[1].
                    
                    # Start an undo block.
                    doc.StartUndo()
                    # Insert the cube.
                    doc.InsertObject(cube)
                    # Add the undo for inserting the cube.
                    doc.AddUndo(c4d.UNDOTYPE_NEW, cube)
                    # Close the undo block.
                    doc.EndUndo()
                
                    # Notify Cinema 4D of the changes made by us.
                    c4d.EventAdd()
                
                if __name__=='__main__':
                    main()
                

                MAXON SDK Specialist
                developers.maxon.net

                ferdinandF CairynC 2 Replies Last reply Reply Quote 0
                • ferdinandF
                  ferdinand @ferdinand
                  last edited by

                  Hello @JH23,

                  without further questions or postings, we will consider this topic as solved by Wednesday and flag it accordingly.

                  Thank you for your understanding,
                  Ferdinand

                  MAXON SDK Specialist
                  developers.maxon.net

                  1 Reply Last reply Reply Quote 0
                  • CairynC
                    Cairyn @ferdinand
                    last edited by

                    @ferdinand said in create a cube that disappears when you return?:

                    https://developers.maxon.net/forum/topic/13366

                    waitwaitwait...

                    According to the documentation (AddUndo), it's the other way round:

                    Always has to be called before a change is made.
                    In the case of the creation of a new object the call is done afterwards, after insertion into the document/object/track/sequence but before calling subsequent functions like BaseDocument.SetActiveObject() which creates another undo:

                    Also makes more sense that way round, because in a standard case (insertion being the exception), the Undo system has to store the state before the script makes the change, otherwise the original state would already be overwritten.

                    1 Reply Last reply Reply Quote 0
                    • ferdinandF
                      ferdinand
                      last edited by ferdinand

                      Hey @Cairyn,

                      good catch and my bad. I fixed the code above to not cause any confusion. Thank you for taking the time to point it out.

                      Cheers,
                      Ferdinand

                      MAXON SDK Specialist
                      developers.maxon.net

                      1 Reply Last reply Reply Quote 0
                      • ferdinandF
                        ferdinand
                        last edited by

                        Hello @JH23,

                        without any further questions or postings, we will consider this topic as solved by Wednesday and flag it accordingly.

                        Cheers,
                        Ferdinand

                        MAXON SDK Specialist
                        developers.maxon.net

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