# Custom Undo/Redo develop

**URL:** <https://discourse.slicer.org/t/custom-undo-redo-develop/27368>\
**Category:** Development\
**Tags:** python\
**Created:** [January 20, 2023, 4:00am UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368 "2023-01-20T04:00:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [January 20, 2023, 4:00am UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368/1 "2023-01-20T04:00:45Z")

</div>

Operating system: window 11  
Slicer version: slicer 5.2.1

Q1. After searching various materials, it was said that the undo/redo function for all nodes drawn in the scene is difficult to implement. Is this true?

Q2. If so, I want to create a redo and undo function for a specific node (markupFiducial, Closedcurved).

```auto
# Enable undo for the scene

slicer.mrmlScene.SetUndoOn()

# Enable undo for markups fiducial nodes

defaultMarkupsNode = slicer.mrmlScene.GetDefaultNodeByClass("vtkMRMLMarkupsFiducialNode")
if not defaultMarkupsNode:
    defaultMarkupsNode = slicer.vtkMRMLMarkupsFiducialNode()
    slicer.mrmlScene.AddDefaultNode(defaultMarkupsNode)

defaultMarkupsNode.UndoEnabledOn()

# Add standard keyboard shortcuts for scene undo/redo

redoKeyBindings = qt.QKeySequence.keyBindings(qt.QKeySequence.Redo)
for redoBinding in redoKeyBindings:
    redoShortcut = qt.QShortcut(slicer.util.mainWindow())
    redoShortcut.setKey(redoBinding)
    redoShortcut.connect("activated()", slicer.mrmlScene.Redo)

undoKeyBindings = qt.QKeySequence.keyBindings(qt.QKeySequence.Undo)
for undoBinding in undoKeyBindings:
    undoShortcut = qt.QShortcut(slicer.util.mainWindow())
    undoShortcut.setKey(undoBinding)
    undoShortcut.connect("activated()", slicer.mrmlScene.Undo)

```

This @lassoan 's code not working in python interactor

After posting this question, I’ll add more detailed questions as I continue to investigate.

What I want to know is how to implement redo/undo for a specific node.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 20, 2023, 6:38am UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368/2 "2023-01-20T06:38:55Z")

</div>

After copy-pasting the code snippet in the Python console, undo/redo works well for markup point lists. Please describe what you did, what you expected to happen, and what happened instead.

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [January 20, 2023, 7:18am UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368/3 "2023-01-20T07:18:52Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/d/add62a25e77a5eea139994a81770347282c5cd75.png)

step1) copy-pasting your full-code in python console

step2) check one markup points

step3) Press ‘Ctrl+z’ or ‘Alt + Backspace’

result ) The markup point does not disappear and is maintained.

---

<div class="post-metadata">

**Author:** ![dsa934](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dsa934/32/17907_2.png) [@dsa934](https://discourse.slicer.org/u/dsa934)\
**Post date:** [January 20, 2023, 8:41am UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368/4 "2023-01-20T08:41:46Z")

</div>

```auto
# set undo flag on 
slicer.mrmlScene.SetUndoOn()

# set node types what you want to undo
m_node = slicer.mrmlScene.GetDefaultNodeByClass("vtkMRMLMarkupsFiducialNode")

# save current state
slicer.mrmlScene.SaveStateForUndo()

'making.. markups...'

'oh my mistakes ! let's undo '

# exe undo fucntions ! 
slicer.mrmlScene.Undo

# render preview view 
"um.. i don't know built-in functions"

```

I think the undo process for vtkMRMLMarkupsFiducialNode is the same as above. Is that correct?  
@lassoan

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 20, 2023, 1:48pm UTC](https://discourse.slicer.org/t/custom-undo-redo-develop/27368/5 "2023-01-20T13:48:30Z")

</div>

The code snippets changes the default markup point list node so that by default all new nodes of this class are created with undo enabled. If you already have a node then call `UndoEnabledOn()` method on that existing node.
