Debian Codegraph @

src/bonsai/docs/guides/development/undo_system.rst

raw
1 Undo system
2 ===========
3
4 Supporting undo and redo is quite a complex problem because the Blender undo
5 system only keeps track of changes occurring in the Blender system. However,
6 changes actually occur in two other locations that Blender doesn't know about:
7 the IFC dataset, and the Bonsai system that synchronises Blender and the IFC
8 dataset.
9
10 Let's see how undo works in a basic Blender add-on without IFC or Bonsai
11 getting involved.
12
13 .. code-block:: python
14 :emphasize-lines: 4
15
16 class Foobar(bpy.types.Operator):
17 bl_idname = "foobar"
18 bl_label = "Foobar"
19 bl_options = {"REGISTER", "UNDO"}
20
21 def execute(self, context):
22 context.scene.name = "Foobar"
23 return {"FINISHED"}
24
25 This operation changes Blender data. The important line is ``bl_options =
26 {"REGISTER", "UNDO"}``, which tells Blender to keep track of it as a single
27 transaction in its undo history. When you press undo or redo, Blender figures
28 out all the changes automatically and you don't need to do anything.
29
30 If you have an operator that only manipulates (creates, removes, or edits)
31 Blender data, this solution is sufficient.
32
33 Now let's look at pure IfcOpenShell.
34
35 .. code-block:: python
36 :emphasize-lines: 3,5
37
38 import ifcopenshell
39 model = ifcopenshell.open("foo.ifc")
40 model.begin_transaction()
41 model.create_entity("IfcWall")
42 model.end_transaction()
43 model.undo()
44 model.redo()
45
46 Pure IfcOpenShell let's you start and stop recording transactions whenever you
47 want. Since IfcOpenShell has no interface, you manually run code like
48 ``model.undo()`` and ``model.redo()`` to undo and redo.
49
50 This scenario where there is pure IfcOpenShell never occurs with Bonsai.
51 Instead, stuff happens in Blender operators.
52
53 .. code-block:: python
54 :emphasize-lines: 6,7
55
56 class Foobar(bpy.types.Operator):
57 bl_idname = "foobar"
58 bl_label = "Foobar"
59 bl_options = {"REGISTER", "UNDO"}
60
61 def execute(self, context):
62 return IfcStore.execute_ifc_operator(self, context)
63
64 def _execute(self, context):
65 ifcopenshell.api.run("foo.bar", tool.Ifc.get())
66 return {"FINISHED"}
67
68 When your operator manipulates (creates, removes, or edits) IFC data directly or
69 indirectly (i.e. through calling another operator), your operator must be
70 wrapped in an ``IfcStore.execute_ifc_operator`` call. This wrapper will:
71
72 1. Begin a Bonsai transaction
73 2. Begin an IfcOpenShell transaction
74 3. Run your operator's ``_execute``.
75 4. End the IfcOpenShell transaction
76 5. End the Bonsai transaction
77
78 The IfcOpenShell transaction keeps track of IFC data changes, and the Bonsai
79 transaction keeps track of all other custom data changes, like changes in the
80 ``id_map`` and ``guid_map``. For the vast majority of operations, this wrapper
81 provides everything that you need.
82
83 If, however, your operator manipulates data that is not tracked by Blender, is
84 not tracked in the IFC data, and is not tracked in the element map, then you
85 will have to write your own rollback (undo) and commit (redo) code for your
86 operator. Here is an example.
87
88 .. code-block:: python
89
90 class Foobar(bpy.types.Operator):
91 bl_idname = "foobar"
92 bl_label = "Foobar"
93 bl_options = {"REGISTER", "UNDO"}
94
95 def execute(self, context):
96 IfcStore.begin_transaction(operator)
97 old_value = Foo.bar
98 result = self._execute(context)
99 new_value = Foo.bar
100 self.transaction_data = {"old_value": old_value, "new_value": new_value}
101 IfcStore.add_transaction_operation(self)
102 IfcStore.end_transaction(operator)
103 return result
104
105 def _execute(self, context):
106 Foo.bar = "baz"
107 return {"FINISHED"}
108
109 def rollback(self, data):
110 Foo.baz = data["old_value"]
111
112 def commit(self, data):
113 Foo.baz = data["new_value"]
114
115 Note that there is still a distinction between ``execute`` and ``_execute``.
116 This recommended convention allows you to quickly discern undo state tracking
117 code from regular operation code.