Jump to content

How to deal with "AddUndo"?


kweso
Go to solution Solved by Fritz,

Recommended Posts

Hey all!

I hope this is the correct section to post this question.

I built myself a little script that allows me to boole multiple objects in one go:

 

import c4d
from c4d import gui
def main():
    doc = c4d.documents.GetActiveDocument()
    doc.StartUndo() #Don't know if this works like this at all
    selected = doc.GetActiveObjects(1)
    while len(selected) > 1:
        newBool = c4d.BaseObject(c4d.Oboole)
        doc.AddUndo(c4d.UNDOTYPE_NEW, newBool)
        doc.InsertObject(newBool)
        newBool[c4d.BOOLEOBJECT_SINGLE_OBJECT] = True
        newBool[c4d.BOOLEOBJECT_TYPE] = 0

        tmpA = selected.pop(0) 
        tmpA.InsertUnder(newBool)
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tmpA) # ???
        tmpB = selected.pop(0)
        tmpB.InsertUnder(newBool)
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tmpB) # ???
        doc.SetActiveObject(newBool)
        c4d.CallCommand(12236)
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, newBool) # ???
        newerBool = doc.GetObjects()[0]
        selected.insert(0, newerBool.GetDown())

    c4d.EventAdd(c4d.EVENT_FORCEREDRAW)
    doc.EndUndo();


if __name__=='__main__':
    main()

 

Doing this, I thought it would be time to learn about "AddUndo". But it does not work like I thought it would. 

Would someone be able to point me in the right direction?

 

WARNING: The script like it is now CHRASHES my c4d... If you'd like to use the script delete all "UNDO"-lines. Then it should work.

Tyvm!

kws

 

EDIT: Yes, I know Booleans are the Crocs of modeling. But for my current task they really speed up my workflow...

 

Link to comment
  • Solution
import c4d
from c4d import gui
from c4d import utils
def main():
    doc = c4d.documents.GetActiveDocument()
    doc.StartUndo() #Don't know if this works like this at all
    selected = doc.GetActiveObjects(1)
    while len(selected) > 1:
        newBool = c4d.BaseObject(c4d.Oboole)
        doc.InsertObject(newBool)
        newBool[c4d.BOOLEOBJECT_SINGLE_OBJECT] = True
        newBool[c4d.BOOLEOBJECT_TYPE] = 0
        doc.AddUndo(c4d.UNDOTYPE_NEW, newBool)
        tmpA = selected.pop(0)
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tmpA)
        tmpA.InsertUnder(newBool)
        
        tmpB = selected.pop(0)
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, tmpB)
        tmpB.InsertUnder(newBool)

        settings = c4d.BaseContainer()
        res = utils.SendModelingCommand(command=c4d.MCOMMAND_MAKEEDITABLE,
                                        list=[newBool],
                                        mode=c4d.MODELINGCOMMANDMODE_ALL,
                                        bc=settings,
                                        doc=doc,
                                        flags=c4d.MODELINGCOMMANDFLAGS_NONE)
        if res and len(res) > 0:
            insertObject = None
            if res[0].IsInstanceOf(c4d.Onull) and res[0].GetDown() is not None:
                insertObject = res[0].GetDown()
                insertObject.Remove()
                doc.AddUndo(c4d.UNDOTYPE_NEW, insertObject)
            else:
                insertObject = res[0]
            
            doc.InsertObject(insertObject)
            doc.SetActiveObject(insertObject)
            insertObject.SetName(insertObject.GetName() + " Yay")
            selected.insert(0, insertObject)   
        
    doc.EndUndo();
    c4d.EventAdd()
   


if __name__=='__main__':
    main()

 

UNDOTYPE_CHANGE needs to be called before the change and the CallCommand started it's own undo. So I replaced it with a ModelingCommand that does the same. Also resolved the null node that happens (but not always), which requires an "undo new" for the child of the null if you take it out of the hierarchy. The modelingcommand adds an "undo new" for the null node it created, going nowhere because the null will not be added to the scene. The big difference is, the modeling command adds the undo step to the current undo scope that you started (all packed in one undo), but the CallCommand starts a new Undo scope. Cool script 😉

 

Regards

Fritz

 

p.s. plugincafe.maxon.net is a great place for getting help with the programming APIs. 🙂

Link to comment
×
×
  • Create New...

Copyright Core 4D © 2023 Powered by Invision Community