Sub topic 15
Scripting Cameras with `bpy`
This chapter assumes that you can navigate Blender and read simple Python. It also assumes that you understand the difference between a scene object and the data attached to that object.
Camera objects and camera data
A Blender camera has two related parts:
- A camera object has a location, rotation, and constraints. It places the camera in the scene.
- Camera data stores lens settings such as focal length, sensor width, clipping, and depth of field.
The object points into the scene along its local negative Z axis. Its local Y axis normally defines which direction is “up.” The scene renders through the object assigned to scene.camera.
The following script creates a camera, positions it, aims it at a target, and makes it active:
import bpy
scene = bpy.context.scene
# Create camera data and set optical properties.
cam_data = bpy.data.cameras.new("ShotCameraData")
cam_data.lens = 50
cam_data.sensor_width = 36
cam_data.clip_start = 0.1
cam_data.clip_end = 1000
# Create the camera object and link it to the scene.
cam_obj = bpy.data.objects.new("ShotCamera", cam_data)
scene.collection.objects.link(cam_obj)
cam_obj.location = (7.0, -7.0, 5.0)
# Create a target at the origin.
target = bpy.data.objects.new("Subject", None)
scene.collection.objects.link(target)
target.empty_display_type = "SPHERE"
target.empty_display_size = 0.5
target.location = (0.0, 0.0, 1.0)
# Keep the camera aimed at the target.
track = cam_obj.constraints.new(type="TRACK_TO")
track.target = target
track.track_axis = "TRACK_NEGATIVE_Z"
track.up_axis = "UP_Y"
# Use this camera for rendering.
scene.camera = cam_objThe script uses Blender’s data API, which directly creates and edits datablocks. This is usually more predictable than scripting interface actions such as selecting an object and pressing a shortcut.
Configuring focus and framing
Depth of field belongs to the camera data, but its focus object can be the target object:
cam_data.dof.use_dof = True
cam_data.dof.focus_object = target
cam_data.dof.aperture_fstop = 2.8A lower f-stop produces a shallower focus range. It can blur the background, but the effect also depends on the camera’s distance, focal length, sensor, and subject distance.
Set the output shape before judging the composition:
scene.render.resolution_x = 1920
scene.render.resolution_y = 1080
scene.render.resolution_percentage = 100
cam_data.show_composition_thirds = True
cam_data.show_passepartout = TrueThe resolution determines the aspect ratio. A camera composition that looks balanced in a square preview can feel too wide or too narrow in a 16:9 render.
Switching cameras in an animation
A timeline marker can activate another camera from a chosen frame onward:
camera_b = bpy.data.cameras.new("CameraBData")
camera_b_obj = bpy.data.objects.new("CameraB", camera_b)
scene.collection.objects.link(camera_b_obj)
camera_b_obj.location = (-6.0, -6.0, 4.0)
marker = scene.timeline_markers.new("CutToB", frame=48)
marker.camera = camera_b_objThis creates a camera cut at frame 48. Both cameras should be configured before rendering the animation.
Exercise
Which property makes a camera render the scene, and which property controls its focal length?
Show answer
bpy.context.scene.camera selects the active camera object. The camera object's data.lens property controls focal length.