Skip to content

Single place to convert your 2D floor plans to 3d model (no LLM)

Python
2.0 KB
Raw Blame
"""Pictures of a theme's pieces for the Themes page: blender -b OUT/house.blend -P blender/thumbs.py -- OUT_DIR
(OUT/house.blend = gen.py run on catalog.preview_plan(theme), saved as OUT/plan.json; one PNG per piece, named after
its slot)."""
import bpy, json, os, sys
from mathutils import Vector

out, F = sys.argv[sys.argv.index("--") + 1], 0.3048
plan = json.load(open(os.path.join(out, "plan.json")))
sc = bpy.context.scene
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import bake  # noqa: E402
bake._gpu(sc)          # Cycles: real materials, and unlike EEVEE it runs on a server with no display or GPU
sc.cycles.samples, sc.cycles.use_denoising = 32, True
for o in sc.objects:                # roof off: the sky lights the preview room evenly, as in a photo studio
    if o.name.startswith(("roof", "parapet")):
        o.hide_render = True
sc.render.resolution_x, sc.render.resolution_y = 480, 360
cam = bpy.data.objects.new("thumb", bpy.data.cameras.new("thumb"))
sc.collection.objects.link(cam)
cam.data.lens = 26
sc.camera = cam
for it in plan["floors"][0]["items"]:
    x0, x1, y0, y1 = it["box"]
    z = it["kw"].get("z", 0.0)
    wall, small = it["fn"] in ("art", "mirror"), max(x1 - x0, y1 - y0) < 1.5
    c = Vector(((x0 + x1) / 2, (y0 + y1) / 2, z + (1.0 if it["fn"] == "light" else 0.8 if wall else 0.7 if small else 2.2))) * F
    back = max(x1 - x0, y1 - y0) * 0.7 + (2.5 if small else 3.0 if wall or it["fn"] == "light" else 4.5)     # feet: fits the piece
    room = (y0 + y1) / 2 - 1.5           # the camera stays inside the preview room: a wider lens for big pieces
    cam.data.lens = 26 * min(1.0, room / back)
    back = min(back, room)
    cam.location = c + Vector((back * 0.55, -back, 3.2 if not (wall or small) and it["fn"] != "light" else 0.4)) * F
    cam.rotation_euler = (c - cam.location).to_track_quat("-Z", "Y").to_euler()
    sc.render.filepath = os.path.join(out, it["fn"] + ".png")
    bpy.ops.render.render(write_still=True)
print("THUMBS OK", flush=True)