{"slug": "agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01", "title": "Agentic Blender (bpy + bmesh) visual feedback loop workflow & prompt (HADLEY ST-01)", "summary": "A developer published a Blender Visual Feedback Loop (VFL) script that captures a deterministic six-view contact sheet and close-up renders of a 3D asset using only Blender's bundled bpy, mathutils, and numpy modules. The tool, designed to run inside Blender MCP via execute_blender_code, supports Blender 3.6 through 5.x and offers clay-render modes with configurable dark, mid, and light backgrounds plus a built-in 5x7 bitmap font for view labels. It is intended to give agentic modeling workflows a repeatable visual inspection grid.", "body_md": "|  | \"\"\" | \n|  | Blender Visual Feedback Loop (VFL) — Deterministic Multi-Angle Contact Sheet & Close-Up Capture. | \n|  |  | \n|  | Zero external dependencies beyond Blender's bundled bpy, mathutils, and numpy. | \n|  | Compatible with Blender 3.6, 4.x, and 5.x. | \n|  |  | \n|  | Usage inside Blender MCP (execute_blender_code): | \n|  | import sys, importlib | \n|  | sys.path.insert(0, r\"/path/to/vfl/scripts\") | \n|  | import vfl_capture; importlib.reload(vfl_capture) | \n|  | out = vfl_capture.capture_contact_sheet( | \n|  | output_path=r\"/tmp/vfl_stage1.png\", | \n|  | target_collection=\"Asset_MyModel\", | \n|  | mode=\"clay\", | \n|  | ) | \n|  | \"\"\" | \n|  |  | \n|  | import math | \n|  | import os | \n|  | import shutil | \n|  | import tempfile | \n|  | import bpy | \n|  | import numpy as np | \n|  | from mathutils import Matrix, Vector | \n|  |  | \n|  | STUDIO_COL_NAME = \"_VFL_Studio\" | \n|  | _TEMP_PREFIX = \"_VFL_TMP_\" | \n|  |  | \n|  | # Canonical 6-view inspection grid (3 columns x 2 rows) | \n|  | # Azimuth convention (Blender front = -Y, right = +X, up = +Z): | \n|  | #   0 deg = Front (-Y), +90 deg = Right (+X), 180 deg = Back (+Y), -90 deg = Left (-X) | \n|  | DEFAULT_VIEWS = [ | \n|  | {\"label\": \"3/4 FRONT\", \"type\": \"PERSP\", \"az\": 35.0, \"el\": 22.0, \"lens\": 50.0}, | \n|  | {\"label\": \"3/4 REAR\",  \"type\": \"PERSP\", \"az\": -145.0, \"el\": 22.0, \"lens\": 50.0}, | \n|  | {\"label\": \"TOP (+Z)\",  \"type\": \"ORTHO\", \"az\": 0.0, \"el\": 90.0, \"up\": (0.0, 1.0, 0.0)}, | \n|  | {\"label\": \"FRONT (-Y)\", \"type\": \"ORTHO\", \"az\": 0.0, \"el\": 0.0}, | \n|  | {\"label\": \"RIGHT (+X)\", \"type\": \"ORTHO\", \"az\": 90.0, \"el\": 0.0}, | \n|  | {\"label\": \"BACK (+Y)\",  \"type\": \"ORTHO\", \"az\": 180.0, \"el\": 0.0}, | \n|  | ] | \n|  |  | \n|  | _BG_PRESETS = { | \n|  | \"dark\":  {\"world\": (0.045, 0.050, 0.060, 1.0), \"world_str\": 0.60, \"floor\": (0.075, 0.080, 0.090, 1.0)}, | \n|  | \"mid\":   {\"world\": (0.170, 0.190, 0.220, 1.0), \"world_str\": 0.85, \"floor\": (0.130, 0.145, 0.165, 1.0)}, | \n|  | \"light\": {\"world\": (0.460, 0.485, 0.520, 1.0), \"world_str\": 1.00, \"floor\": (0.360, 0.380, 0.405, 1.0)}, | \n|  | } | \n|  |  | \n|  | # Minimal 5x7 uppercase bitmap font (each int is a 5-bit row, MSB on left) | \n|  | _FONT_5X7 = { | \n|  | \"A\": (0x0E, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11), | \n|  | \"B\": (0x1E, 0x11, 0x11, 0x1E, 0x11, 0x11, 0x1E), | \n|  | \"C\": (0x0E, 0x11, 0x10, 0x10, 0x10, 0x11, 0x0E), | \n|  | \"D\": (0x1C, 0x12, 0x11, 0x11, 0x11, 0x12, 0x1C), | \n|  | \"E\": (0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x1F), | \n|  | \"F\": (0x1F, 0x10, 0x10, 0x1E, 0x10, 0x10, 0x10), | \n|  | \"G\": (0x0E, 0x11, 0x10, 0x17, 0x11, 0x11, 0x0F), | \n|  | \"H\": (0x11, 0x11, 0x11, 0x1F, 0x11, 0x11, 0x11), | \n|  | \"I\": (0x0E, 0x04, 0x04, 0x04, 0x04, 0x04, 0x0E), | \n|  | \"J\": (0x07, 0x02, 0x02, 0x02, 0x12, 0x12, 0x0C), | \n|  | \"K\": (0x11, 0x12, 0x14, 0x18, 0x14, 0x12, 0x11), | \n|  | \"L\": (0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1F), | \n|  | \"M\": (0x11, 0x1B, 0x15, 0x15, 0x11, 0x11, 0x11), | \n|  | \"N\": (0x11, 0x19, 0x15, 0x13, 0x11, 0x11, 0x11), | \n|  | \"O\": (0x0E, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E), | \n|  | \"P\": (0x1E, 0x11, 0x11, 0x1E, 0x10, 0x10, 0x10), | \n|  | \"Q\": (0x0E, 0x11, 0x11, 0x11, 0x15, 0x12, 0x0D), | \n|  | \"R\": (0x1E, 0x11, 0x11, 0x1E, 0x14, 0x12, 0x11), | \n|  | \"S\": (0x0F, 0x10, 0x10, 0x0E, 0x01, 0x01, 0x1E), | \n|  | \"T\": (0x1F, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04), | \n|  | \"U\": (0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x0E), | \n|  | \"V\": (0x11, 0x11, 0x11, 0x11, 0x11, 0x0A, 0x04), | \n|  | \"W\": (0x11, 0x11, 0x11, 0x15, 0x15, 0x1B, 0x11), | \n|  | \"X\": (0x11, 0x11, 0x0A, 0x04, 0x0A, 0x11, 0x11), | \n|  | \"Y\": (0x11, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04), | \n|  | \"Z\": (0x1F, 0x01, 0x02, 0x04, 0x08, 0x10, 0x1F), | \n|  | \"0\": (0x0E, 0x11, 0x13, 0x15, 0x19, 0x11, 0x0E), | \n|  | \"1\": (0x04, 0x0C, 0x04, 0x04, 0x04, 0x04, 0x0E), | \n|  | \"2\": (0x0E, 0x11, 0x01, 0x06, 0x08, 0x10, 0x1F), | \n|  | \"3\": (0x1E, 0x01, 0x01, 0x0E, 0x01, 0x01, 0x1E), | \n|  | \"4\": (0x02, 0x06, 0x0A, 0x12, 0x1F, 0x02, 0x02), | \n|  | \"5\": (0x1F, 0x10, 0x1E, 0x01, 0x01, 0x11, 0x0E), | \n|  | \"6\": (0x06, 0x08, 0x10, 0x1E, 0x11, 0x11, 0x0E), | \n|  | \"7\": (0x1F, 0x01, 0x02, 0x04, 0x08, 0x08, 0x08), | \n|  | \"8\": (0x0E, 0x11, 0x11, 0x0E, 0x11, 0x11, 0x0E), | \n|  | \"9\": (0x0E, 0x11, 0x11, 0x0F, 0x01, 0x02, 0x0C), | \n|  | \" \": (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00), | \n|  | \"/\": (0x01, 0x02, 0x02, 0x04, 0x08, 0x08, 0x10), | \n|  | \"-\": (0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00), | \n|  | \"+\": (0x00, 0x04, 0x04, 0x1F, 0x04, 0x04, 0x00), | \n|  | \"(\": (0x02, 0x04, 0x08, 0x08, 0x08, 0x04, 0x02), | \n|  | \")\": (0x08, 0x04, 0x02, 0x02, 0x02, 0x04, 0x08), | \n|  | \"[\": (0x0E, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0E), | \n|  | \"]\": (0x0E, 0x02, 0x02, 0x02, 0x02, 0x02, 0x0E), | \n|  | \":\": (0x00, 0x04, 0x04, 0x00, 0x04, 0x04, 0x00), | \n|  | \".\": (0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04), | \n|  | \",\": (0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x08), | \n|  | \"_\": (0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F), | \n|  | } | \n|  |  | \n|  |  | \n|  | def _pick_eevee_engine(): | \n|  | \"\"\"Return the valid EEVEE engine identifier across Blender 3.6, 4.x, and 5.x.\"\"\" | \n|  | prop = bpy.types.RenderSettings.bl_rna.properties[\"engine\"] | \n|  | valid = {item.identifier for item in prop.enum_items} | \n|  | for candidate in (\"BLENDER_EEVEE_NEXT\", \"BLENDER_EEVEE\", \"CYCLES\", \"BLENDER_WORKBENCH\"): | \n|  | if candidate in valid: | \n|  | return candidate | \n|  | return next(iter(valid)) | \n|  |  | \n|  |  | \n|  | def _set_input(node, names, value): | \n|  | for name in names: | \n|  | sock = node.inputs.get(name) | \n|  | if sock is not None: | \n|  | sock.default_value = value | \n|  | return sock | \n|  | return None | \n|  |  | \n|  |  | \n|  | def _ensure_node_tree(datablock): | \n|  | if hasattr(datablock, \"use_nodes\") and not datablock.use_nodes: | \n|  | datablock.use_nodes = True | \n|  | return datablock.node_tree | \n|  |  | \n|  |  | \n|  | def _collect_collection_meshes(col): | \n|  | meshes = [] | \n|  | for obj in col.objects: | \n|  | if obj.type == \"MESH\" and not obj.hide_viewport and not obj.hide_render: | \n|  | meshes.append(obj) | \n|  | for child in col.children: | \n|  | if child.name != STUDIO_COL_NAME: | \n|  | meshes.extend(_collect_collection_meshes(child)) | \n|  | return meshes | \n|  |  | \n|  |  | \n|  | def _resolve_target_meshes(target_collection=None, target_objects=None): | \n|  | if target_objects is not None: | \n|  | resolved = [] | \n|  | for item in target_objects: | \n|  | obj = bpy.data.objects.get(item) if isinstance(item, str) else item | \n|  | if obj is not None and obj.type == \"MESH\": | \n|  | resolved.append(obj) | \n|  | if not resolved: | \n|  | raise ValueError(f\"No valid MESH objects found in target_objects={target_objects!r}\") | \n|  | return resolved | \n|  |  | \n|  | if target_collection is not None: | \n|  | col = bpy.data.collections.get(target_collection) if isinstance(target_collection, str) else target_collection | \n|  | if col is None: | \n|  | raise ValueError(f\"Collection not found: {target_collection!r}\") | \n|  | meshes = _collect_collection_meshes(col) | \n|  | if not meshes: | \n|  | raise ValueError(f\"No visible MESH objects in collection {col.name!r}\") | \n|  | return meshes | \n|  |  | \n|  | studio_col = bpy.data.collections.get(STUDIO_COL_NAME) | \n|  | studio_objs = set(studio_col.all_objects) if studio_col else set() | \n|  | meshes = [ | \n|  | obj for obj in bpy.context.scene.objects | \n|  | if obj.type == \"MESH\" | \n|  | and obj not in studio_objs | \n|  | and not obj.hide_viewport | \n|  | and not obj.hide_render | \n|  | ] | \n|  | if not meshes: | \n|  | raise ValueError(\"No visible MESH objects found in the current scene.\") | \n|  | return meshes | \n|  |  | \n|  |  | \n|  | def _compute_world_bounds(mesh_objects): | \n|  | \"\"\"Compute evaluated world-space AABB (respecting modifiers) for mesh_objects.\"\"\" | \n|  | depsgraph = bpy.context.evaluated_depsgraph_get() | \n|  | xs, ys, zs = [], [], [] | \n|  |  | \n|  | for obj in mesh_objects: | \n|  | eval_obj = obj.evaluated_get(depsgraph) | \n|  | if eval_obj.data is None or len(eval_obj.data.vertices) == 0: | \n|  | continue | \n|  | mw = eval_obj.matrix_world | \n|  | for corner in eval_obj.bound_box: | \n|  | wc = mw @ Vector(corner) | \n|  | xs.append(wc.x) | \n|  | ys.append(wc.y) | \n|  | zs.append(wc.z) | \n|  |  | \n|  | if not xs: | \n|  | raise ValueError(\"Target mesh objects have zero vertices.\") | \n|  |  | \n|  | bmin = Vector((min(xs), min(ys), min(zs))) | \n|  | bmax = Vector((max(xs), max(ys), max(zs))) | \n|  | center = (bmin + bmax) * 0.5 | \n|  | size = bmax - bmin | \n|  | radius = max(size.length * 0.5, 0.05) | \n|  | corners = [ | \n|  | Vector((x, y, z)) | \n|  | for x in (bmin.x, bmax.x) | \n|  | for y in (bmin.y, bmax.y) | \n|  | for z in (bmin.z, bmax.z) | \n|  | ] | \n|  | return bmin, bmax, center, size, radius, corners | \n|  |  | \n|  |  | \n|  | def _dir_from_az_el(az_deg, el_deg): | \n|  | \"\"\"Unit vector from target center to camera (az=0 -> -Y front, +90 -> +X right).\"\"\" | \n|  | az = math.radians(az_deg) | \n|  | el = math.radians(el_deg) | \n|  | ce = math.cos(el) | \n|  | return Vector((math.sin(az) * ce, -math.cos(az) * ce, math.sin(el))).normalized() | \n|  |  | \n|  |  | \n|  | def _camera_basis(forward, up_hint=None): | \n|  | \"\"\"Return right-handed orthonormal (right, up, -forward) and 3x3 rotation matrix.\"\"\" | \n|  | fwd = forward.normalized() | \n|  | if up_hint is not None: | \n|  | up_ref = Vector(up_hint).normalized() | \n|  | elif abs(fwd.z) > 0.995: | \n|  | up_ref = Vector((0.0, 1.0, 0.0)) | \n|  | else: | \n|  | up_ref = Vector((0.0, 0.0, 1.0)) | \n|  |  | \n|  | right = fwd.cross(up_ref) | \n|  | if right.length < 1e-6: | \n|  | right = fwd.cross(Vector((1.0, 0.0, 0.0))) | \n|  | right.normalize() | \n|  | up = right.cross(fwd).normalized() | \n|  | rot = Matrix((right, up, -fwd)).transposed() | \n|  | return right, up, rot | \n|  |  | \n|  |  | \n|  | def _position_camera(cam_obj, center, corners, radius, view_cfg, aspect, padding=1.22, clip_radius=None): | \n|  | cam_dir = _dir_from_az_el(view_cfg[\"az\"], view_cfg[\"el\"]) | \n|  | forward = -cam_dir | \n|  | right, up, rot = _camera_basis(forward, view_cfg.get(\"up\")) | \n|  |  | \n|  | cam_data = cam_obj.data | \n|  | cam_data.sensor_fit = \"HORIZONTAL\" | \n|  | cam_data.sensor_width = 36.0 | \n|  | far_ref = max(radius, float(clip_radius or radius)) | \n|  | cam_data.clip_start = max(0.005, radius * 0.005) | \n|  | cam_data.clip_end = max(100.0, far_ref * 60.0) | \n|  |  | \n|  | if view_cfg[\"type\"] == \"ORTHO\": | \n|  | cam_data.type = \"ORTHO\" | \n|  | xs = [(c - center).dot(right) for c in corners] | \n|  | ys = [(c - center).dot(up) for c in corners] | \n|  | span_x = max(xs) - min(xs) | \n|  | span_y = max(ys) - min(ys) | \n|  | cam_data.ortho_scale = max(span_x, span_y * aspect, 0.1) * padding | \n|  | dist = radius * 4.0 + 1.0 | \n|  | else: | \n|  | cam_data.type = \"PERSP\" | \n|  | lens = float(view_cfg.get(\"lens\", 50.0)) | \n|  | cam_data.lens = lens | \n|  | half_tan_x = (cam_data.sensor_width * 0.5) / lens | \n|  | half_tan_y = half_tan_x / aspect | \n|  | dist = radius * 2.5 | \n|  | req = [] | \n|  | for c in corners: | \n|  | v = c - center | \n|  | px = abs(v.dot(right)) | \n|  | py = abs(v.dot(up)) | \n|  | # v.dot(forward) is negative for corners closer to the camera | \n|  | pz = v.dot(forward) | \n|  | d_need = -pz + max(px / half_tan_x, py / half_tan_y) * padding | \n|  | req.append(d_need) | \n|  | dist = max(max(req), radius * 1.1) | \n|  |  | \n|  | loc = center + cam_dir * dist | \n|  | mat = rot.to_4x4() | \n|  | mat.translation = loc | \n|  | cam_obj.matrix_world = mat | \n|  |  | \n|  |  | \n|  | def _build_diagnostic_material(mode, radius): | \n|  | mat = bpy.data.materials.new(name=f\"{_TEMP_PREFIX}Mat_{mode}\") | \n|  | nt = _ensure_node_tree(mat) | \n|  | nt.nodes.clear() | \n|  |  | \n|  | out = nt.nodes.new(\"ShaderNodeOutputMaterial\") | \n|  |  | \n|  | if mode == \"clay\": | \n|  | bsdf = nt.nodes.new(\"ShaderNodeBsdfPrincipled\") | \n|  | _set_input(bsdf, (\"Roughness\",), 0.58) | \n|  | _set_input(bsdf, (\"Specular IOR Level\", \"Specular\"), 0.35) | \n|  | _set_input(bsdf, (\"Metallic\",), 0.0) | \n|  |  | \n|  | ao = nt.nodes.new(\"ShaderNodeAmbientOcclusion\") | \n|  | ao.samples = 8 | \n|  | ao.inputs[\"Distance\"].default_value = max(0.05, radius * 0.28) | \n|  |  | \n|  | mr = nt.nodes.new(\"ShaderNodeMapRange\") | \n|  | mr.inputs[\"From Min\"].default_value = 0.0 | \n|  | mr.inputs[\"From Max\"].default_value = 1.0 | \n|  | mr.inputs[\"To Min\"].default_value = 0.38 | \n|  | mr.inputs[\"To Max\"].default_value = 1.0 | \n|  |  | \n|  | vm = nt.nodes.new(\"ShaderNodeVectorMath\") | \n|  | vm.operation = \"SCALE\" | \n|  | vm.inputs[0].default_value = (0.58, 0.56, 0.53) | \n|  |  | \n|  | nt.links.new(ao.outputs[\"AO\"], mr.inputs[\"Value\"]) | \n|  | nt.links.new(mr.outputs[\"Result\"], vm.inputs[\"Scale\"]) | \n|  | nt.links.new(vm.outputs[\"Vector\"], bsdf.inputs[\"Base Color\"]) | \n|  | nt.links.new(bsdf.outputs[\"BSDF\"], out.inputs[\"Surface\"]) | \n|  |  | \n|  | elif mode == \"wireframe\": | \n|  | bsdf = nt.nodes.new(\"ShaderNodeBsdfPrincipled\") | \n|  | _set_input(bsdf, (\"Base Color\",), (0.68, 0.69, 0.71, 1.0)) | \n|  | _set_input(bsdf, (\"Roughness\",), 0.65) | \n|  | _set_input(bsdf, (\"Specular IOR Level\", \"Specular\"), 0.25) | \n|  |  | \n|  | wire = nt.nodes.new(\"ShaderNodeWireframe\") | \n|  | wire.use_pixel_size = True | \n|  | wire.inputs[\"Size\"].default_value = 1.25 | \n|  |  | \n|  | em = nt.nodes.new(\"ShaderNodeEmission\") | \n|  | em.inputs[\"Color\"].default_value = (0.03, 0.04, 0.06, 1.0) | \n|  | em.inputs[\"Strength\"].default_value = 1.0 | \n|  |  | \n|  | mix = nt.nodes.new(\"ShaderNodeMixShader\") | \n|  | nt.links.new(wire.outputs[\"Fac\"], mix.inputs[\"Fac\"]) | \n|  | nt.links.new(bsdf.outputs[\"BSDF\"], mix.inputs[1]) | \n|  | nt.links.new(em.outputs[\"Emission\"], mix.inputs[2]) | \n|  | nt.links.new(mix.outputs[\"Shader\"], out.inputs[\"Surface\"]) | \n|  |  | \n|  | elif mode == \"normals\": | \n|  | geom = nt.nodes.new(\"ShaderNodeNewGeometry\") | \n|  |  | \n|  | front_bsdf = nt.nodes.new(\"ShaderNodeBsdfPrincipled\") | \n|  | _set_input(front_bsdf, (\"Base Color\",), (0.14, 0.44, 0.88, 1.0)) | \n|  | _set_input(front_bsdf, (\"Roughness\",), 0.55) | \n|  |  | \n|  | back_em = nt.nodes.new(\"ShaderNodeEmission\") | \n|  | back_em.inputs[\"Color\"].default_value = (1.0, 0.04, 0.04, 1.0) | \n|  | back_em.inputs[\"Strength\"].default_value = 1.8 | \n|  |  | \n|  | mix = nt.nodes.new(\"ShaderNodeMixShader\") | \n|  | nt.links.new(geom.outputs[\"Backfacing\"], mix.inputs[\"Fac\"]) | \n|  | nt.links.new(front_bsdf.outputs[\"BSDF\"], mix.inputs[1]) | \n|  | nt.links.new(back_em.outputs[\"Emission\"], mix.inputs[2]) | \n|  | nt.links.new(mix.outputs[\"Shader\"], out.inputs[\"Surface\"]) | \n|  |  | \n|  | return mat | \n|  |  | \n|  |  | \n|  | def _build_studio_world(bg_tone): | \n|  | preset = _BG_PRESETS.get(bg_tone, _BG_PRESETS[\"mid\"]) | \n|  | world = bpy.data.worlds.new(name=f\"{_TEMP_PREFIX}World\") | \n|  | nt = _ensure_node_tree(world) | \n|  | nt.nodes.clear() | \n|  | bg = nt.nodes.new(\"ShaderNodeBackground\") | \n|  | bg.inputs[\"Color\"].default_value = preset[\"world\"] | \n|  | bg.inputs[\"Strength\"].default_value = preset[\"world_str\"] | \n|  | out = nt.nodes.new(\"ShaderNodeOutputWorld\") | \n|  | nt.links.new(bg.outputs[\"Background\"], out.inputs[\"Surface\"]) | \n|  | return world | \n|  |  | \n|  |  | \n|  | def _create_area_light(col, name, center, radius, az_deg, el_deg, dist_mult, size_mult, energy_mult, color): | \n|  | ldata = bpy.data.lights.new(name=name, type=\"AREA\") | \n|  | ldata.shape = \"RECTANGLE\" | \n|  | ldata.size = max(0.3, radius * size_mult) | \n|  | ldata.size_y = max(0.3, radius * size_mult * 0.75) | \n|  | ldata.energy = max(5.0, energy_mult * (radius ** 2)) | \n|  | ldata.color = color | \n|  |  | \n|  | lobj = bpy.data.objects.new(name=name, object_data=ldata) | \n|  | col.objects.link(lobj) | \n|  |  | \n|  | direction = _dir_from_az_el(az_deg, el_deg) | \n|  | loc = center + direction * (radius * dist_mult) | \n|  | _, _, rot = _camera_basis(-direction) | \n|  | mat = rot.to_4x4() | \n|  | mat.translation = loc | \n|  | lobj.matrix_world = mat | \n|  | return lobj | \n|  |  | \n|  |  | \n|  | def _create_ground_plane(col, center, radius, ground_z, bg_tone): | \n|  | preset = _BG_PRESETS.get(bg_tone, _BG_PRESETS[\"mid\"]) | \n|  | half = max(radius * 5.0, 2.5) | \n|  | mesh = bpy.data.meshes.new(name=f\"{_TEMP_PREFIX}GroundMesh\") | \n|  | verts = [(-half, -half, 0.0), (half, -half, 0.0), (half, half, 0.0), (-half, half, 0.0)] | \n|  | mesh.from_pydata(verts, [], [(0, 1, 2, 3)]) | \n|  | mesh.update() | \n|  |  | \n|  | mat = bpy.data.materials.new(name=f\"{_TEMP_PREFIX}GroundMat\") | \n|  | nt = _ensure_node_tree(mat) | \n|  | nt.nodes.clear() | \n|  | bsdf = nt.nodes.new(\"ShaderNodeBsdfPrincipled\") | \n|  | _set_input(bsdf, (\"Base Color\",), preset[\"floor\"]) | \n|  | _set_input(bsdf, (\"Roughness\",), 0.88) | \n|  | _set_input(bsdf, (\"Specular IOR Level\", \"Specular\"), 0.15) | \n|  | out = nt.nodes.new(\"ShaderNodeOutputMaterial\") | \n|  | nt.links.new(bsdf.outputs[\"BSDF\"], out.inputs[\"Surface\"]) | \n|  | mesh.materials.append(mat) | \n|  |  | \n|  | obj = bpy.data.objects.new(name=f\"{_TEMP_PREFIX}Ground\", object_data=mesh) | \n|  | obj.location = (center.x, center.y, ground_z) | \n|  | col.objects.link(obj) | \n|  | return obj, mat | \n|  |  | \n|  |  | \n|  | def cleanup_studio(): | \n|  | \"\"\"Remove any leftover _VFL_Studio collection and temporary VFL datablocks.\"\"\" | \n|  | col = bpy.data.collections.get(STUDIO_COL_NAME) | \n|  | if col is not None: | \n|  | for obj in list(col.objects): | \n|  | bpy.data.objects.remove(obj, do_unlink=True) | \n|  | bpy.data.collections.remove(col) | \n|  |  | \n|  | for container in ( | \n|  | bpy.data.objects, | \n|  | bpy.data.cameras, | \n|  | bpy.data.lights, | \n|  | bpy.data.meshes, | \n|  | bpy.data.materials, | \n|  | bpy.data.worlds, | \n|  | bpy.data.images, | \n|  | ): | \n|  | for item in list(container): | \n|  | if item.name.startswith(_TEMP_PREFIX): | \n|  | container.remove(item) | \n|  |  | \n|  |  | \n|  | def _set_raw_colorspace(img): | \n|  | for cs in (\"Non-Color\", \"Raw\"): | \n|  | try: | \n|  | img.colorspace_settings.name = cs | \n|  | return | \n|  | except Exception: | \n|  | continue | \n|  |  | \n|  |  | \n|  | def _load_tile_array(filepath): | \n|  | img = bpy.data.images.load(filepath, check_existing=False) | \n|  | try: | \n|  | _set_raw_colorspace(img) | \n|  | w, h = int(img.size[0]), int(img.size[1]) | \n|  | buf = np.empty(w * h * 4, dtype=np.float32) | \n|  | img.pixels.foreach_get(buf) | \n|  | return buf.reshape((h, w, 4)) | \n|  | finally: | \n|  | bpy.data.images.remove(img) | \n|  |  | \n|  |  | \n|  | def _draw_label(arr, text, x0, y_top, scale=2): | \n|  | \"\"\"Draw a high-contrast bitmap label in bottom-up RGBA float array arr (H, W, 4).\"\"\" | \n|  | text = text.upper() | \n|  | h, w, _ = arr.shape | \n|  | char_w = 6 * scale | \n|  | char_h = 7 * scale | \n|  | pad_x = 6 | \n|  | pad_y = 5 | \n|  | box_w = len(text) * char_w - scale + pad_x * 2 | \n|  | box_h = char_h + pad_y * 2 | \n|  |  | \n|  | bx0 = max(0, x0) | \n|  | bx1 = min(w, x0 + box_w) | \n|  | by1 = min(h, y_top + 1) | \n|  | by0 = max(0, by1 - box_h) | \n|  |  | \n|  | if bx1 > bx0 and by1 > by0: | \n|  | arr[by0:by1, bx0:bx1, :3] = arr[by0:by1, bx0:bx1, :3] * 0.22 + 0.04 | \n|  | arr[by0:by1, bx0:bx1, 3] = 1.0 | \n|  |  | \n|  | cursor_x = x0 + pad_x | \n|  | text_top = by1 - 1 - pad_y | \n|  | for ch in text: | \n|  | glyph = _FONT_5X7.get(ch, _FONT_5X7[\" \"]) | \n|  | for gy, row_bits in enumerate(glyph): | \n|  | for gx in range(5): | \n|  | if row_bits & (1 << (4 - gx)): | \n|  | px0 = cursor_x + gx * scale | \n|  | px1 = min(w, px0 + scale) | \n|  | py_hi = text_top - gy * scale + 1 | \n|  | py_lo = max(0, py_hi - scale) | \n|  | if 0 <= px0 < w and py_hi > py_lo: | \n|  | arr[py_lo:py_hi, px0:px1, :3] = (0.96, 0.97, 0.99) | \n|  | arr[py_lo:py_hi, px0:px1, 3] = 1.0 | \n|  | cursor_x += char_w | \n|  |  | \n|  |  | \n|  | def _save_rgba_array(arr, output_path): | \n|  | h, w, _ = arr.shape | \n|  | out_dir = os.path.dirname(os.path.abspath(output_path)) | \n|  | if out_dir: | \n|  | os.makedirs(out_dir, exist_ok=True) | \n|  | img = bpy.data.images.new(name=f\"{_TEMP_PREFIX}Sheet\", width=w, height=h, alpha=True) | \n|  | try: | \n|  | _set_raw_colorspace(img) | \n|  | img.pixels.foreach_set(np.ascontiguousarray(arr, dtype=np.float32).ravel()) | \n|  | img.filepath_raw = output_path | \n|  | img.file_format = \"PNG\" | \n|  | img.save() | \n|  | finally: | \n|  | bpy.data.images.remove(img) | \n|  |  | \n|  |  | \n|  | def _swap_materials(mesh_objects, override_mat): | \n|  | \"\"\"Temporarily replace materials on mesh_objects (safe for shared/instanced meshes).\"\"\" | \n|  | # Pass 1: read-only snapshot before any mutation | \n|  | orig_mesh_mats = {} | \n|  | orig_obj_slots = [] | \n|  | for obj in mesh_objects: | \n|  | mesh = obj.data | \n|  | if mesh not in orig_mesh_mats: | \n|  | orig_mesh_mats[mesh] = list(mesh.materials) | \n|  | orig_obj_slots.append((obj, [(s.link, s.material) for s in obj.material_slots])) | \n|  |  | \n|  | # Pass 2: apply override | \n|  | for mesh in orig_mesh_mats: | \n|  | if len(mesh.materials) == 0: | \n|  | mesh.materials.append(override_mat) | \n|  | else: | \n|  | for i in range(len(mesh.materials)): | \n|  | mesh.materials[i] = override_mat | \n|  | for obj, _ in orig_obj_slots: | \n|  | for slot in obj.material_slots: | \n|  | if slot.link == \"OBJECT\": | \n|  | slot.material = override_mat | \n|  |  | \n|  | return orig_mesh_mats, orig_obj_slots | \n|  |  | \n|  |  | \n|  | def _restore_materials(saved): | \n|  | orig_mesh_mats, orig_obj_slots = saved | \n|  | for mesh, mats in orig_mesh_mats.items(): | \n|  | mesh.materials.clear() | \n|  | for m in mats: | \n|  | mesh.materials.append(m) | \n|  | for obj, slots in orig_obj_slots: | \n|  | for idx, (link, mat) in enumerate(slots): | \n|  | if idx < len(obj.material_slots): | \n|  | obj.material_slots[idx].link = link | \n|  | if link == \"OBJECT\": | \n|  | obj.material_slots[idx].material = mat | \n|  |  | \n|  |  | \n|  | def _capture_views( | \n|  | output_path, | \n|  | mesh_objects, | \n|  | center, | \n|  | corners, | \n|  | radius, | \n|  | views, | \n|  | mode=\"clay\", | \n|  | tile_size=(640, 480), | \n|  | grid_cols=3, | \n|  | padding=1.22, | \n|  | bg_tone=\"mid\", | \n|  | samples=16, | \n|  | ground_plane=True, | \n|  | ground_z=0.0, | \n|  | use_scene_lights=False, | \n|  | draw_labels=True, | \n|  | studio_center=None, | \n|  | studio_radius=None, | \n|  | ): | \n|  | scene = bpy.context.scene | \n|  | render = scene.render | \n|  | cleanup_studio() | \n|  |  | \n|  | light_center = studio_center if studio_center is not None else center | \n|  | light_radius = max(float(studio_radius), radius) if studio_radius is not None else radius | \n|  |  | \n|  | # Snapshot mutable scene state | \n|  | orig = { | \n|  | \"engine\": render.engine, | \n|  | \"res_x\": render.resolution_x, | \n|  | \"res_y\": render.resolution_y, | \n|  | \"res_pct\": render.resolution_percentage, | \n|  | \"pix_x\": render.pixel_aspect_x, | \n|  | \"pix_y\": render.pixel_aspect_y, | \n|  | \"border\": render.use_border, | \n|  | \"crop\": render.use_crop_to_border, | \n|  | \"filepath\": render.filepath, | \n|  | \"file_format\": render.image_settings.file_format, | \n|  | \"color_mode\": render.image_settings.color_mode, | \n|  | \"color_depth\": render.image_settings.color_depth, | \n|  | \"film_transparent\": render.film_transparent, | \n|  | \"camera\": scene.camera, | \n|  | \"world\": scene.world, | \n|  | \"exposure\": scene.view_settings.exposure, | \n|  | \"gamma\": scene.view_settings.gamma, | \n|  | \"eevee_samples\": getattr(scene.eevee, \"taa_render_samples\", None), | \n|  | \"cycles_samples\": getattr(scene.cycles, \"samples\", None), | \n|  | } | \n|  |  | \n|  | hidden_lights = [] | \n|  | saved_materials = [] | \n|  | tmp_dir = tempfile.mkdtemp(prefix=\"vfl_cap_\") | \n|  |  | \n|  | try: | \n|  | studio_col = bpy.data.collections.new(STUDIO_COL_NAME) | \n|  | scene.collection.children.link(studio_col) | \n|  |  | \n|  | # Configure render settings | \n|  | render.engine = _pick_eevee_engine() | \n|  | render.resolution_x = int(tile_size[0]) | \n|  | render.resolution_y = int(tile_size[1]) | \n|  | render.resolution_percentage = 100 | \n|  | render.pixel_aspect_x = 1.0 | \n|  | render.pixel_aspect_y = 1.0 | \n|  | render.use_border = False | \n|  | render.use_crop_to_border = False | \n|  | render.film_transparent = False | \n|  | render.image_settings.file_format = \"PNG\" | \n|  | render.image_settings.color_mode = \"RGBA\" | \n|  | render.image_settings.color_depth = \"8\" | \n|  | scene.view_settings.exposure = 0.0 | \n|  | scene.view_settings.gamma = 1.0 | \n|  |  | \n|  | if hasattr(scene.eevee, \"taa_render_samples\"): | \n|  | scene.eevee.taa_render_samples = int(samples) | \n|  | if hasattr(scene.cycles, \"samples\"): | \n|  | scene.cycles.samples = int(samples) | \n|  |  | \n|  | # Studio world & 3-point lighting (unless shaded + use_scene_lights=True) | \n|  | if not (mode == \"shaded\" and use_scene_lights): | \n|  | for obj in scene.objects: | \n|  | if obj.type == \"LIGHT\" and not obj.hide_render: | \n|  | obj.hide_render = True | \n|  | hidden_lights.append(obj) | \n|  | scene.world = _build_studio_world(bg_tone) | \n|  | _create_area_light(studio_col, f\"{_TEMP_PREFIX}Key\",  light_center, light_radius,   42.0, 40.0, 2.6, 1.4, 42.0, (1.00, 0.97, 0.92)) | \n|  | _create_area_light(studio_col, f\"{_TEMP_PREFIX}Fill\", light_center, light_radius,  -55.0, 22.0, 2.8, 2.2, 18.0, (0.86, 0.92, 1.00)) | \n|  | _create_area_light(studio_col, f\"{_TEMP_PREFIX}Rim\",  light_center, light_radius,  155.0, 38.0, 2.6, 1.6, 28.0, (0.98, 0.99, 1.00)) | \n|  |  | \n|  | # Ground plane at ground_z (exposes floating or sunk geometry immediately) | \n|  | if ground_plane: | \n|  | _create_ground_plane(studio_col, light_center, light_radius, ground_z, bg_tone) | \n|  |  | \n|  | # Diagnostic material override (skip for shaded) | \n|  | if mode in (\"clay\", \"wireframe\", \"normals\"): | \n|  | diag_mat = _build_diagnostic_material(mode, light_radius) | \n|  | saved_materials = _swap_materials(mesh_objects, diag_mat) | \n|  |  | \n|  | # Studio camera | \n|  | cam_data = bpy.data.cameras.new(name=f\"{_TEMP_PREFIX}Cam\") | \n|  | cam_obj = bpy.data.objects.new(name=f\"{_TEMP_PREFIX}Cam\", object_data=cam_data) | \n|  | studio_col.objects.link(cam_obj) | \n|  | scene.camera = cam_obj | \n|  |  | \n|  | aspect = float(tile_size[0]) / float(tile_size[1]) | \n|  | rendered_tiles = [] | \n|  |  | \n|  | for idx, vcfg in enumerate(views): | \n|  | _position_camera(cam_obj, center, corners, radius, vcfg, aspect, padding=padding, clip_radius=light_radius) | \n|  | tile_path = os.path.join(tmp_dir, f\"tile_{idx:02d}.png\") | \n|  | render.filepath = tile_path | \n|  | bpy.ops.render.render(write_still=True) | \n|  | rendered_tiles.append((vcfg.get(\"label\", f\"VIEW {idx+1}\"), _load_tile_array(tile_path))) | \n|  |  | \n|  | # Stitch tiles into contact sheet (or single frame) | \n|  | if len(rendered_tiles) == 1: | \n|  | label, arr = rendered_tiles[0] | \n|  | if draw_labels and label: | \n|  | _draw_label(arr, f\"{label} [{mode}]\", 10, arr.shape[0] - 11, scale=2) | \n|  | _save_rgba_array(arr, output_path) | \n|  | else: | \n|  | th, tw, _ = rendered_tiles[0][1].shape | \n|  | cols = min(int(grid_cols), len(rendered_tiles)) | \n|  | rows = math.ceil(len(rendered_tiles) / cols) | \n|  | gap = 4 | \n|  | out_w = cols * tw + (cols + 1) * gap | \n|  | out_h = rows * th + (rows + 1) * gap | \n|  |  | \n|  | canvas = np.zeros((out_h, out_w, 4), dtype=np.float32) | \n|  | canvas[:, :, :3] = (0.08, 0.09, 0.11) | \n|  | canvas[:, :, 3] = 1.0 | \n|  |  | \n|  | for idx, (label, tile_arr) in enumerate(rendered_tiles): | \n|  | r = idx // cols | \n|  | c = idx % cols | \n|  | # Bottom-up row placement: row 0 is at the top of the image | \n|  | x0 = gap + c * (tw + gap) | \n|  | y0 = gap + (rows - 1 - r) * (th + gap) | \n|  | canvas[y0:y0 + th, x0:x0 + tw, :] = tile_arr | \n|  | if draw_labels and label: | \n|  | badge = f\"{label} [{mode}]\" if idx == 0 else label | \n|  | _draw_label(canvas, badge, x0 + 8, y0 + th - 9, scale=2) | \n|  |  | \n|  | _save_rgba_array(canvas, output_path) | \n|  |  | \n|  | return os.path.abspath(output_path) | \n|  |  | \n|  | finally: | \n|  | if saved_materials: | \n|  | _restore_materials(saved_materials) | \n|  | for lobj in hidden_lights: | \n|  | lobj.hide_render = False | \n|  |  | \n|  | render.engine = orig[\"engine\"] | \n|  | render.resolution_x = orig[\"res_x\"] | \n|  | render.resolution_y = orig[\"res_y\"] | \n|  | render.resolution_percentage = orig[\"res_pct\"] | \n|  | render.pixel_aspect_x = orig[\"pix_x\"] | \n|  | render.pixel_aspect_y = orig[\"pix_y\"] | \n|  | render.use_border = orig[\"border\"] | \n|  | render.use_crop_to_border = orig[\"crop\"] | \n|  | render.filepath = orig[\"filepath\"] | \n|  | render.image_settings.file_format = orig[\"file_format\"] | \n|  | render.image_settings.color_mode = orig[\"color_mode\"] | \n|  | render.image_settings.color_depth = orig[\"color_depth\"] | \n|  | render.film_transparent = orig[\"film_transparent\"] | \n|  | scene.camera = orig[\"camera\"] | \n|  | scene.world = orig[\"world\"] | \n|  | scene.view_settings.exposure = orig[\"exposure\"] | \n|  | scene.view_settings.gamma = orig[\"gamma\"] | \n|  | if orig[\"eevee_samples\"] is not None and hasattr(scene.eevee, \"taa_render_samples\"): | \n|  | scene.eevee.taa_render_samples = orig[\"eevee_samples\"] | \n|  | if orig[\"cycles_samples\"] is not None and hasattr(scene.cycles, \"samples\"): | \n|  | scene.cycles.samples = orig[\"cycles_samples\"] | \n|  |  | \n|  | cleanup_studio() | \n|  | shutil.rmtree(tmp_dir, ignore_errors=True) | \n|  |  | \n|  |  | \n|  | def capture_contact_sheet( | \n|  | output_path, | \n|  | target_collection=None, | \n|  | target_objects=None, | \n|  | mode=\"clay\", | \n|  | tile_size=(640, 480), | \n|  | grid_cols=3, | \n|  | padding=1.22, | \n|  | bg_tone=\"mid\", | \n|  | samples=16, | \n|  | ground_plane=True, | \n|  | ground_z=0.0, | \n|  | use_scene_lights=False, | \n|  | views=None, | \n|  | ): | \n|  | \"\"\"Render a labeled 6-view inspection contact sheet and return its absolute path.\"\"\" | \n|  | if mode not in (\"clay\", \"shaded\", \"wireframe\", \"normals\"): | \n|  | raise ValueError(f\"Unsupported mode {mode!r}; use 'clay', 'shaded', 'wireframe', or 'normals'.\") | \n|  | mesh_objects = _resolve_target_meshes(target_collection=target_collection, target_objects=target_objects) | \n|  | _, _, center, _, radius, corners = _compute_world_bounds(mesh_objects) | \n|  | return _capture_views( | \n|  | output_path=output_path, | \n|  | mesh_objects=mesh_objects, | \n|  | center=center, | \n|  | corners=corners, | \n|  | radius=radius, | \n|  | views=views or DEFAULT_VIEWS, | \n|  | mode=mode, | \n|  | tile_size=tile_size, | \n|  | grid_cols=grid_cols, | \n|  | padding=padding, | \n|  | bg_tone=bg_tone, | \n|  | samples=samples, | \n|  | ground_plane=ground_plane, | \n|  | ground_z=ground_z, | \n|  | use_scene_lights=use_scene_lights, | \n|  | ) | \n|  |  | \n|  |  | \n|  | def capture_closeup( | \n|  | output_path, | \n|  | target, | \n|  | target_collection=None, | \n|  | mode=\"shaded\", | \n|  | azimuth_deg=35.0, | \n|  | elevation_deg=18.0, | \n|  | lens=65.0, | \n|  | size=(1024, 768), | \n|  | padding=1.25, | \n|  | bg_tone=\"mid\", | \n|  | samples=24, | \n|  | ground_plane=True, | \n|  | ground_z=0.0, | \n|  | use_scene_lights=False, | \n|  | label=None, | \n|  | ): | \n|  | \"\"\" | \n|  | Render a high-res close-up framed on: | \n|  | - target = object name (str), | \n|  | - target = list of object names / bpy.types.Object, or | \n|  | - target = ((x, y, z), radius) world coordinate tuple. | \n|  | \"\"\" | \n|  | if mode not in (\"clay\", \"shaded\", \"wireframe\", \"normals\"): | \n|  | raise ValueError(f\"Unsupported mode {mode!r}; use 'clay', 'shaded', 'wireframe', or 'normals'.\") | \n|  | all_meshes = _resolve_target_meshes(target_collection=target_collection) | \n|  | _, _, studio_center, _, studio_radius, _ = _compute_world_bounds(all_meshes) | \n|  | if isinstance(target, tuple) and len(target) == 2 and isinstance(target[0], (tuple, list, Vector)): | \n|  | center = Vector(target[0]) | \n|  | radius = max(float(target[1]), 0.03) | \n|  | corners = [ | \n|  | center + Vector((dx, dy, dz)) | \n|  | for dx in (-radius, radius) | \n|  | for dy in (-radius, radius) | \n|  | for dz in (-radius, radius) | \n|  | ] | \n|  | view_label = label or \"CLOSEUP\" | \n|  | else: | \n|  | focus_list = [target] if isinstance(target, (str, bpy.types.Object)) else list(target) | \n|  | focus_meshes = _resolve_target_meshes(target_objects=focus_list) | \n|  | _, _, center, _, radius, corners = _compute_world_bounds(focus_meshes) | \n|  | view_label = label or (focus_meshes[0].name if len(focus_meshes) == 1 else \"CLOSEUP\") | \n|  |  | \n|  | view = {\"label\": view_label, \"type\": \"PERSP\", \"az\": float(azimuth_deg), \"el\": float(elevation_deg), \"lens\": float(lens)} | \n|  | return _capture_views( | \n|  | output_path=output_path, | \n|  | mesh_objects=all_meshes, | \n|  | center=center, | \n|  | corners=corners, | \n|  | radius=radius, | \n|  | views=[view], | \n|  | mode=mode, | \n|  | tile_size=size, | \n|  | grid_cols=1, | \n|  | padding=padding, | \n|  | bg_tone=bg_tone, | \n|  | samples=samples, | \n|  | ground_plane=ground_plane, | \n|  | ground_z=ground_z, | \n|  | use_scene_lights=use_scene_lights, | \n|  | studio_center=studio_center, | \n|  | studio_radius=studio_radius, | \n|  | ) |", "url": "https://wpnews.pro/news/agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01", "canonical_source": "https://gist.github.com/Esdeaz/4813b877dff22dda12b812409efaa446", "published_at": "2026-12-03 16:49:02+00:00", "updated_at": "2026-12-05 13:13:44.742456+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "generative-ai"], "entities": ["Blender", "Blender MCP", "bpy", "bmesh", "numpy", "mathutils"], "also_reported_by": [], "takeaway": "The VFL script renders a fixed six-view contact sheet (3/4 front and rear perspective plus top, front, right, and back orthographic) from a named collection, giving agents a deterministic multi-angle image set to inspect instead of ad-hoc screenshots.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01", "markdown": "https://wpnews.pro/news/agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01.md", "text": "https://wpnews.pro/news/agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01.txt", "jsonld": "https://wpnews.pro/news/agentic-blender-bpy-bmesh-visual-feedback-loop-workflow-prompt-hadley-st-01.jsonld"}}