Initial commit: cad-building-showcase skill
Agent-led CAD-drawing → 3D web showcase pipeline. Inspects CAD reports, maps layers/floors, extracts a normalized building spec (with block-probed glazing and audit gates), builds Blender/trimesh GLB variants, and assembles a Three.js web viewer. Includes XREF-shell detection and a regression test. Large inputs/outputs (work/, outputs/, source_cad/, renders) are gitignored. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
179
scripts/render_ifc_meshes_blender.py
Executable file
179
scripts/render_ifc_meshes_blender.py
Executable file
@@ -0,0 +1,179 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render IFC mesh JSON with Blender and export a GLB/PNG preview."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import math
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
COLORS = {
|
||||
"IfcSlab": (0.72, 0.74, 0.70, 0.34),
|
||||
"IfcWall": (0.78, 0.76, 0.68, 1.0),
|
||||
"IfcColumn": (1.00, 0.46, 0.10, 1.0),
|
||||
"IfcBeam": (0.05, 0.28, 0.82, 1.0),
|
||||
}
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
argv = sys.argv
|
||||
if "--" in argv:
|
||||
argv = argv[argv.index("--") + 1 :]
|
||||
else:
|
||||
argv = []
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--meshes", required=True, help="Mesh JSON from extract_ifc_meshes.py")
|
||||
parser.add_argument("--out-dir", required=True)
|
||||
parser.add_argument("--name", default="ifc_render")
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def clear_scene(bpy):
|
||||
bpy.ops.object.select_all(action="SELECT")
|
||||
bpy.ops.object.delete()
|
||||
for datablocks in (bpy.data.meshes, bpy.data.materials, bpy.data.lights, bpy.data.cameras, bpy.data.curves):
|
||||
for item in list(datablocks):
|
||||
if item.users == 0:
|
||||
datablocks.remove(item)
|
||||
|
||||
|
||||
def make_material(bpy, name: str, color: tuple[float, float, float, float]):
|
||||
mat = bpy.data.materials.new(name)
|
||||
mat.diffuse_color = color
|
||||
mat.use_nodes = True
|
||||
bsdf = mat.node_tree.nodes.get("Principled BSDF")
|
||||
if bsdf:
|
||||
bsdf.inputs["Base Color"].default_value = color
|
||||
bsdf.inputs["Roughness"].default_value = 0.62
|
||||
if "Alpha" in bsdf.inputs:
|
||||
bsdf.inputs["Alpha"].default_value = color[3]
|
||||
if color[3] < 1.0:
|
||||
mat.blend_method = "BLEND"
|
||||
mat.show_transparent_back = True
|
||||
return mat
|
||||
|
||||
|
||||
def create_mesh_object(bpy, group: dict, material):
|
||||
mesh = bpy.data.meshes.new(group["ifc_type"] + "Mesh")
|
||||
mesh.from_pydata(group["vertices"], [], group["faces"])
|
||||
mesh.update()
|
||||
obj = bpy.data.objects.new(group["ifc_type"], mesh)
|
||||
bpy.context.collection.objects.link(obj)
|
||||
obj.data.materials.append(material)
|
||||
obj["IFC_TYPE"] = group["ifc_type"]
|
||||
obj["IFC_ELEMENT_COUNT"] = group["elements"]
|
||||
return obj
|
||||
|
||||
|
||||
def scene_bounds(objects, Vector):
|
||||
mins = [float("inf"), float("inf"), float("inf")]
|
||||
maxs = [float("-inf"), float("-inf"), float("-inf")]
|
||||
for obj in objects:
|
||||
for corner in obj.bound_box:
|
||||
point = obj.matrix_world @ Vector(corner)
|
||||
for index in range(3):
|
||||
mins[index] = min(mins[index], point[index])
|
||||
maxs[index] = max(maxs[index], point[index])
|
||||
if not objects:
|
||||
return [0, 0, 0], [1, 1, 1]
|
||||
return mins, maxs
|
||||
|
||||
|
||||
def add_label(bpy, text: str, location: tuple[float, float, float], size: float, material):
|
||||
curve = bpy.data.curves.new(text + "Curve", "FONT")
|
||||
curve.body = text
|
||||
curve.size = size
|
||||
curve.align_x = "LEFT"
|
||||
curve.align_y = "CENTER"
|
||||
obj = bpy.data.objects.new(text, curve)
|
||||
obj.location = location
|
||||
bpy.context.collection.objects.link(obj)
|
||||
obj.data.materials.append(material)
|
||||
|
||||
|
||||
def setup_render(bpy, objects: list):
|
||||
from mathutils import Vector
|
||||
|
||||
mins, maxs = scene_bounds(objects, Vector)
|
||||
center = [(mins[i] + maxs[i]) / 2.0 for i in range(3)]
|
||||
size = [max(maxs[i] - mins[i], 1.0) for i in range(3)]
|
||||
|
||||
bpy.context.scene.unit_settings.system = "METRIC"
|
||||
try:
|
||||
bpy.context.scene.render.engine = "BLENDER_EEVEE"
|
||||
except TypeError:
|
||||
pass
|
||||
bpy.context.scene.view_settings.view_transform = "Standard"
|
||||
bpy.context.scene.view_settings.look = "Medium High Contrast"
|
||||
if bpy.context.scene.world:
|
||||
bpy.context.scene.world.color = (0.78, 0.82, 0.86)
|
||||
bpy.context.scene.render.resolution_x = 1800
|
||||
bpy.context.scene.render.resolution_y = 1200
|
||||
|
||||
bpy.ops.object.light_add(type="SUN", location=(0, 0, maxs[2] + 40))
|
||||
sun = bpy.context.object
|
||||
sun.name = "Sun"
|
||||
sun.data.energy = 2.4
|
||||
sun.rotation_euler = (math.radians(48), 0, math.radians(35))
|
||||
|
||||
bpy.ops.object.light_add(type="AREA", location=(center[0] - 30, center[1] - 40, maxs[2] + 45))
|
||||
area = bpy.context.object
|
||||
area.name = "Softbox"
|
||||
area.data.energy = 700
|
||||
area.data.size = max(size[0], size[1]) * 0.55
|
||||
|
||||
distance = max(size) * 0.95
|
||||
bpy.ops.object.camera_add(location=(center[0] - distance, center[1] - distance * 0.95, maxs[2] + distance * 0.70))
|
||||
camera = bpy.context.object
|
||||
direction = Vector((center[0], center[1], center[2])) - camera.location
|
||||
camera.rotation_euler = direction.to_track_quat("-Z", "Y").to_euler()
|
||||
camera.data.type = "ORTHO"
|
||||
camera.data.ortho_scale = max(size[0], size[1]) * 1.10
|
||||
bpy.context.scene.camera = camera
|
||||
|
||||
text_mat = make_material(bpy, "label black", (0.02, 0.02, 0.018, 1.0))
|
||||
add_label(
|
||||
bpy,
|
||||
"IFC render: IfcWall / IfcColumn / IfcBeam / IfcSlab",
|
||||
(mins[0], maxs[1] + 3.0, maxs[2] + 0.4),
|
||||
2.2,
|
||||
text_mat,
|
||||
)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
args = parse_args()
|
||||
try:
|
||||
import bpy # type: ignore
|
||||
except ImportError as exc:
|
||||
raise SystemExit("Run this script with Blender: blender -b --python render_ifc_meshes_blender.py -- --meshes ...") from exc
|
||||
payload = json.loads(Path(args.meshes).read_text(encoding="utf-8"))
|
||||
out_dir = Path(args.out_dir)
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
clear_scene(bpy)
|
||||
materials = {
|
||||
ifc_type: make_material(bpy, ifc_type + " material", COLORS.get(ifc_type, (0.8, 0.8, 0.8, 1.0)))
|
||||
for ifc_type in [group["ifc_type"] for group in payload["groups"]]
|
||||
}
|
||||
objects = [
|
||||
create_mesh_object(bpy, group, materials[group["ifc_type"]])
|
||||
for group in payload["groups"]
|
||||
if group["vertices"] and group["faces"]
|
||||
]
|
||||
setup_render(bpy, objects)
|
||||
blend_path = out_dir / f"{args.name}.blend"
|
||||
glb_path = out_dir / f"{args.name}.glb"
|
||||
png_path = out_dir / f"{args.name}.png"
|
||||
bpy.ops.wm.save_as_mainfile(filepath=str(blend_path))
|
||||
bpy.ops.export_scene.gltf(filepath=str(glb_path), export_format="GLB")
|
||||
bpy.context.scene.render.filepath = str(png_path)
|
||||
bpy.ops.render.render(write_still=True)
|
||||
print(json.dumps({"blend": str(blend_path), "glb": str(glb_path), "png": str(png_path), "mesh_objects": [obj.name for obj in objects]}, ensure_ascii=False))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user