Solved create a cube that disappears when you return?

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()

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://plugincafe.maxon.net/topic/13366

References:
    [1] https://developers.maxon.net/docs/Cinema4DPythonSDK/html/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

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.

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

@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.

@Cairyn Thanks, I'll check it

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://plugincafe.maxon.net/topic/13366

References:
    [1] https://developers.maxon.net/docs/Cinema4DPythonSDK/html/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

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

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

https://plugincafe.maxon.net/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.

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

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