Sub topic 14
Controlling Modes and Workspaces with Python
Assume you know basic Python syntax, can navigate Blender's 3D Viewport, and understand that an object contains separate mesh data. This chapter uses Blender's bpy API, which is Blender's Python interface.
Reading and changing the current mode
Blender stores the current interaction mode on the active object. For a mesh, obj.mode can contain values such as OBJECT, EDIT, or SCULPT. Mode changes usually require an active object and a valid Blender context, meaning the correct window, view, and selection state.
import bpy
obj = bpy.context.active_object
if obj is None:
print("No active object")
elif obj.type != 'MESH':
print("The active object is not a mesh")
else:
print("Starting mode:", obj.mode)
if obj.mode != 'EDIT':
bpy.ops.object.mode_set(mode='EDIT')
print("Current mode:", obj.mode)
bpy.ops.object.mode_set(mode='OBJECT')
print("Final mode:", obj.mode)bpy.ops.object.mode_set() is an operator. Operators behave like commands invoked through Blender's user interface, so they depend on context. The bpy.context.edit_object value is useful when you need the mesh currently being edited. It is None when no mesh is in Edit Mode.
Editing mesh data in Edit Mode
Object Mode changes an object's transforms and other object-level properties. Edit Mode changes the vertices, edges, and faces inside its mesh data. While a mesh is in Edit Mode, use BMesh to edit the live edit mesh safely.
import bpy
import bmesh
obj = bpy.context.edit_object
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)
print("Selected vertices moved")
else:
print("Select a mesh and enter Edit Mode first")bmesh.from_edit_mesh() gives Python access to the mesh representation Blender is actively editing. Call bmesh.update_edit_mesh() after changes so the viewport and dependent systems receive the update.
Selecting several mesh objects
Blender can edit multiple selected mesh objects at once. The objects must be selected, one must be active, and they must be compatible with Edit Mode.
import bpy
meshes = [
obj for obj in bpy.context.selected_objects
if obj.type == 'MESH'
]
if meshes:
bpy.context.view_layer.objects.active = meshes[0]
if bpy.context.active_object.mode != 'EDIT':
bpy.ops.object.mode_set(mode='EDIT')
print("Editing:", [obj.name for obj in meshes])
else:
print("Select at least one mesh object")The operator changes the mode for the active editing context. The selected mesh objects then participate in the same Edit Mode session. Selection state inside each mesh remains separate.
Switching workspaces
A workspace is a saved interface arrangement. It contains areas such as the 3D Viewport, Outliner, Properties editor, and scripting editors. The active workspace belongs to the current window.
import bpy
workspace = bpy.data.workspaces.get("Scripting")
if workspace is not None:
bpy.context.window.workspace = workspace
print("Active workspace:", bpy.context.workspace.name)
else:
print("The Scripting workspace does not exist")Built-in workspaces provide task-focused layouts. Modeling is arranged for mesh work, Sculpting for sculpt tools, UV Editing for UV maps, Shading for materials and rendering, Animation for keyframes, and Scripting for Python development. The exact list can vary because workspaces can be added, renamed, or removed.
Workspace defaults and safe scripts
A workspace can have a preferred object mode. You can inspect or set that preference, but Blender still needs a compatible active object when it activates Edit Mode.
import bpy
workspace = bpy.context.workspace
print("Workspace:", workspace.name)
print("Workspace mode preference:", workspace.object_mode)
workspace.object_mode = 'OBJECT'
print("New preference:", workspace.object_mode)
print("Available workspaces:")
for item in bpy.data.workspaces:
print("-", item.name)Workspace changes affect the current window. If a script runs from a background process or without a suitable UI context, UI operators and workspace switching may not work. Keep data edits separate from context-sensitive UI operations when possible.
Exercise
Why should a script use bmesh.fromeditmesh() instead of directly changing obj.data.vertices while a mesh is in Edit Mode?
Show answer
Edit Mode uses a live edit mesh that is not automatically synchronized with regular mesh data. BMesh edits that live structure and bmesh.updateeditmesh() sends the changes back to Blender.