Sub topic 1
The camera object and its camera data
This chapter explains how Blender stores a camera. It covers the split between the camera object and its camera data, how a scene picks which camera renders, and how to build one with Python. Later chapters build on this model for focal length, framing, and depth of field.
Prerequisites
This chapter assumes you can already do the following:
- Orbit, pan, and zoom in the 3D viewport, and select objects.
- Tell the 3D viewport apart from a rendered image.
- Use the Properties editor and the N sidebar.
- Optionally, read simple Python. The scripting examples use the
bpymodule and run in Blender's Scripting workspace.
Two data-blocks, not one
A Blender camera is made of two linked pieces.
- The camera object is an entry in the scene. It holds the transform: location, rotation, and scale. It also holds constraints, parent relationships, and animation of those values.
- The camera data is a separate data-block of type
Camera. It holds the lens settings: focal length, sensor size, clip range, projection type, depth of field, and display options.
The object points to the data. This is the same pattern Blender uses for meshes, lights, and curves. In Python the object is in bpy.data.objects and the data is in bpy.data.cameras. Reach the data from the object with obj.data.
Several objects can share one camera data-block. Edit the lens on one and every linked object changes. This is useful when you want two positions with identical optics. It also causes surprises when you change a lens and a camera you did not select changes too. The number beside the data-block name in the Properties editor shows how many users it has.
What lives where
Knowing which block owns a setting tells you where to look in the interface and in code.
| Setting | Owner | Python path |
|---|---|---|
| Location, rotation | Object | `cam_obj.location`, `cam_obj.rotation_euler` |
| Track To constraint | Object | `cam_obj.constraints` |
| Focal length | Camera data | `cam_obj.data.lens` |
| Sensor width | Camera data | `cam_obj.data.sensor_width` |
| Clip start and end | Camera data | `cam_obj.data.clip_start`, `cam_obj.data.clip_end` |
| Depth of field | Camera data | `cam_obj.data.dof` |
| Render resolution | Scene | `scene.render.resolution_x` |
Render resolution belongs to the scene, not the camera. Two cameras in one scene always render at the same pixel size.
Adding a camera in the interface
In the 3D viewport, choose Add > Camera. Blender places the new camera at the 3D cursor. It is rotated to match the view or to the world axes, depending on the Align setting in the Adjust Last Operation panel.
The new camera is not automatically the one that renders. If the scene had no camera, Blender assigns the first camera you add. If one already exists, the old one stays active.
The active camera
A scene has one camera property. The renderer uses whatever object it names. Having many cameras in the file does not matter. Only the assigned one renders.
To change it in the interface:
- Select the camera object.
- In the viewport, choose View > Cameras > Set Active Object as Camera, or press
Ctrl+Numpad 0.
This also switches the viewport into camera view. The active camera is drawn with a filled triangle on top. Inactive cameras show an outline.
To look through the camera without changing which one is active, use Numpad 0 while the scene camera is set. To fly the camera by moving the view, enable Lock Camera to View in the N sidebar's View tab. While that is on, orbiting and panning move the camera object itself.
Creating a camera with Python
The same two-step structure shows up in code. You create the data first, then wrap it in an object, then link the object into a collection so it appears in the scene.
import bpy
scene = bpy.context.scene
cam_data = bpy.data.cameras.new("ShotCamera")
cam_data.lens = 50 # focal length in mm
cam_data.sensor_width = 36 # sensor width in mm
cam_obj = bpy.data.objects.new("ShotCamera", cam_data)
scene.collection.objects.link(cam_obj)
cam_obj.location = (7.0, -7.0, 5.0)
scene.camera = cam_obj # make it the active cameraEach step has a purpose:
bpy.data.cameras.newcreates the camera data-block. Nothing in the scene changes yet.bpy.data.objects.newcreates an object that points to that data.scene.collection.objects.linkplaces the object in the scene. Without this call the object exists in the file but is invisible and cannot render.scene.camera = cam_objmakes it the active camera. This takes the object, not the data.
The object and the data can have the same name here because they live in different lists. In the Outliner you will see both: the object, and the data nested beneath it.
Common errors
These are the mistakes people hit most often with this model.
- Assigning data instead of an object.
scene.camera = cam_dataraises aTypeError. The scene wants the object. - Forgetting to link. A camera created with
objects.newbut never linked does not appear in the Outliner's View Layer mode and cannot be seen in the viewport. - Editing shared data by accident. If two camera objects use one data-block, changing
lenson one changes both. Make a single-user copy with the number button next to the data name, or in code withcam_obj.data = cam_obj.data.copy(). - Looking up by name after a rename. Names are unique per data type, but a second camera named
ShotCamerabecomesShotCamera.001. A lookup such asbpy.data.objects["ShotCamera"]then returns the old one. - No camera, no render. If
scene.cameraisNone, rendering fails with a "no camera found in scene" error. Check it before a batch render.
You can guard against the last case with a short check.
import bpy
scene = bpy.context.scene
if scene.camera is None:
print("No active camera set")
else:
cam = scene.camera
print(cam.name, cam.data.name, cam.data.lens)Listing every camera in a file
Because cameras are data-blocks, you can enumerate them directly. This helps when you inherit a file and want to see what is in it.
import bpy
scene = bpy.context.scene
for obj in bpy.data.objects:
if obj.type == 'CAMERA':
marker = "ACTIVE" if obj == scene.camera else ""
print(f"{obj.name:20} data={obj.data.name:20} users={obj.data.users} {marker}")A users count above 1 tells you the data is shared. A camera data-block with zero users and no fake user is removed when the file is saved and reopened.
Summary of the model
- The object holds position and aim. The data holds the optics.
- The scene names exactly one camera object as active.
- Linking an object to a collection puts it in the scene.
- Shared data means shared lens settings.
The next chapters change the optics, starting with how focal length and sensor size set what the camera sees.
Exercise
You run scene.camera = bpy.data.cameras["ShotCamera"] and get a TypeError. What is wrong, and what is the fix?
Show answer
The scene's camera property takes a camera object, but this code passes camera data. Use the object instead: scene.camera = bpy.data.objects["ShotCamera"].
Exercise
Two camera objects, CamA and CamB, share one data-block. You set CamA's focal length to 85 mm and CamB also changes. How do you give CamB its own lens settings in Python?
Show answer
Copy the data and assign the copy to CamB: bpy.data.objects["CamB"].data = bpy.data.objects["CamB"].data.copy(). After this, CamB has a single-user data-block and edits to CamA no longer affect it.
Exercise
A script creates a camera with bpy.data.objects.new and sets scene.camera to it, but the camera never shows in the viewport. Which call is missing?
Show answer
The object was never linked to a collection. Add scene.collection.objects.link(cam_obj).