Sub topic 7
Constraining a transform to an axis or plane
This chapter assumes you can select objects and start a move, rotate, or scale with G, R, and S. It also assumes you know the X, Y, and Z axes. Typed numeric values get only a brief mention here and are covered in the next chapter.
Why constrain a transform
An unconstrained transform follows the mouse in the screen plane of your current view. That is fast but imprecise. A constraint limits a transform to one axis or one plane, so the object cannot drift in any other direction. Constraints remove the most common mistake in Blender: an object that looks correct from the front view but sits in the wrong place in depth.
A constraint lasts only for the current operation. When you confirm or cancel, it is gone.
Locking to a single axis
Start a transform, then press X, Y, or Z. You can press the axis key before or after you move the mouse. The result is the same.
- Select an object and press G.
- Press X. A red line appears through the object, and the movement is limited to that line.
- Move the mouse. The object slides along X only.
- Click or press Enter to confirm. Press Esc or right-click to cancel.
Blender colors the constraint line to match the axis: red for X, green for Y, blue for Z. The header at the top of the viewport also shows the active constraint and the distance moved, for example D: 2.0000 m along global X. Read the header when you are unsure which orientation is active.
The same keys work for rotation and scale:
- R then Z rotates around the Z axis. This is the usual way to turn an object like a door or a wheel.
- S then Z scales along Z only, which stretches or squashes the object vertically.
Locking to a plane
Hold Shift while pressing the axis key to constrain to the plane perpendicular to that axis. In other words, Shift+Z means "everything except Z". The object moves freely in X and Y and cannot change height.
| Keys | Effect on move (G) | Effect on scale (S) |
|---|---|---|
| X | Only along X | Only along X |
| Shift+X | In the YZ plane, X locked | Scales Y and Z, X unchanged |
| Y | Only along Y | Only along Y |
| Shift+Y | In the XZ plane, Y locked | Scales X and Z, Y unchanged |
| Z | Only along Z | Only along Z |
| Shift+Z | In the XY plane, Z locked | Scales X and Y, Z unchanged |
Shift+Z is the most useful of these. G Shift+Z slides an object across the ground without lifting it. S Shift+Z widens an object without changing its height.
Cycling between Global and Local axes
Pressing the same axis key again changes which set of axes is used. With the default Global orientation:
- First press: the Global axis. This is the world's X, Y, or Z, no matter how the object is turned.
- Second press: the object's Local axis. This follows the object's own rotation.
- Third press: the constraint is removed and the transform is free again.
The header text changes between along global X and along local X, so you can confirm which one you have.
The Local setting matters when an object has been rotated. Suppose a plank is turned 30 degrees around Z. G X moves it along the world's X axis, which is diagonal to the plank's length. G X X moves it along its own X axis, which is along the plank's length. Choose Local when you want to slide an object "forward" in its own sense.
If you change the scene's transform orientation (a later chapter covers this), the first press uses that orientation instead of Global.
Constraining with the middle mouse button
You can also constrain without a key. During a transform, hold the middle mouse button and drag a little in the direction you want. Blender picks the axis closest to your drag direction and locks to it. This is handy when you are already dragging and do not want to move your hand to the keyboard. Keys are more reliable, because the middle-mouse choice depends on your current view angle.
Combining a constraint with typed values
Axis constraints work with typed numbers. After choosing the axis, type a value and press Enter:
- G, X, 3, Enter moves the object 3 units along X.
- R, Z, 90, Enter rotates the object 90 degrees around Z.
- S, Shift+Z, 2, Enter doubles the size in X and Y and leaves Z alone.
The order of keys does not matter as long as the operation (G, R, or S) comes first.
The same constraints in Python
In the Python API, a constraint is simply a matter of changing only some components. The location, rotation, and scale properties are vectors, and you can change a single component.
import bpy
obj = bpy.context.active_object
# Equivalent of G, X, 2, Enter (Global axis)
obj.location.x += 2.0
# Equivalent of G, Shift+Z, then moving 1 unit in X and 1 in Y
obj.location.x += 1.0
obj.location.y += 1.0
# Equivalent of S, Shift+Z, 2, Enter (scale X and Y only)
obj.scale.x *= 2.0
obj.scale.y *= 2.0Moving along the object's Local axis needs one more step. You convert the local direction into world space using the object's rotation:
import bpy
from mathutils import Vector
obj = bpy.context.active_object
# Equivalent of G, X, X, 2, Enter (Local axis)
local_x = obj.matrix_world.to_quaternion() @ Vector((1.0, 0.0, 0.0))
obj.location += local_x * 2.0This assumes the object has no parent. If it has one, location is expressed relative to the parent, so the result would need further conversion.
Common errors
- Using Z when you meant Shift+Z. Z moves only vertically. Shift+Z moves in the horizontal plane. If the object barely moves or moves only up and down, check the header.
- Pressing the axis key too many times. A third press removes the constraint. If your object suddenly follows the mouse freely, you cycled past Local.
- Expecting Global when the object is rotated and Local is active. Check the header for
globalorlocalbefore confirming. - Forgetting that constraints reset. Each new G, R, or S starts unconstrained, so you must choose the axis again.
- Judging the result only from one view. Confirm in a second view, or read the values in the Transform panel, that the object moved only where you intended.
Exercise
A cube is rotated 45 degrees around Z. You want to slide it along its own X direction, not the world's X direction, by 3 units. Which keys do you press, and how does the header text confirm that the right constraint is active?
Show answer
Press G, then X twice, then type 3 and press Enter (G, X, X, 3, Enter). The first X press selects the Global axis, and the second switches to the Local axis. The header should read "along local X" before you confirm.
Exercise
You want to double the width and depth of an object but keep its height unchanged. Give the key sequence, and write the equivalent Python for an object stored in a variable called obj.
Show answer
Press S, Shift+Z, 2, Enter. Shift+Z locks Z and leaves X and Y free. In Python: obj.scale.x = 2.0 and obj.scale.y = 2.0.