Francisco E. Gonzalez ← Back to portfolio
Archive  Published tutorial · 2014

Real f-stops with the mia_lens_bokeh shader

How I drove mental ray's bokeh lens in Maya from a camera's real focal length and f-number, so render depth of field matched a photographic lens instead of being dialed in by eye.

Published 2014Maya · mental ray (legacy)
Archived tutorial. This is a 2014 write-up of a workflow from the mental ray era, kept here for reference. Since then mental ray has been discontinued, so the shader no longer applies, but the lens math still does: current renderers such as Arnold and RenderMan expose aperture or f-stop controls on the camera, and the same math below converts a real lens into the aperture size those renderers expect.

F-number

The f-number, or f-stop, controls how much depth of field you get. It is the lens's focal length divided by the diameter of its opening. A low f-number such as f/1.4 means a wide opening and a shallow depth of field, so only a thin slice of the scene is in focus. A high f-number such as f/8 means a small opening, and much more of the scene stays sharp.

F-stop examples

Render of stacked candy at f/1.4, with a very shallow depth of field
f/1.4
The same render at f/4
f/4
The same render at f/5.6
f/5.6
The same render at f/8, with most of the stack in focus
f/8

Bokeh lens parameters

mia_lens_bokeh attributes in Maya's Attribute Editor
mia_lens_bokeh attributes
Plane
The focus distance: how far from the camera, in scene units, the subject is. Measure it with a Distance tool, or turn on Display › Heads Up Display › Object Details to read the distance from the camera to the selected object.
Radius
The size of the lens opening. A bigger radius gives a shallower depth of field and stronger blur. This is the value the setup below calculates from a real lens.
Samples
The quality of the depth of field. More samples give smoother, less noisy blur, and take longer to render.
Bias
Shifts how samples are spread across the lens. Raising it makes out-of-focus areas blur more and makes highlights spread into larger bokeh shapes.

Blade count and blade angle

With Blade Count at 0, the bokeh is a perfect circle, like an ideal lens. Setting a count turns it into a polygon, matching a real iris: 5 blades give a pentagon. Blade Angle rotates that shape. With 4 blades you get a diamond, and rotating it turns the diamond into a flat square.

Diamond-shaped bokeh from 4 blades
4 blades
Square bokeh from 4 blades, rotated
4 blades, rotated

Use Bokeh

Turning on Use Bokeh replaces the built-in shape with a map of your own. This is useful for stylized bokeh, or for faking chromatic aberration by using an image whose color channels are slightly offset, so highlights pick up colored fringes the way a real lens does.

Setup in Maya

Select the camera, open its mental ray section, and connect a mia_lens_bokeh node as the lens shader. Then build a small node network that computes the radius from the camera's real focal length and the f-number you want:

radius = focal length ÷ f-number ÷ 2 ÷ 10
focal length ÷ f-number = aperture diameter in mm · ÷ 2 = radius · ÷ 10 = mm to cm (Maya's default unit)

Create three multiplyDivide nodes and set each one's operation to Divide.

  1. Connect the camera's Focal Length output to Input 1 X of the first node.
  2. Type your f-number into its Input 2 X, and connect its Output X to Input 1 X of the second node.
  3. Type 2 into the second node's Input 2 X, and connect its Output X to Input 1 X of the third node.
  4. Type 10 into the third node's Input 2 X, and connect its Output X to the bokeh lens Radius.

If you have scaled your camera away from its default of 1, account for that scale too, since the radius is measured in scene units.

Diagram: camera focal length divided by f-number, then by 2, then by 10, feeding the bokeh lens radius
The network at a glance
The same network in Maya's Hypershade: camera, three divide nodes, and the mia_lens_bokeh node
The same network in Hypershade