Sub topic 5
Switching between modes
This chapter assumes you can orbit, pan and zoom the viewport, select objects, and use Move, Rotate and Scale. It also assumes you know the difference between an object and its mesh data, and between Object Mode and Edit Mode. The chapter covers the ways to change mode, what happens to your data when you do, and what goes wrong when scripts or shortcuts run in the wrong mode.
What a mode switch actually does
Each object has a current mode, and the active object decides which mode the 3D Viewport shows. A mode switch changes which tools, shortcuts and selection rules are available. For a mesh, Edit Mode also changes where the data lives.
In Object Mode, mesh geometry is stored in the mesh data-block (obj.data). When you enter Edit Mode, Blender copies that geometry into a temporary editing structure called a BMesh. Your edits change the BMesh. When you leave Edit Mode, Blender writes the BMesh back into the mesh data-block.
This copy-in, copy-out step explains most surprises in this chapter. While a mesh is in Edit Mode, the regular mesh data is not kept in sync with what you see on screen.
Ways to switch modes in the interface
You have three common options.
- The mode menu. The drop-down at the left of the 3D Viewport header lists every mode the active object supports. A mesh offers Object Mode, Edit Mode, Sculpt Mode, Vertex Paint, Weight Paint and Texture Paint. A camera or an empty offers only Object Mode, because it has no mode-specific data to edit.
- The Tab key. With the pointer over the 3D Viewport, Tab toggles Edit Mode on and off. It works for mesh objects and for other editable types such as curves and armatures.
- The mode pie menu. Ctrl+Tab opens a pie menu with the available modes. In Edit Mode on a mesh, Ctrl+Tab opens the vertex, edge and face selection pie instead. That pie changes the select mode, not the object mode.
Shortcuts are context-sensitive. Tab does nothing useful if the pointer is over the Outliner or the Properties editor, and it cannot enter Edit Mode if there is no active object.
The active object decides
The mode menu always acts on the active object, which is the one with the lighter outline. A selected object that is not active does not control the menu. If the menu looks wrong or is greyed out, check what is active before checking anything else.
Some cases that confuse people:
- Nothing is active. The mode menu offers no useful options. Click an object to make it active.
- The active object is hidden. Blender cannot switch a hidden object into Edit Mode.
- The active object is a type without an edit mode. An empty is one example.
- The object is linked from another file. Library data is read-only unless you make it local or override it.
Switching several objects together
If several objects of a compatible type are selected, pressing Tab enters Edit Mode for all of them. This is multi-object editing, covered in a later chapter. For this chapter, remember two points.
- Only the selected compatible objects join the editing session.
- The active object is only one of the objects being edited.
Leaving Edit Mode with Tab returns all of them to Object Mode.
Another case is a locked interaction mode. The lock icon in the mode area of the header (labelled "Lock Object Modes" in the Edit menu) controls whether clicking another object while in Edit Mode switches that object into the same mode. With it on, clicking an object of the same type in the viewport adds it to the session. Turn it off if you want to click between objects freely.
What you can lose or confuse
Edits are written on exit
The BMesh is committed to the mesh data when you leave Edit Mode. Saving a file while in Edit Mode also writes the edit data, so saving is safe. Scripts that read obj.data.vertices while the object is in Edit Mode, however, can read stale positions.
Undo has two layers
Edit Mode keeps its own undo steps for mesh changes. Object Mode steps are tracked separately. Switching modes is itself an undo step, so Ctrl+Z after an edit session can step back through your edits first and then through the mode change. If an undo seems to do nothing, check which mode you are in.
Some operators run only in one mode
Object-level operators such as Join, Apply transforms and Duplicate Object are available in Object Mode. Mesh operators such as Extrude, Bevel and Loop Cut need Edit Mode. When a menu item is greyed out, the cause is often a mode mismatch, not a bug.
Switching modes from Python
The operator bpy.ops.object.mode_set changes the mode of the active object. Every object has a read-only mode property that reports its current mode as a string.
import bpy
obj = bpy.context.active_object
if obj is not None and obj.type == 'MESH':
print(obj.mode) # 'OBJECT' or 'EDIT', and so on
bpy.ops.object.mode_set(mode='EDIT')
print(obj.mode) # 'EDIT'
bpy.ops.object.mode_set(mode='OBJECT')
print(obj.mode) # 'OBJECT'Common mode names are 'OBJECT', 'EDIT', 'SCULPT', 'VERTEX_PAINT', 'WEIGHT_PAINT', 'TEXTURE_PAINT' and 'POSE'. A name that does not apply to the active object raises an error.
Guarding against failures
mode_set raises a RuntimeError when its context check fails. Typical causes are no active object, a hidden active object, or a mode the object type does not support. Calling it when the object is already in the requested mode is harmless, but it is cleaner to test first.
import bpy
def ensure_mode(mode):
"""Switch the active object to `mode`. Return False if there is nothing to switch."""
obj = bpy.context.active_object
if obj is None:
return False
if obj.mode != mode:
bpy.ops.object.mode_set(mode=mode)
return True
if ensure_mode('OBJECT'):
print("Active object is in Object Mode")
else:
print("No active object")Editing while in Edit Mode
To change geometry while the mesh is in Edit Mode, use BMesh. The next script moves every selected vertex up by one unit.
import bpy
import bmesh
obj = bpy.context.edit_object # None unless a mesh is in Edit Mode
if obj is not None and obj.type == 'MESH':
bm = bmesh.from_edit_mesh(obj.data)
for vert in bm.verts:
if vert.select:
vert.co.z += 1.0
bmesh.update_edit_mesh(obj.data)bpy.context.edit_object is None unless the object is in Edit Mode, so the check also acts as a mode test. After the loop, bmesh.update_edit_mesh tells Blender to refresh the display.
If you need obj.data itself to match the Edit Mode state without leaving Edit Mode, call obj.update_from_editmode(). It writes the current edit data into the mesh data-block. Use it sparingly, because it copies the whole mesh.
Object-level and data-level changes
A script that is not in Edit Mode can change either the object or its data, and the two behave differently.
import bpy
obj = bpy.context.active_object
if obj is not None and obj.type == 'MESH':
bpy.ops.object.mode_set(mode='OBJECT')
obj.location.x += 2.0 # object-level change (transform)
obj.data.vertices[0].co.z += 1.0 # data-level change (one vertex)The first line moves the whole object. The second moves one vertex inside the shared mesh data. If other objects share that mesh data, they all show the moved vertex.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Tab does nothing | Pointer is not over the 3D Viewport, or no active object | Move the pointer into the viewport and click an object |
| Mode menu offers only Object Mode | Active object has no edit data, such as an empty | Select a mesh, curve or armature |
| `RuntimeError` from `mode_set` | No active object, a hidden object, or an unsupported mode name | Check `bpy.context.active_object` and its visibility first |
| Script reads old vertex positions | Mesh is in Edit Mode, so `obj.data` is stale | Switch to Object Mode, or use BMesh |
| Wrong objects enter Edit Mode | Several compatible objects were selected | Deselect extras before pressing Tab |
| Clicking another object does not select it | Lock Object Modes is on and the other object is a different type | Return to Object Mode first, or turn the lock off |
Exercises
Exercise
You select a mesh cube and a camera, with the cube active, then press Tab with the pointer over the 3D Viewport. Which objects enter Edit Mode, and why?
Show answer
Only the cube. The camera has no edit data and is not a compatible type, so it is left out. Tab enters Edit Mode for the selected objects that can share the session, and the cube is the only mesh in the selection.
Exercise
A script runs in Edit Mode and prints obj.data.vertices[0].co after you moved that vertex by hand. The printed value does not change. What are two ways to read the current position correctly?
Show answer
Switch to Object Mode with bpy.ops.object.mode_set(mode='OBJECT') before reading, or read the position from BMesh with bmesh.from_edit_mesh(obj.data) and bm.verts[0].co. A third option is to call obj.update_from_editmode() first, which writes the edit data into obj.data.
Exercise
Write a short script that switches the active object to Edit Mode only if it is a mesh and is not already in Edit Mode.
Show answer
import bpy obj = bpy.context.activeobject if obj is not None and obj.type == 'MESH' and obj.mode != 'EDIT': bpy.ops.object.modeset(mode='EDIT')