Lego · SQL Server 2025 · Blender
Last time I built my head out of Lego inside SQL Server and made a video of it. Two questions came back. How was the video made? And what would the bricks cost? This page answers both, and you can take the parts list away with you.
Trevor Dwyer@sqlartistIn I Built My Head Out of Lego. Inside SQL Server 2025. I took a 3D model of my own head, loaded it into SQL Server and had T-SQL turn it into bricks. Then I noticed I'd built myself without a brain, so I built one of those as well, and made a short video of the whole lot going together.
Two things came back from readers. They wanted to know how the video was made, because a database isn't the obvious place to start an animation. And they wanted to know what the bricks would cost if you built it for real.
Well, obviously building my head, not yours, would be weird. Well, not weird; pretty cool, actually. Build away.
So this is the follow-up, and I've tried to make it easy. There's a new video. This time the head gets a skull, which sits in the gap between the skin and the brain. Hence the title. Every step that made the video is below, and each card opens to show the code that does it. Every brick is priced at LEGO's own Pick a Brick shop. You can download the parts list, or the whole model as a file that BrickLink Studio opens.
None of it is special to my head. The database doesn't know it's a head. It gets a 3D model, fills it with bricks, colours them and counts them. Give it a different model and you get different bricks, and a parts list to go with them. Your own head, or anything else you like. The scripts are in the previous project. This page is just the explanation.
The answer to the second question is £1,009, by the way.
One script, video_skull.ps1, runs five programs one after another. Each coloured band below is one of them, and the labels between the cards say what gets handed on. Open any card to see the code.
SQL Server doesn't draw a single frame. It writes a script: a list of the bricks, each with up to four frame numbers saying when it arrives and when it leaves. That goes out once, as two CSV files. Blender has the lot before it draws frame 1, and from then on it asks every brick the same question: is this frame between your T1 and T2, or between your T3 and T4?
LegoSQL databaseSELECT Model = m.Name, Bricks = COUNT(*), Studs = SUM(b.SizeX * b.SizeZ) FROM lego.Brick AS b JOIN lego.Model AS m ON m.ModelID = b.ModelID WHERE m.ModelID IN (2, 4, 6) GROUP BY m.Name;
| Model | Bricks | Studs | |
|---|---|---|---|
| 1 | Trevor head (2x life size) | 4149 | 14544 |
| 2 | Trevor skull (2x life size) | 2347 | 10412 |
| 3 | Trevor brain in skull (2x life size) | 2173 | 5658 |
A stud is 8 mm and a layer is 9.6 mm, so the grid numbers are all Blender needs to place a brick. Every brick is one layer tall, which is why studs and voxels come to the same 30,614.
ROW_NUMBER() and turns each brick's place in the queue into a frame number: start + n / rate. Integer division does the pacing, so 14 brain bricks share each frame. Six steps, A to F, one for each act.
/* every brick gets an angle round the head, so each layer is laid in a ring */
Angle = ATN2(b.Z + b.SizeZ / 2.0 - @cz, b.X + b.SizeX / 2.0 - @cx + 1e-9), -- ATN2(0, 0) is an error
/* C. The brain, up the spinal cord */
DECLARE @StartC int = @EndB + @Hold / 2; -- 359
WITH C AS (SELECT T1, n = ROW_NUMBER() OVER (ORDER BY Y, Angle) - 1
FROM #T WHERE ModelID = @BrainModelID)
UPDATE C SET T1 = @StartC + n / @BrainRate; -- 359 + n / 14
| Step | Sets | Which bricks | ORDER BY | Per frame | Frames |
|---|---|---|---|---|---|
| A · build | T1 | head, Y ≤ 53 (@Stop) | Y, Angle | 18 | 1–205 |
| B · rewind | T2 | head, 12 < Y ≤ 53 | Y DESC, Angle DESC | 50 | 277–341 |
| C · brain | T1 | brain | Y, Angle | 14 | 359–514 |
| D · skull | T1 | skull | Y, Angle | 16 | 532–678 |
| E · skin | T3 | head, Y > 12 (@Back) | Y, Angle | 24 | 750–902 |
| F · cutaway | T4 head, T2 skull | InCut = 1 | head first, Y DESC | 8 | 938–1043 |
The gaps between the steps are @Hold frames (36, or 72 when doubled). Nothing in the script changes then, and the camera does the work.
-- a brick is on screen at frame @Frame when WHERE (@Frame >= T1 AND @Frame < T2) OR (@Frame >= T3 AND @Frame < T4) -- one real brick for each of the eight life stories SELECT BrickID, T1, T2, T3, T4 FROM lego.VideoScript WHERE BrickID IN (…);
SELECT Seq, Phase, FromFrame, ToFrame, Caption FROM lego.VideoPhase ORDER BY Seq;
video_skull.ps1 with sqlcmdlego.SkullVideoTimeline, exports both tables with sqlcmd, then starts Blender with no window and hands it the two file names.
Invoke-Sql "SELECT b.ModelID, b.X, b.Y, b.Z, b.SizeX, b.SizeZ, c.Hex,
ISNULL(s.T1, -1), ISNULL(s.T2, -1), ISNULL(s.T3, -1), ISNULL(s.T4, -1)
FROM lego.VideoScript AS s
JOIN lego.Brick AS b ON b.BrickID = s.BrickID
JOIN lego.Colour AS c ON c.ColourID = b.ColourID
ORDER BY COALESCE(s.T1, s.T3);" -OutFile $script
Invoke-Sql "SELECT Seq, Phase, FromFrame, ToFrame, Caption FROM lego.VideoPhase ORDER BY Seq;" -OutFile $phases
& $Blender -b --factory-startup -noaudio --python tools\render_video.py -- $script $frames `
--phases $phases --fstop 5.6 --view AgX --backdrop 3.5 --exposure 0.15 --res 1920x1080 --samples 64
The first three lines of script.csv. Columns: ModelID, X, Y, Z, SizeX, SizeZ, Hex, T1, T2, T3, T4.
2,15,1,31,8,2,D7BA8C,1,-1,-1,-1 2,21,2,29,8,2,D7BA8C,1,-1,-1,-1 2,23,1,31,6,2,D7BA8C,1,-1,-1,-1
That's a Tan 2 × 8 on layer 1 of the neck: it arrives on frame 1 and never leaves.
tools/render_video.pyscript.csv and builds two meshes that are only points: one per brick (8,669) and one per stud (30,614). Every point carries its brick's t1, t2, t3, t4, size and colour as attributes. -1 becomes NEVER, ten million.
STUD, LAYER = 0.008, 0.0096 # metres
NEVER = 1.0e7
def world(x, layer, z):
"""Lego grid -> Blender metres: Z up, the face looking down -Y."""
return ((x - cx) * STUD, -(z - cz) * STUD, (layer - y0) * LAYER)
for m, x, y, z, sx, sz, hx, t1, t2, t3, t4 in bricks:
times = {"t1": t(t1), "t2": t(t2), "t3": t(t3), "t4": t(t4)} # -1 -> NEVER
brick_pts.append((world(x + sx / 2, y + 0.5, z + sz / 2),
dict(times, col=linear(hx), size=(sx * STUD - GAP, sz * STUD - GAP, LAYER - GAP))))
for i in range(sx):
for j in range(sz): # a point for every stud
stud_pts.append((world(x + i + 0.5, y + 1, z + j + 0.5), dict(times, col=linear(hx))))
The bricks never become 8,669 objects. They stay as points with numbers attached, which is what keeps Blender fast.
phases.csv becomes a shot list. Each phase name has a camera position, and Blender keyframes it on that phase's last frame and eases in between. So lego.VideoPhase directs the camera as well as the captions.
SHOTS = { # yaw, elevation, distance (m), look-at height (fraction of the head)
"start": (-40, 30, 2.05, 0.45),
"build": (-15, 30, 2.05, 0.45),
"empty": (-10, 44, 1.80, 0.50), # look down into the empty skull
"rewind": (0, 28, 2.05, 0.40),
"brain": (25, 22, 1.95, 0.48),
"skull": (8, 12, 1.75, 0.50), # turn to face the skull
"eyes": (0, 8, 1.70, 0.54), # push in on the eyes
"skin": (30, 22, 2.00, 0.45),
"cutaway": (50, 26, 1.75, 0.55), # the cut faces the camera
"end": (80, 22, 1.85, 0.50),
}
keys = [(1, SHOTS["start"])] + [(to, SHOTS[name]) for name, _, to in phases]
for frame, (yaw, elev, dist, look) in keys:
pivot.rotation_euler = (math.radians(-elev), 0, math.radians(yaw))
pivot.location = (0, 0, top * look)
cam.location = (0, -dist, 0)
pivot.keyframe_insert("rotation_euler", frame=frame)
lego.VideoScript, rebuilt out of nodes.
# (frame >= T1 and frame < T2) or (frame >= T3 and frame < T4)
frame = N.new("GeometryNodeInputSceneTime").outputs["Frame"]
second = compare("GREATER_EQUAL", frame, t3)
visible = boolean("OR",
boolean("AND", compare("GREATER_EQUAL", frame, t1), compare("LESS_THAN", frame, t2)),
boolean("AND", second, compare("LESS_THAN", frame, t4)))
Lk.new(visible, inst.inputs["Selection"]) # only visible points get a brick
# drop in: 1 -> 0 over 6 frames after appearing, lift out: 0 -> 1 over 6 frames before leaving
appear = t1 + (t3 - t1) * second
drop_in = clamp((appear + DROP_FRAMES - frame) / DROP_FRAMES)
leave = t2 + (t4 - t2) * second
lift = clamp((frame - (leave - DROP_FRAMES)) / DROP_FRAMES)
translation.z = max(drop_in, lift) ** 2 * DROP # DROP = 3.5 cm
frame_0470.png and moves on. Frames already on disk are skipped, so a stopped render carries on where it left off.
scene.render.engine = "CYCLES" scene.cycles.samples = 64 scene.cycles.device = "GPU" # OptiX on the RTX 5090 scene.cycles.denoiser = "OPTIX" scene.view_settings.view_transform = "AgX" cam.data.dof.focus_object = pivot # f/5.6, focused on the head floor.is_shadow_catcher = True scene.render.filepath = os.path.join(a.out, "frame_") scene.render.use_overwrite = False # re-running carries on where it stopped bpy.ops.render.render(animation=True)
tools/caption_frames.pyfor start, end, caption in phases:
if start <= n <= end:
alpha = min(1.0, (n - start + 1) / FADE, (end - n + 1) / FADE)
d.rounded_rectangle(box, radius=pad // 2, fill=(30, 30, 30, round(215 * alpha)))
for kind, t in tokens(caption): # T-SQL syntax colours
d.text((x, y), t, font=mono, fill=COLOURS[kind] + (round(255 * alpha),))
x += mono.getlength(t)
& ffmpeg -y -loglevel error -framerate 30 -i (Join-Path $captioned 'frame_%04d.png') `
-c:v libx264 -pix_fmt yuv420p -crf 18 -preset slow -movflags +faststart $mp4
sql/11_PartPrice.sqlThis is the bit people asked about. I've priced every brick at LEGO's Pick a Brick shop in the UK, using the prices on 23 November 2026. It's one price per part. Where LEGO charges a range across colours (the 2 × 4 and 2 × 6), I've used the standard, higher price, because that's what Tan and Medium Nougat cost.
SELECT Model = ISNULL(m.Name, N'All three'), Bricks = COUNT(*),
Studs = SUM(b.SizeX * b.SizeZ), CostGBP = SUM(pp.PriceGBP)
FROM lego.Brick AS b
JOIN lego.Model AS m ON m.ModelID = b.ModelID
JOIN lego.PartPrice AS pp ON pp.PartID = b.PartID
WHERE b.ModelID IN (2, 4, 6)
GROUP BY ROLLUP (m.Name);
| Model | Bricks | Studs | CostGBP | |
|---|---|---|---|---|
| 1 | Trevor brain in skull (2x life size) | 2173 | 5658 | 213.96 |
| 2 | Trevor head (2x life size) | 4149 | 14544 | 481.58 |
| 3 | Trevor skull (2x life size) | 2347 | 10412 | 313.93 |
| 4 | All three | 8669 | 30614 | 1009.47 |
SELECT TOP (5) * FROM lego.KitPartsList(2, 4, 6) ORDER BY TotalGBP DESC;
| PartID | Part | ColourID | Colour | Head | Skull | Brain | Quantity | PriceGBP | TotalGBP | |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3007 | Brick 2 x 8 | 15 | White | 0 | 154 | 0 | 154 | .35 | 53.90 |
| 2 | 3003 | Brick 2 x 2 | 15 | White | 3 | 426 | 0 | 429 | .12 | 51.48 |
| 3 | 3004 | Brick 1 x 2 | 15 | White | 15 | 430 | 0 | 445 | .10 | 44.50 |
| 4 | 3001 | Brick 2 x 4 | 15 | White | 0 | 190 | 0 | 190 | .19 | 36.10 |
| 5 | 3004 | Brick 1 x 2 | 71 | Light Bluish Grey | 286 | 68 | 0 | 354 | .10 | 35.40 |
Per brick the skull is the dearest at 13.4p, against 11.6p for the head and 9.8p for the brain. It's nearly all one colour, so the merge can use long bricks, including 154 white 2 × 8s at 35p each. The head's colour changes chop it into cheap 1 × 1s and 1 × 2s.
| Part | Each | Bricks | Colours used | On Pick a Brick | Total | |
|---|---|---|---|---|---|---|
3004 | Brick 1 × 2 | £0.10 | 2,235 | 16 | 43 | £223.50 |
3003 | Brick 2 × 2 | £0.12 | 1,404 | 13 | 39 | £168.48 |
3005 | Brick 1 × 1 | £0.06 | 2,727 | 15 | 42 | £163.62 |
3007 | Brick 2 × 8 | £0.35 | 356 | 8 | 13 | £124.60 |
3001 | Brick 2 × 4 | £0.19 | 511 | 12 | 37 | £97.09 |
3002 | Brick 2 × 3 | £0.16 | 597 | 12 | 20 | £95.52 |
3622 | Brick 1 × 3 | £0.12 | 369 | 14 | 32 | £44.28 |
2456 | Brick 2 × 6 | £0.28 | 150 | 9 | 28 | £42.00 |
3010 | Brick 1 × 4 | £0.13 | 230 | 12 | 40 | £29.90 |
3009 | Brick 1 × 6 | £0.22 | 56 | 7 | 30 | £12.32 |
3008 | Brick 1 × 8 | £0.24 | 34 | 7 | 18 | £8.16 |
| 125 part and colour lines | 8,669 | £1,009.47 |
"On Pick a Brick" is how many colours LEGO lists that part in. It doesn't stock every size in every colour: the 2 × 8 comes in only 13, so a real order would fill some lines from BrickLink, usually for a little more.
Set prices: Brickset data for 14,752 sets from 1979 to 2019, in sets-brickset.csv from iechevarria/lego-price-analysis. The median takes UK-priced sets of 100 pieces or more.
The kit has the model as an LDraw file, the parts list as CSV and a README. LDraw is the open file format the LEGO CAD world runs on: one line per brick (colour, position, rotation, part), with a 0 STEP after every layer so it reads as building instructions. BrickLink Studio, LeoCAD and LDView all open it, and Studio can turn it into a BrickLink wanted list. The CSV has the same columns as lego.KitPartsList, with LDraw part and colour numbers.
This is the video again, but running in your browser from the same script Blender read. Same rule for which bricks are showing, same 6-frame drop. Switch to Blender render for the real frames. Leave Show the SQL ticked and the panel keeps up with the frame: the step that scheduled whatever is moving, the query for what's on screen, and one brick worked through the Geometry Nodes.
The browser replay draws plain boxes and studs with WebGL and uses Blender's camera keyframes, eased the same way. It shows what Geometry Nodes hands to Cycles before any path tracing happens. The Blender render view is the finished video, scaled to 720p, with the captions caption_frames.py drew.
Every row in lego.VideoScript has room for two lives on screen: T1 to T2, then T3 to T4. A NULL end means the brick stays to the end. I expected this to get complicated, but eight patterns cover all 8,669 bricks. Each bar below is one real brick from its group, drawn against the nine phases. Click a row to find that brick in the replay.
Hope you enjoyed the article. If you do build a head, yours or anybody else's, send me a picture.
Trevor (@sqlartist)