How do I use UndoGroup in the API?

I’m trying to figure out how to use the UndoGroup correctly in my Python script.

I don’t understand the documentation much on how to use it: 2.22. UndoGroup — mocha documentation

I can’t find any example here or on github of any code using undogroups ether, so no luck there.

Could someone with the knowlage share the correct way to create undo blocks in Mocha?

FYI I’m making a script that scales your track and splines if your canvas size have changed compared to what you first did your track.
Think a 1920x1080 that got cropped to 1080x1080.
Would love for the user to be able to undo the scaling if something wrong happens.

Try this:

with project.undo_group():

Thanks for pointing this out @Danell . We’ll make sure to have a guide for Undo groups in the next version of the documentation.

To broaden @mikkiw 's example, you apply UndoGroup as a literal grouping, using the ‘with’ statement.

For example, the code below will set an undo group for renaming two layers:

from mocha import project 
proj = project.get_current_project() 
layer_list = proj.layers 
with proj.undo_group(): 
    layer_list[0].name = "New name 1" 
    layer_list[1].name = "New name 2"