Photo-Reference 3D Print Painter
Automatically transfer exact, crisp, solid filament colors from a single reference photograph onto a monochrome 3D printable mesh in Blender via MCP — producing clean multi-material meshes ready for Bambu Studio / OrcaSlicer AMS (.3mf) or No-AMS part splitting (no-ams-color-split).
Helper Library: Import directly from scripts/photo_3d_painter.py inside
execute_blender_code(zero externalpipdependencies — uses native Blenderbpy,bmesh,mathutils.bvhtree,numpy, andzipfile).
Pipeline Overview
1. Inspect Photo & Mesh → Identify physical filament palette (N solid colors) + viewing angle + subject crop
2. Camera Alignment → Project 3D mesh along matching view_dir → map projected bbox onto photo subject crop
3. Occlusion-Safe Sample → BVH visibility raycast so front pixels NEVER bleed through onto occluded backfaces
4. CIELAB Quantization → Map sampled pixels to physical filament palette in perceptual Lab space (shadow-safe!)
5. Symmetry & Back Fill → Mirror crisp lit side (optional) + geodesic BFS flood-fill for unseen back/sides
6. Denoise & Smooth → Remove speckled micro-islands + smooth jagged boundaries (protecting micro highlights)
7. Visual QA & Export → Mandatory 6-angle + close-up `look` → Export multi-color `.3mf` or hand off to splitter
Step-by-Step Workflow
Step 1: Analyze Reference Photo & Define Filament Palette
Examine the user's reference photo carefully and define an explicit, compact physical filament palette (3–8 solid colors, ordered cleanly):
- Example (Husky clicker):
White,Black,Cyan,Pink(plus optional glossy variants for preview). - Avoid gradient bloat: Physical FDM prints use discrete plastic spools — never quantize into 20 shades of gray. Let CIELAB handle light/shadow variations cleanly.
Step 2: Match View Direction & Subject Crop Box (crop_uv)
- Estimate the photo's camera direction
view_dirpointing from the model toward the camera (e.g.,(0.0, -1.0, 0.22)for straight front slightly elevated, or(-0.7, -0.7, 0.25)for front-corner 3/4). - Determine the subject's normalized UV bounding box
crop_uv = (u_min, u_max, v_min, v_max)in Blender image coordinates (v = 0at bottom,v = 1at top):- Clean isolated photo (transparent/solid background): Pass
crop_uv=Noneto auto-detect tight foreground bounds viaauto_detect_subject_crop(). - Photo containing other props (e.g., character inside a box/stand): Measure or supply the exact
crop_uvof the target mesh only (excluding the box/table!) AND optionally setproj_z_range=(z_min, z_max)to align strictly to the visible portion of the 3D mesh (e.g., head above the box rim!).
- Clean isolated photo (transparent/solid background): Pass
Step 3: Run Projection, CIELAB Quantization & Cleanup
Call project_photo_to_mesh(...) from scripts/photo_3d_painter.py:
- Shadow-Resistant CIELAB (
chroma_weight=1.35,shadow_white_L=58.0): Neutral gray shadows on white plastic stay White while studio key-light sheen on matte black plastic (L* ≈ 45–54) stays Black; colored accents (pink tongue, cyan eyes) stay crisp even under soft shadow. - BVH Occlusion Check: Every face center is raycast toward the camera; any face facing away (
normal.dot(view_dir) < 0.14) or blocked by a foreground surface is marked unseen (-1). - Symmetry Reinforcement (
symmetry_x=True): For symmetric characters aligned to-Y(canonical_rot_z_deg), mirrors labels acrossX = 0(preferring better-lit / seen faces) so slight photo perspective tilts never create asymmetric color drift. - Backface & Base Fill:
- Unseen back/top regions are filled cleanly via
back_default_idx(e.g., Black fur on the back of a Husky head) or smooth geodesic BFS propagation from visible boundary colors. - Optional
z_floor_override=(z_max, color_idx)forces hidden lower mechanical collars/skirts (e.g.,Z < 6.5 mmMX switch base) to a clean solid color.
- Unseen back/top regions are filled cleanly via
- Median Pixel Sampling, Island Denoising & Boundary Smoothing:
sample_radius_px=2(5×5 median filter) eliminates single-pixel JPEG/specular noise at sampling time.min_island_faces=120removes tiny specular glare islands while preserving valid features (like white eye highlights on dense meshes).smooth_passes=4straightens stair-stepped triangle boundaries.
Step 4: Mandatory Visual QA (look)
Run look(..., shading="solid") from 6 angles (front, front-low, left-3/4, right-3/4, top, back) plus a close-up of eyes/mouth/seams:
- Verify eyes, pupils, highlights, nose, mouth, and markings align to the sculpted 3D grooves (if offset slightly, adjust
crop_uvorview_dirby0.01–0.03and re-run — takes ~3 seconds!). - Verify zero stray color speckles on forehead, ears, or back of head.
Step 5: Export for Target 3D Print Workflow
- AMS / Multi-Material Slicers (Bambu Studio / OrcaSlicer / PrusaSlicer):
Call
export_multicolor_3mf(obj, filepath, apply_modifiers=True)to write a valid.3mfarchive with<basematerials>+ per-triangle material indices AND Bambu/Orcapaint_colorattributes! - No-AMS Single-Extruder Printing:
Hand off the color-mapped mesh directly to the
no-ams-color-splitskill to generate interlocking, flat-backed0.18 mm-clearance STL parts!
Hard Constraints
MUST DO
- Always work in
1 BU = 1 mmand verifynon_manifold == 0before.3mfexport. - Always set
mat.diffuse_colormatching each filament RGB so Blender viewportsolidshading shows accurate colors. - Always run a visual
lookverification after projection and tunecrop_uv/view_dirif features shift off sculpted grooves. - Keep
SM_*backup geometry untouched or save.blendafter verification.
MUST NOT DO
- Never compare raw RGB Euclidean distance alone on lit photographs — white plastic in shadow will misclassify as black/gray. Always quantize in CIELAB with chroma weighting.
- Never project front pixels onto backfaces without a BVH occlusion + normal-facing check.
- Never export standard
.stlclaiming it carries color — STL is geometry-only; export.3mf(or split multi-STL viano-ams-color-split).
Reference Files
- Reusable Blender Python Engine: scripts/photo_3d_painter.py
- CIELAB Tuning, Occlusion & Bambu
.3mfSpec: references/projection-and-3mf-spec.md - End-to-End Example Recipe: examples/paint_husky_from_photo.py