Single place to convert your 2D floor plans to 3d model (no LLM)
Python
Move all furniture into blender/furniture.py
23f2005639e2f95e0fbf21
diff --git a/AGENTS.md b/AGENTS.mdindex 329a36a..4ebea6d 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -24,8 +24,9 @@ Needs Python 3, Node 18+, Blender 4.2+ and poppler. If Blender is not on `PATH`,| `quantities.py` | plan JSON -> areas, paint, tiles, doors, windows, furniture counts. Also holds `FLOOR_MAT` and `opening_heights`, which `gen.py` imports, so the sheet counts what is built || `themes.py` | interior themes: `LIBRARY` (Poly Haven models with their measured sizes and turn), `STYLES` (pieces gen.py draws), `THEMES` (built-in), `check`/`load` for people's own (`assets/themes/<id>.json`, uploads in `assets/themes/<id>/<slot>.glb`) || `catalog.py` | built-in pieces' default footprints, and `preview_plan(theme)`: the row of a theme's pieces that `blender/thumbs.py` photographs |-| `blender/gen.py` | plan JSON -> house (run inside Blender). `CALLS` maps item `fn` to the function that builds it |-| `blender/lib.py` | materials, box meshes, furniture parts. Feet in, metres out (`F = 0.3048`) |+| `blender/gen.py` | plan JSON -> house (run inside Blender): walls, openings, stairs, roof, site, lights, then bake |+| `blender/furniture.py` | every furniture piece and fixture, the parts they are made of, theme models fitted to a box. `CALLS` maps item `fn` to its builder; `build(item)` builds one item in the theme's piece |+| `blender/lib.py` | materials and box meshes. Feet in, metres out (`F = 0.3048`); `LEVEL` and `WALL_H` are the storey being built || `blender/bake.py` | light maps (Cycles) and `house.glb` export || `web/` | `index.html` (upload), `edit.html` + `edit.js` (review), `view.html` + `view.js` (walkthrough), `walk.js` (collision, stairs, start point), `themes.html` || `examples/` | `sample_plan.pdf`, its generator `make_sample.py`, `embed.html` |@@ -58,7 +59,7 @@ If you change `examples/make_sample.py`, run it to rewrite the PDF and keep its## How to change things-- **A new furniture item:** write the builder in `blender/gen.py` (or `lib.py`), add it to `CALLS` and to `ITEM_FNS` in `server.py` (what the review page may send), place it from a rule in `furnish.py` with its footprint `box`. Add it to `themes.SLOTS` (and its size to `catalog.py`) if themes should be able to change it.+- **A new furniture item:** write the builder in `blender/furniture.py`, add it to `CALLS` and to `ITEM_FNS` in `server.py` (what the review page may send), place it from a rule in `furnish.py` with its footprint `box`. Add it to `themes.SLOTS` (and its size to `catalog.py`) if themes should be able to change it.- **A model for the themes:** measure it as Blender imports it (feet, front facing -y after `turn`), add it to `themes.LIBRARY` with the slots it may fill; `fetch_assets.py` downloads every library model. Large ones: keep it under about 30k triangles.- **A new theme:** an entry in `themes.THEMES`. Furniture rules read only sizes from it (`Room.sizes`), so a model that does not fit falls back to the built-in piece in the theme's colours. Check it with `tests/test_themes.py` and look at a build: rules can be right and a room still look wrong.- **A new built-in piece style:** a `style=` branch in its builder, listed in `themes.STYLES`. New materials go in `lib.py`; a coloured one goes in `PAINT` in `gen.py` so themes repaint it.@@ -70,7 +71,7 @@ If you change `examples/make_sample.py`, run it to rewrite the PDF and keep its## Traps- Blender is often installed as a snap: it cannot read `/tmp`. Give it paths inside the project.-- `gen.py` does `from lib import *`: do not name a global in `gen.py` after one in `lib.py`.+- `blender/furniture.py` reads the storey from `lib.LEVEL` / `lib.WALL_H`: never `from lib import LEVEL` (a copy that does not follow the storey).- Everything in a saved plan comes from a browser. Validate it before it reaches a file path or `innerHTML`.- `jobs/`, `assets/` and `node_modules/` are not in git. Do not commit real client drawings or screenshots of them: use the sample plan.- The walkthrough's lighting is baked. Hiding or moving objects in the browser leaves their shadows behind; change the build instead.diff --git a/README.md b/README.mdindex 89c568c..63968a2 100644--- a/README.md+++ b/README.md@@ -208,7 +208,7 @@ quantities.py areas, paint, tiles, doors, windows and furniture counts (purthemes.py interior themes: the model library, the built-in themes, checks for your owncatalog.py the built-in pieces' sizes and the preview row the Themes page photographsfetch_assets.py downloads the CC0 textures, models and sky-blender/ gen.py (build), bake.py (light maps), lib.py (parts), cinema.py (video renderer)+blender/ gen.py (build), furniture.py (pieces), bake.py (light maps), lib.py (materials, meshes), cinema.py (video renderer)web/ home, review and walkthrough pagesexamples/ sample_plan.pdf, its generator, embed.htmltests/ detector, furniture, walking and browser testsdiff --git a/blender/furniture.py b/blender/furniture.pynew file mode 100644index 0000000..8cc400e--- /dev/null+++ b/blender/furniture.py@@ -0,0 +1,992 @@+"""Furniture and fixtures for gen.py (run inside Blender; units: feet, front faces -y at rot 0): built-in pieces and+the parts they are made of, models placed or fitted to a box (theme pieces, uploads), and CALLS: item fn -> builder.+gen.py sets T (the interior theme) before building; LEVEL and WALL_H are read from lib, which gen.py sets per storey."""+import bmesh, bpy, math, os, random+from mathutils import Vector+import lib+from lib import ASSETS, F, MATS, collection, mesh_from_boxes, scene, subtract_all+import themes++T = None # the interior theme (themes.load), set by gen.py before it builds+FURN = [] # (name, footprint) of furniture, for the doorway clearance check+# ---------------------------------------------------------------- furniture+PROTO = {}+++def load_model(name):+ if name in PROTO:+ return PROTO[name]+ if not name.replace("_", "").isalnum():+ raise ValueError(f"bad model name {name!r}")+ path = os.path.join(ASSETS, "models", name, f"{name}_1k.gltf")+ before = set(bpy.data.objects)+ bpy.ops.import_scene.gltf(filepath=path)+ new = [o for o in bpy.data.objects if o not in before]+ for o in new:+ for c in list(o.users_collection):+ c.objects.unlink(o)+ for m in {sl.material for sl in o.material_slots if sl.material and sl.material.use_nodes} if o.type == "MESH" else ():+ glass_from_transmission(m)+ PROTO[name] = new+ return new+++def glass_from_transmission(m):+ """scanned glass uses transmission, which the web viewer cannot show, and the glTF exporter can drop its map and+ break the whole file: make it plain see-through glass instead"""+ p = m.node_tree.nodes.get("Principled BSDF")+ t = p and p.inputs.get("Transmission Weight")+ if t and (t.is_linked or t.default_value > 0.5):+ for link in list(t.links):+ m.node_tree.links.remove(link)+ t.default_value = 0.0+ p.inputs["Alpha"].default_value = 0.25+ if hasattr(m, "surface_render_method"):+ m.surface_render_method = "BLENDED"+++def place(name, x, y, rot=0, z=0.0, scale=1.0, collide=True):+ """x,y,z in feet; rot = degrees, 0 = model's default facing."""+ objs = load_model(name)+ root = bpy.data.objects.new(f"{name}_at_{x:.1f}_{y:.1f}", None)+ collection("furniture").objects.link(root)+ mapping = {}+ for o in objs:+ c = o.copy() # shares mesh data -> glTF instancing+ mapping[o] = c+ collection("furniture").objects.link(c)+ for o, c in mapping.items():+ c.parent = mapping.get(o.parent, root)+ root.location = (x * F, y * F, (z + lib.LEVEL) * F)+ root.rotation_euler = (0, 0, math.radians(rot))+ root.scale = (scale,) * 3+ if collide:+ bpy.context.view_layer.update()+ add_collider([c for c in mapping.values() if c.type == "MESH"])+ return root+++def add_collider(objs):+ pts = [o.matrix_world @ Vector(c) for o in objs for c in o.bound_box]+ if not pts:+ return+ # furniture does not block the walker; footprints are only used for the doorway clearance check+ FURN.append((objs[0].name, (min(p.x for p in pts) / F, max(p.x for p in pts) / F,+ min(p.y for p in pts) / F, max(p.y for p in pts) / F)))+++def group(name, boxes, x, y, rot=0, bevel=0.0, collide=True, z=0.0, seg=2):+ """procedural item: boxes in local feet, front faces -Y, origin at footprint centre."""+ ob = mesh_from_boxes(name, boxes, coll="furniture", bevel=bevel * F, seg=seg)+ ob.location = (x * F, y * F, (z + lib.LEVEL) * F)+ ob.rotation_euler = (0, 0, math.radians(rot))+ if collide:+ bpy.context.view_layer.update()+ add_collider([ob])+ return ob+++# rot convention for procedural items: 0 -> front faces -y, 90 -> +x, 180 -> +y, 270 -> -x+def bed(x, y, rot, w=5.0, l=6.6, blanket="throw", panel=None, style="contemporary"):+ """bed made up with sheet, duvet, a throw and pillows. blanket: material of the throw at the foot.+ panel: width of the wood panel on the wall behind the bed (default bed + 1.8 ft each side; 0 = none)+ style: contemporary (upholstered, tufted headboard), low (oak platform, slab headboard), farmhouse (painted planks)"""+ hw = w / 2+ yb, yf = l / 2, -l / 2 # head (back) at +y local+ hp = hw + 1.8 if panel is None else panel / 2+ if hp > 0:+ group("accent_panel", [(-hp, hp, yb - 0.03, yb, 0, lib.WALL_H - 0.01, "wall_accent")],+ x, y, rot, collide=False) # panel sits flush on the wall behind the headboard+ if blanket not in MATS:+ blanket = "throw"+ seed = round(x * 7 + y * 13)+ r = item("bed", x, y, rot)+ dz = 0.0 # mattress height against the contemporary bed's+ if style == "low": # oak platform on a recessed plinth, slab headboard+ dz = -0.35+ p_box(r, "bed_plinth", w - 0.8, l - 1.2, 0.18, "black", (0, -0.3, 0), bevel=0.01)+ p_box(r, "bed_base", w + 0.5, l + 0.1, 0.52, "teak", (0, -0.05, 0.18), bevel=0.03)+ p_box(r, "headboard", w + 0.5, 0.22, 3.0, "teak", (0, yb - 0.11, 0), bevel=0.03)+ elif style == "farmhouse": # painted posts, plank head- and footboard, side rails+ for sx in (-1, 1):+ p_box(r, "bed_post", 0.32, 0.32, 4.3, "painted_light", (sx * (hw + 0.05), yb - 0.16, 0), bevel=0.04)+ p_box(r, "bed_post", 0.32, 0.32, 2.7, "painted_light", (sx * (hw + 0.05), yf + 0.16, 0), bevel=0.04)+ p_box(r, "bed_rail", 0.18, l - 0.6, 0.55, "painted_light", (sx * (hw - 0.02), -0.05, 0.6), bevel=0.02)+ for yy, top in ((yb - 0.16, 3.9), (yf + 0.16, 2.3)):+ n = max(4, round(w / 0.55))+ for i in range(n): # vertical planks with a hairline gap+ p_box(r, "bed_plank", w / n - 0.03, 0.12, top - 0.9, "painted_light", (-hw + w * (i + 0.5) / n, yy, 0.9), bevel=0.01)+ p_box(r, "bed_cap", w + 0.6, 0.26, 0.16, "painted_light", (0, yy, top), bevel=0.03)+ p_box(r, "bed_base", w - 0.1, l - 0.7, 0.45, "painted_light", (0, -0.05, 0.6), bevel=0.02)+ if blanket == "throw":+ blanket = "quilt"+ else:+ p_box(r, "bed_plinth", w - 0.5, l - 0.8, 0.3, "black", (0, -0.2, 0), bevel=0.01)+ p_box(r, "bed_base", w, l - 0.35, 0.75, "teak", (0, -0.175, 0.3), bevel=0.05)+ p_box(r, "headboard_back", w + 0.3, 0.15, 3.9, "teak", (0, yb - 0.1, 0), bevel=0.03)+ n = max(3, round((w + 0.3) / 0.9)) # channel-tufted upholstered headboard+ cw = (w + 0.3) / n+ for i in range(n):+ p_soft(r, "headboard", cw - 0.02, 0.4, 2.9, "velvet_head", (-(w + 0.3) / 2 + cw * (i + 0.5), yb - 0.37, 0.95),+ r=0.17)+ p_soft(r, "mattress", w - 0.15, l - 0.55, 0.75, "sheet", (0, -0.225, 1.05 + dz), r=0.12)+ p_drape(r, "duvet", -hw + 0.08, hw - 0.08, yf + 0.1, yb - 2.1, 1.86 + dz, 0.85, "duvet", "xXy", thick=0.06, seed=seed)+ p_soft(r, "duvet_fold", w - 0.1, 0.55, 0.3, "duvet", (0, yb - 2.1, 1.72 + dz), r=0.14)+ p_drape(r, "throw", -hw, hw, yf + 0.3, yf + 1.7, 1.94 + dz, 1.0, blanket, "xX", thick=0.04, seed=seed)+ pw, xs = (w - 0.6, (0,)) if w < 4 else (hw - 0.3, (-w / 4, w / 4))+ for px in xs: # sleeping pillows leaning on the headboard+ p_soft(r, "pillow", pw, 0.55, 1.35, "sheet", (px, yb - 0.95, 1.75 + dz), (-18, 0, 0), r=0.26)+ if w >= 4: # cushions in front of them+ for k, s in enumerate((-1, 1)):+ p_soft(r, "cushion", 1.25, 0.4, 1.25, "jacquard", (s * (w / 4 - 0.1), yb - 1.45, 1.8 + dz), (-22, 0, s * 6), r=0.2)+ p_soft(r, "cushion", 1.1, 0.35, 0.8, "boucle", (0, yb - 1.6, 1.8 + dz), (-15, 0, 0), r=0.16)+ return done(r)+++def wardrobe(x, y, rot, w=6.0, d=2.0, h=7.0, style="contemporary"):+ """style: contemporary (oak, steel bar handles), flat (handleless white), classic (walnut armoire: plinth, cornice,+ raised panels, brass knobs), farmhouse (painted plank doors, iron hinges and latches)"""+ hw, f = w / 2, -d / 2 # f: the door face+ n = max(2, round(w / 1.5))+ if style == "flat":+ b = [(-hw, hw, f + 0.05, d / 2, 0.25, h, "matte_white"), (-hw + 0.1, hw - 0.1, f + 0.12, d / 2, 0, 0.25, "black")]+ for i in range(n):+ x0 = -hw + w * i / n+ b.append((x0 + 0.01, x0 + w / n - 0.01, f, f + 0.05, 0.27, h - 0.02, "matte_white"))+ return group("wardrobe", b, x, y, rot, bevel=0.005)+ if style == "classic":+ b = [(-hw, hw, f + 0.1, d / 2, 0.5, h - 0.45, "teak"),+ (-hw - 0.08, hw + 0.08, f - 0.02, d / 2, 0, 0.5, "teak"), # plinth+ (-hw - 0.05, hw + 0.05, f, d / 2, h - 0.45, h - 0.25, "teak"), # cornice, stepping out+ (-hw - 0.15, hw + 0.15, f - 0.12, d / 2, h - 0.25, h - 0.12, "teak"),+ (-hw - 0.22, hw + 0.22, f - 0.18, d / 2, h - 0.12, h, "teak")]+ for i in range(n):+ x0, x1 = -hw + w * i / n + 0.03, -hw + w * (i + 1) / n - 0.03+ b.append((x0, x1, f + 0.02, f + 0.1, 0.55, h - 0.5, "teak")) # door+ for z0, z1 in ((0.85, 3.2), (3.55, h - 0.8)): # raised panels+ b.append((x0 + 0.22, x1 - 0.22, f - 0.03, f + 0.02, z0, z1, "teak"))+ kx = x1 - 0.2 if i % 2 == 0 else x0 + 0.2+ b.append((kx - 0.06, kx + 0.06, f - 0.12, f - 0.03, 3.3, 3.42, "brass")) # knob+ return group("wardrobe", b, x, y, rot, bevel=0.02)+ if style == "farmhouse":+ b = [(-hw, hw, f + 0.1, d / 2, 0.35, h - 0.3, "painted_wood"),+ (-hw - 0.05, hw + 0.05, f + 0.02, d / 2, 0, 0.35, "painted_wood"),+ (-hw - 0.12, hw + 0.12, f - 0.06, d / 2, h - 0.3, h, "painted_wood")]+ for i in range(n):+ x0, x1 = -hw + w * i / n + 0.03, -hw + w * (i + 1) / n - 0.03+ k = max(2, round((x1 - x0) / 0.45))+ for j in range(k): # tongue-and-groove planks+ b.append((x0 + (x1 - x0) * j / k + 0.01, x0 + (x1 - x0) * (j + 1) / k - 0.01, f + 0.02, f + 0.1, 0.4, h - 0.35, "painted_wood"))+ for z in (0.8, h - 1.0): # battens and iron strap hinges+ b.append((x0 + 0.05, x1 - 0.05, f - 0.02, f + 0.02, z, z + 0.3, "painted_wood"))+ hx0, hx1 = (x0, x0 + 0.7) if i % 2 == 0 else (x1 - 0.7, x1)+ b.append((hx0, hx1, f - 0.05, f - 0.02, z + 0.1, z + 0.2, "iron"))+ lx = x1 - 0.15 if i % 2 == 0 else x0 + 0.15+ b.append((lx - 0.04, lx + 0.04, f - 0.14, f - 0.02, 3.2, 3.9, "iron")) # latch handle+ return group("wardrobe", b, x, y, rot, bevel=0.01)+ b = [(-hw, hw, -d / 2 + 0.05, d / 2, 0, h, "laminate_w")]+ for i in range(n):+ x0 = -hw + w * i / n+ b.append((x0 + 0.03, x0 + w / n - 0.03, -d / 2, -d / 2 + 0.05, 0.3, h - 0.05, "laminate_w"))+ hx = x0 + (w / n - 0.25 if i % 2 == 0 else 0.25)+ b.append((hx - 0.04, hx + 0.04, -d / 2 - 0.08, -d / 2, 3.0, 4.2, "steel"))+ group("wardrobe", b, x, y, rot, bevel=0.01)+++def wall_cabinet(x0, x1, y0, y1):+ mesh_from_boxes("wall_cabinet", [(x0, x1, y0, y1, 5.0, 7.2, "laminate_w")], coll="furniture",+ bevel=0.01 * F)+++def fridge(x, y, rot):+ group("fridge", [(-1.15, 1.15, -1.0, 1.1, 0, 5.8, "appliance"),+ (0.9, 0.98, -1.1, -1.0, 2.5, 4.5, "steel"), (0.9, 0.98, -1.1, -1.0, 4.7, 5.5, "steel"),+ (-1.14, 1.14, -1.02, -1.0, 4.55, 4.6, "black")], x, y, rot, bevel=0.06)+++def wc(x, y, rot):+ group("wc", [(-0.6, 0.6, -1.0, 0.5, 0, 1.3, "ceramic"), (-0.7, 0.7, 0.5, 1.15, 0, 2.6, "ceramic"),+ (-0.65, 0.65, -1.05, 0.5, 1.3, 1.38, "white_gloss")], x, y, rot, bevel=0.15, seg=3)+++def basin(x, y, rot):+ group("basin", [(-0.9, 0.9, -0.7, 0.6, 2.4, 2.85, "ceramic"), (-0.15, 0.15, -0.2, 0.6, 0, 2.4, "ceramic"),+ (-0.05, 0.05, 0.25, 0.4, 2.85, 3.3, "steel"),+ (-1.0, 1.0, 0.58, 0.62, 3.6, 6.0, "glass"), (-1.05, 1.05, 0.62, 0.66, 3.55, 6.05, "steel")],+ x, y, rot, bevel=0.05, collide=True)+++def shower(x, y):+ group("shower", [(-0.04, 0.04, -0.04, 0.04, 0, 6.8, "steel"), (-0.35, 0.35, -0.35, 0.35, 6.7, 6.78, "steel")],+ x, y, 0, bevel=0.01, collide=False)+++def tv(x, y, rot, z=3.2):+ group("tv", [(-2.0, 2.0, -0.05, 0.1, z, z + 2.3, "black"), (-1.95, 1.95, -0.07, -0.05, z + 0.05, z + 2.25, "screen")],+ x, y, rot, bevel=0.01, collide=False)+++def rug(x0, x1, y0, y1, m="rug"):+ mesh_from_boxes("rug", [(x0, x1, y0, y1, 0, 0.04, m)], coll="furniture", bevel=0.01 * F)+++def desk(x, y, rot):+ group("desk", [(-1.75, 1.75, -1.0, 1.0, 2.4, 2.55, "teak"), (-1.75, -1.6, -1.0, 1.0, 0, 2.4, "teak"),+ (1.6, 1.75, -1.0, 1.0, 0, 2.4, "teak"), (0.3, 1.6, -0.9, 1.0, 1.4, 2.4, "teak")],+ x, y, rot, bevel=0.02)+++def ceiling_light(x, y, s=0.9):+ mesh_from_boxes("ceiling_light", [(x - s, x + s, y - s, y + s, lib.WALL_H - 0.08, lib.WALL_H, "light")],+ coll="furniture")+++++# ---------------------------------------------------------------- real fixtures: curved, bevelled meshes+# (items are assembled from parts under an empty; local feet, front faces -y at rot 0, back against +y)+++def _box_uv(bm, tile):+ """texture UVs in metres / tile, projected along each face's main axis (as mesh_from_boxes does)"""+ uv = bm.loops.layers.uv.new("UVMap")+ for f in bm.faces:+ f.normal_update()+ ax = max(range(3), key=lambda i: abs(f.normal[i]))+ a, c = [(1, 2), (0, 2), (0, 1)][ax]+ for lp in f.loops:+ lp[uv].uv = (lp.vert.co[a] / tile, lp.vert.co[c] / tile)+++def _mesh_obj(name, bm, mn, parent, loc=(0, 0, 0), rot=(0, 0, 0), smooth=40, bevel=0.0, seg=3, solid=0.0, soft=0):+ """soft: subdivision levels after the bevel, for upholstery and pillows"""+ if not bm.loops.layers.uv:+ _box_uv(bm, MATS[mn]["tile"])+ me = bpy.data.meshes.new(name)+ bm.to_mesh(me)+ bm.free()+ me.materials.append(MATS[mn])+ if smooth:+ me.shade_smooth()+ me.set_sharp_from_angle(angle=math.radians(smooth))+ ob = bpy.data.objects.new(name, me)+ collection("furniture").objects.link(ob)+ ob.parent = parent+ ob.location = Vector(loc) * F+ ob.rotation_euler = [math.radians(a) for a in rot]+ if solid:+ md = ob.modifiers.new("solid", "SOLIDIFY")+ md.thickness = solid * F+ if bevel:+ md = ob.modifiers.new("bevel", "BEVEL")+ md.width, md.segments, md.limit_method = bevel * F, seg, "ANGLE"+ md.harden_normals = False+ if soft:+ md = ob.modifiers.new("soft", "SUBSURF")+ md.levels = md.render_levels = soft+ return ob+++def p_soft(root, name, sx, sy, sz, mn, loc=(0, 0, 0), rot=(0, 0, 0), r=0.15):+ """upholstered block: a box with round edges r (feet), smoothed into a slightly puffy cushion"""+ return p_box(root, name, sx, sy, sz, mn, loc, rot, bevel=min(r, sx / 2.2, sy / 2.2, sz / 2.2), seg=2, soft=2,+ smooth=180)+++def p_drape(root, name, x0, x1, y0, y1, top, hang, mn, sides="xX y", thick=0.07, seed=0, step=0.12):+ """cloth lying on the rectangle x0..x1, y0..y1 at height top and falling over the edges named in sides+ (x = -x, X = +x, y = -y, Y = +y) by hang feet, with soft folds; local feet like p_box"""+ rnd = random.Random(seed)+ ex = [hang if "x" in sides else 0, hang if "X" in sides else 0, hang if "y" in sides else 0, hang if "Y" in sides else 0]+ us = [x0 - ex[0] + i * step for i in range(int((x1 - x0 + ex[0] + ex[1]) / step) + 1)]+ vs = [y0 - ex[2] + j * step for j in range(int((y1 - y0 + ex[2] + ex[3]) / step) + 1)]+ ph = [rnd.uniform(0, 6.3) for _ in range(4)]+ bm = bmesh.new()+ uv = bm.loops.layers.uv.new("UVMap")+ grid = []+ for v in vs:+ row = []+ for u in us:+ dx, dy = max(0, x0 - u, u - x1), max(0, y0 - v, v - y1)+ drop = math.hypot(dx, dy)+ flare = 0.04 + 0.1 * drop # cloth stands off the edge a little more as it falls+ px = min(max(u, x0 - flare), x1 + flare)+ py = min(max(v, y0 - flare), y1 + flare)+ k = min(drop, 0.6) / 0.6 # vertical folds: they vary along the edge, not down it+ px += 0.05 * k * math.sin(v * 3.1 + ph[0]) * (1 if u > x1 else -1 if u < x0 else 0)+ py += 0.05 * k * math.sin(u * 3.1 + ph[1]) * (1 if v > y1 else -1 if v < y0 else 0)+ pz = top - drop + (0.02 * math.sin(u * 2.3 + ph[2]) * math.sin(v * 1.9 + ph[3]) if drop == 0 else 0)+ row.append(bm.verts.new((px * F, py * F, pz * F)))+ grid.append(row)+ for j in range(len(vs) - 1):+ for i in range(len(us) - 1):+ f = bm.faces.new((grid[j][i], grid[j][i + 1], grid[j + 1][i + 1], grid[j + 1][i]))+ for lp, (a, b) in zip(f.loops, ((i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1))):+ lp[uv].uv = (us[a] * F / MATS[mn]["tile"], vs[b] * F / MATS[mn]["tile"])+ return _mesh_obj(name, bm, mn, root, smooth=80, solid=thick, soft=1)+++def p_pleats(root, name, width, h, mn, loc=(0, 0, 0), rot=(0, 0, 0), depth=0.14, pitch=0.42):+ """curtain panel hanging from its top edge: width along local x (centred), pleated in y, bottom at z=0"""+ n = max(2, round(width / pitch * 8))+ bm = bmesh.new()+ uv = bm.loops.layers.uv.new("UVMap")+ cols = []+ for i in range(n + 1):+ x = -width / 2 + width * i / n+ y = depth / 2 * math.sin(2 * math.pi * x / pitch)+ cols.append([bm.verts.new((x * F, (y + (depth * 0.4 * math.sin(x * 1.3) if z == 0 else 0)) * F, z * F))+ for z in (0, h)])+ t = MATS[mn]["tile"]+ for i in range(n):+ f = bm.faces.new((cols[i][0], cols[i + 1][0], cols[i + 1][1], cols[i][1]))+ for lp, (a, b) in zip(f.loops, ((i, 0), (i + 1, 0), (i + 1, 1), (i, 1))):+ lp[uv].uv = ((-width / 2 + width * a / n) * 1.3 * F / t, b * h * F / t)+ return _mesh_obj(name, bm, mn, root, loc, rot, smooth=80, solid=0.03)+++def _bm_xform(bm, scale=(1, 1, 1), lift=0.0):+ for v in bm.verts:+ v.co = Vector((v.co.x * scale[0], v.co.y * scale[1], v.co.z * scale[2] + lift))+ return bm+++def p_cyl(root, name, r1, r2, h, mn, loc=(0, 0, 0), rot=(0, 0, 0), sx=1.0, sy=1.0, segs=48, **kw):+ """cone/cylinder, base at local z=0 before rotation; sx/sy squash the cross-section."""+ bm = bmesh.new()+ bmesh.ops.create_cone(bm, cap_ends=True, cap_tris=False, segments=segs, radius1=r1 * F, radius2=r2 * F,+ depth=h * F)+ return _mesh_obj(name, _bm_xform(bm, (sx, sy, 1), h * F / 2), mn, root, loc, rot, **kw)+++def p_box(root, name, sx, sy, sz, mn, loc=(0, 0, 0), rot=(0, 0, 0), bevel=0.02, seg=3, **kw):+ """box centred on x/y, base at z=0 (feet)."""+ bm = bmesh.new()+ bmesh.ops.create_cube(bm, size=1.0)+ kw.setdefault("smooth", 30)+ return _mesh_obj(name, _bm_xform(bm, (sx * F, sy * F, sz * F), sz * F / 2), mn, root, loc, rot,+ bevel=bevel, seg=seg, **kw)+++def p_frustum(root, name, w0, d0, w1, d1, h, mn, loc=(0, 0, 0), **kw):+ """tapered box: bottom w0 x d0, top w1 x d1 (feet), base at z=0."""+ bm = bmesh.new()+ bmesh.ops.create_cube(bm, size=1.0)+ for v in bm.verts:+ top = v.co.z > 0+ v.co = Vector((v.co.x * (w1 if top else w0) * F, v.co.y * (d1 if top else d0) * F, (v.co.z + 0.5) * h * F))+ return _mesh_obj(name, bm, mn, root, loc, smooth=0, **kw)+++def p_bowl(root, name, r, depth, mn, loc=(0, 0, 0), sy=1.0, wall=0.04):+ """open bowl (lower half sphere), rim at local z=0."""+ bm = bmesh.new()+ bmesh.ops.create_uvsphere(bm, u_segments=48, v_segments=24, radius=r * F)+ bmesh.ops.delete(bm, geom=[v for v in bm.verts if v.co.z > 1e-5], context="VERTS")+ _bm_xform(bm, (1, sy, depth / r))+ return _mesh_obj(name, bm, mn, root, loc, smooth=80, solid=wall)+++def p_torus(root, name, R, r, mn, loc=(0, 0, 0), rot=(0, 0, 0), sy=1.0, u=48, v=16):+ bm = bmesh.new()+ rings = []+ for i in range(u):+ a = 2 * math.pi * i / u+ ring = []+ for j in range(v):+ b = 2 * math.pi * j / v+ d = R + r * math.cos(b)+ ring.append(bm.verts.new((d * math.cos(a) * F, d * math.sin(a) * sy * F, r * math.sin(b) * F)))+ rings.append(ring)+ for i in range(u):+ for j in range(v):+ a, b = rings[i], rings[(i + 1) % u]+ bm.faces.new((a[j], b[j], b[(j + 1) % v], a[(j + 1) % v]))+ return _mesh_obj(name, bm, mn, root, loc, rot, smooth=80)+++def p_tube(root, name, pts, radius, mn):+ """swept round tube through local points (feet); used for taps, rails and hoses."""+ cu = bpy.data.curves.new(name, "CURVE")+ cu.dimensions = "3D"+ cu.bevel_depth, cu.bevel_resolution, cu.use_fill_caps = radius * F, 4, True+ sp = cu.splines.new("NURBS")+ sp.points.add(len(pts) - 1)+ for p, c in zip(sp.points, pts):+ p.co = (c[0] * F, c[1] * F, c[2] * F, 1)+ sp.use_endpoint_u, sp.order_u = True, min(4, len(pts))+ tmp = bpy.data.objects.new(name + "_crv", cu)+ scene.collection.objects.link(tmp)+ bpy.context.view_layer.update()+ me = bpy.data.meshes.new_from_object(tmp.evaluated_get(bpy.context.evaluated_depsgraph_get()))+ bpy.data.objects.remove(tmp)+ bpy.data.curves.remove(cu)+ me.materials.clear()+ me.materials.append(MATS[mn])+ me.shade_smooth()+ ob = bpy.data.objects.new(name, me)+ collection("furniture").objects.link(ob)+ ob.parent = root+ return ob+++def item(name, x, y, rot=0, z=0.0):+ root = bpy.data.objects.new(name, None)+ collection("furniture").objects.link(root)+ root.location = (x * F, y * F, (z + lib.LEVEL) * F)+ root.rotation_euler = (0, 0, math.radians(rot))+ return root+++def done(root, collide=True):+ if collide:+ bpy.context.view_layer.update()+ add_collider([c for c in root.children_recursive if c.type == "MESH"])+ return root+++def arc(c, r, a0, a1, n=10, plane="yz"):+ """points on a circular arc (degrees) around centre c in the given plane."""+ pts = []+ for i in range(n + 1):+ a = math.radians(a0 + (a1 - a0) * i / n)+ u, v = r * math.cos(a), r * math.sin(a)+ pts.append((c[0], c[1] + u, c[2] + v) if plane == "yz" else (c[0] + u, c[1], c[2] + v))+ return pts+++def wc2(x, y, rot):+ """floor-mounted one-piece EWC with closed soft-close lid, cistern, chrome flush and health faucet."""+ r = item("wc", x, y, rot)+ p_cyl(r, "wc_body", 0.42, 0.6, 1.22, "ceramic", (0, -0.2, 0), sy=1.38, bevel=0.06, seg=4)+ p_box(r, "wc_skirt", 1.05, 0.7, 1.22, "ceramic", (0, 0.5, 0), bevel=0.12, seg=4)+ p_cyl(r, "wc_seat", 0.62, 0.62, 0.07, "white_gloss", (0, -0.2, 1.22), sy=1.38, bevel=0.03)+ p_cyl(r, "wc_lid", 0.6, 0.6, 0.07, "white_gloss", (0, -0.17, 1.29), sy=1.36, bevel=0.03)+ p_box(r, "wc_cistern", 1.32, 0.55, 1.15, "ceramic", (0, 0.78, 1.25), bevel=0.14, seg=5)+ p_box(r, "wc_cistern_lid", 1.38, 0.6, 0.08, "ceramic", (0, 0.78, 2.4), bevel=0.03)+ p_cyl(r, "wc_flush", 0.11, 0.11, 0.03, "chrome", (0, 0.78, 2.48))+ # health faucet on the side wall-ish bracket with a coiled hose+ p_box(r, "hf_holder", 0.12, 0.08, 0.2, "chrome", (0.95, 0.98, 2.0), bevel=0.02)+ p_cyl(r, "hf_head", 0.05, 0.06, 0.45, "chrome", (0.95, 0.9, 1.95), rot=(15, 0, 0))+ p_tube(r, "hf_hose", [(0.95, 0.95, 1.95), (1.0, 0.9, 1.2), (0.9, 0.85, 0.6), (0.85, 0.95, 1.6),+ (0.9, 1.0, 2.3)], 0.025, "chrome")+ return done(r)+++def vanity(x, y, rot, w=2.6):+ """wall-hung vanity: laminate cabinet, quartz top, vessel basin, tall mixer, backlit mirror."""+ r = item("vanity", x, y, rot)+ d = 1.6+ p_box(r, "vanity_cab", w, d, 1.5, "laminate_w", (0, 0.05, 1.05), bevel=0.02)+ p_box(r, "vanity_groove", w - 0.1, 0.02, 0.04, "black", (0, 0.05 - d / 2 - 0.005, 2.25), bevel=0)+ p_box(r, "vanity_top", w + 0.1, d + 0.1, 0.12, "quartz", (0, 0.05, 2.55), bevel=0.02)+ p_bowl(r, "basin_bowl", 0.62, 0.5, "ceramic", (0, -0.1, 3.15), sy=0.82)+ p_cyl(r, "basin_drain", 0.06, 0.06, 0.01, "chrome", (0, -0.1, 2.68))+ p_cyl(r, "tap_base", 0.07, 0.07, 0.15, "chrome", (0, 0.55, 2.67))+ p_tube(r, "tap_spout", [(0, 0.55, 2.7), (0, 0.55, 3.55)] + arc((0, 0.35, 3.55), 0.2, 0, 160, 8)[1:] ++ [(0, 0.2, 3.35)], 0.045, "chrome")+ p_box(r, "tap_lever", 0.06, 0.25, 0.05, "chrome", (0.1, 0.5, 3.2), rot=(-20, 0, 0), bevel=0.01)+ p_box(r, "mirror_glow", w * 0.9 + 0.1, 0.03, 3.1, "led", (0, d / 2 + 0.02, 3.95), bevel=0)+ p_box(r, "mirror", w * 0.9, 0.06, 3.0, "mirror", (0, d / 2 - 0.02, 4.0), bevel=0.05)+ p_box(r, "soap_dish", 0.35, 0.25, 0.05, "black_glass", (w / 2 - 0.35, 0.4, 2.67), bevel=0.02)+ return done(r)+++def shower2(x, y, rot, glass=None):+ """rain shower on a wall arm + concealed mixer + floor drain; glass = (x0,x1,y0,y1) world screen."""+ r = item("shower", x, y, rot)+ p_tube(r, "shower_arm", [(0, 0.0, 7.4), (0, -0.4, 7.4), (0, -0.9, 7.3), (0, -1.1, 7.15)], 0.05, "chrome")+ p_box(r, "shower_head", 0.85, 0.85, 0.05, "chrome", (0, -1.1, 7.05), bevel=0.02)+ p_cyl(r, "mixer_plate", 0.28, 0.28, 0.04, "chrome", (0, -0.09, 3.8), rot=(90, 0, 0))+ p_box(r, "mixer_lever", 0.06, 0.12, 0.4, "chrome", (0, -0.16, 3.75), bevel=0.02)+ p_box(r, "floor_drain", 0.4, 0.4, 0.015, "steel", (0, -1.1, 0.05), bevel=0)+ p_cyl(r, "hand_shower", 0.05, 0.07, 0.6, "chrome", (0.5, -0.15, 3.9), rot=(0, 0, 0))+ p_tube(r, "hand_hose", [(0.5, -0.15, 3.9), (0.55, -0.25, 2.8), (0.4, -0.2, 2.2), (0.3, -0.1, 3.6)], 0.025,+ "chrome")+ p_box(r, "shower_wall", 2.6, 0.04, 7.6, "stone_wall", (0, -0.07, 0), bevel=0) # in front of the 0.03 ft wall tiles+ p_box(r, "shower_tray", 2.5, 2.5, 0.05, "pebble_floor", (0, -1.27, 0), bevel=0.005)+ done(r, collide=False)+ if glass:+ x0, x1, y0, y1 = glass+ mesh_from_boxes("shower_glass", [(x0, x1, y0, y1, 0.05, 6.8, "glass")], coll="furniture")+ mesh_from_boxes("shower_glass_trim", [(x0, x1, y0, y1, 6.8, 6.85, "chrome"),+ (x0, x1, y0, y1, 0.0, 0.05, "chrome")], coll="furniture")+ return r+++def geyser(x, y, rot, z=6.4):+ r = item("geyser", x, y, rot, z=z)+ p_cyl(r, "geyser_tank", 0.5, 0.5, 1.7, "plastic_white", (-0.85, -0.55, 0.5), rot=(0, 90, 0), bevel=0.12,+ seg=4)+ p_box(r, "geyser_panel", 0.5, 0.05, 0.25, "blue_accent", (0, -1.04, 0.35), bevel=0.02)+ return done(r, collide=False)+++def _doors(r, length, depth, z0, z1, handle_at, front_y, n=None):+ """shaker-ish door fronts with bar handles along local x; front_y = local y of the cabinet face."""+ n = n or max(1, round(length / 1.5))+ dw = length / n+ for i in range(n):+ cx = -length / 2 + dw * (i + 0.5)+ p_box(r, "door", dw - 0.04, 0.06, z1 - z0 - 0.04, "laminate_w", (cx, front_y - 0.03, z0 + 0.02), bevel=0.01)+ p_box(r, "door_panel", dw - 0.4, 0.02, z1 - z0 - 0.5, "laminate_w", (cx, front_y - 0.065, z0 + 0.25),+ bevel=0.005)+ hx = cx + (dw / 2 - 0.25 if i % 2 == 0 else -dw / 2 + 0.25)+ p_box(r, "handle", 0.05, 0.08, 0.7, "chrome", (hx, front_y - 0.1, handle_at - 0.35), bevel=0.015)+++def kitchen_run(x0, x1, y0, y1, front, sink=None, hob=None, doors=True):+ """base cabinets + granite top in world feet; front = side the cook stands ('-y','+y','+x','-x').+ sink / hob = position along the run (world x for runs along x, world y for runs along y)."""+ along_x = front in ("-y", "+y")+ L, Dp = (x1 - x0, y1 - y0) if along_x else (y1 - y0, x1 - x0)+ rot = {"-y": 0, "+x": 90, "+y": 180, "-x": 270}[front]+ r = item("kitchen_run", (x0 + x1) / 2, (y0 + y1) / 2, rot)+ flip = 1 if front in ("-y", "+x") else -1 # local x vs world axis direction after rotation+ c = (x0 + x1) / 2 if along_x else (y0 + y1) / 2++ def local(u): # world coordinate along the run -> local x+ return (u - c) * flip++ p_box(r, "plinth", L, Dp - 0.3, 0.33, "black", (0, 0.15, 0), bevel=0)+ p_box(r, "carcass", L, Dp - 0.05, 2.42, "laminate_w", (0, 0.025, 0.33), bevel=0.005)+ if doors:+ _doors(r, L, Dp, 0.36, 2.72, 2.55, -Dp / 2 + 0.05)+ # granite top with a hole for the sink bowl+ top = [(-L / 2, L / 2, -Dp / 2 - 0.08, Dp / 2, 2.75, 2.9)]+ holes = []+ if sink is not None:+ sx = local(sink)+ holes.append((sx - 0.9, sx + 0.9, -0.55, 0.55, 2.0, 3.0))+ top = subtract_all(top, holes)+ for i, b in enumerate(top):+ p_box(r, "counter_top", b[1] - b[0], b[3] - b[2], 0.15, "stone_top",+ ((b[0] + b[1]) / 2, (b[2] + b[3]) / 2, 2.75), bevel=0.01)+ p_box(r, "backsplash", L, 0.04, 2.0, "walltile", (0, Dp / 2 - 0.02, 2.9), bevel=0)+ if sink is not None:+ sx = local(sink)+ for bx, by, w_, d_ in ((sx, -0.55, 1.8, 0.04), (sx, 0.55, 1.8, 0.04), (sx - 0.9, 0, 0.04, 1.1),+ (sx + 0.9, 0, 0.04, 1.1)):+ p_box(r, "sink_wall", w_, d_, 0.7, "steel", (bx, by, 2.2), bevel=0)+ p_box(r, "sink_floor", 1.8, 1.1, 0.03, "steel", (sx, 0, 2.2), bevel=0)+ p_cyl(r, "sink_drain", 0.12, 0.12, 0.01, "cast_iron", (sx, 0, 2.235))+ p_box(r, "sink_rim", 1.95, 1.25, 0.02, "steel", (sx, 0, 2.9), bevel=0)+ p_box(r, "sink_rim_cut", 1.8, 1.1, 0.03, "steel", (sx, 0, 2.9), bevel=0)+ p_cyl(r, "faucet_base", 0.08, 0.08, 0.1, "chrome", (sx, 0.75, 2.9))+ p_tube(r, "faucet", [(sx, 0.75, 2.95), (sx, 0.75, 3.9)] + arc((sx, 0.45, 3.9), 0.3, 0, 170, 10)[1:] ++ [(sx, 0.15, 3.6)], 0.05, "chrome")+ if hob is not None:+ hx = local(hob)+ p_box(r, "hob_glass", 2.4, 1.5, 0.08, "black_glass", (hx, 0.05, 2.9), bevel=0.02)+ for k, bx in enumerate((-0.75, 0.0, 0.75)):+ rb = 0.22 if k != 1 else 0.28+ p_cyl(r, "burner", rb, rb * 0.9, 0.07, "brass", (hx + bx, 0.15, 2.98))+ p_cyl(r, "burner_cap", rb * 0.6, rb * 0.5, 0.05, "cast_iron", (hx + bx, 0.15, 3.05))+ for a in (0, 45, 90, 135):+ p_box(r, "pan_support", 0.06, rb * 2 + 0.25, 0.08, "cast_iron", (hx + bx, 0.15, 3.0),+ rot=(0, 0, a), bevel=0.01)+ p_cyl(r, "knob", 0.07, 0.07, 0.07, "chrome", (hx + bx, -0.6, 2.98))+ return done(r)+++def wall_cab2(x0, x1, y0, y1, front, z0=4.9, z1=7.2):+ along_x = front in ("-y", "+y")+ L, Dp = (x1 - x0, y1 - y0) if along_x else (y1 - y0, x1 - x0)+ rot = {"-y": 0, "+x": 90, "+y": 180, "-x": 270}[front]+ r = item("wall_cabinet", (x0 + x1) / 2, (y0 + y1) / 2, rot)+ p_box(r, "wall_carcass", L, Dp - 0.05, z1 - z0, "laminate_w", (0, 0.025, z0), bevel=0.005)+ _doors(r, L, Dp, z0, z1, z0 + 0.55, -Dp / 2 + 0.05)+ p_box(r, "under_led", L - 0.2, 0.05, 0.02, "led", (0, -Dp / 2 + 0.3, z0 - 0.02), bevel=0)+ return done(r, collide=False)+++def chimney(x, y, rot):+ """kitchen hood: steel pyramid + black glass skirt + flue box up to the ceiling."""+ r = item("chimney", x, y, rot)+ p_frustum(r, "hood", 2.5, 1.5, 0.95, 0.85, 0.9, "steel", (0, -0.6, 5.05), bevel=0.01)+ p_box(r, "hood_skirt", 2.5, 1.5, 0.2, "black_glass", (0, -0.6, 4.85), bevel=0.01)+ p_box(r, "flue", 0.9, 0.8, lib.WALL_H - 5.9, "steel", (0, -0.55, 5.9), bevel=0.01)+ return done(r, collide=False)+++def fridge2(x, y, rot):+ """brushed steel French-door fridge with a freezer drawer"""+ r = item("fridge", x, y, rot)+ p_box(r, "fridge_body", 2.3, 2.1, 5.8, "steel_brushed", (0, 0.05, 0), bevel=0.08, seg=3)+ p_box(r, "fridge_gap", 2.2, 0.05, 5.6, "black", (0, -1.0, 0.1), bevel=0)+ for s in (-1, 1):+ p_box(r, "fridge_door", 1.12, 0.14, 3.6, "steel_brushed", (s * 0.575, -1.05, 2.15), bevel=0.05, seg=3)+ p_tube(r, "fridge_handle", [(s * 0.1, -1.25, 2.9), (s * 0.1, -1.25, 5.0)], 0.04, "chrome")+ p_box(r, "freezer_drawer", 2.27, 0.14, 1.95, "steel_brushed", (0, -1.05, 0.15), bevel=0.05, seg=3)+ p_tube(r, "freezer_handle", [(-0.7, -1.25, 1.85), (0.7, -1.25, 1.85)], 0.04, "chrome")+ p_box(r, "fridge_display", 0.45, 0.02, 0.6, "black_glass", (-0.55, -1.13, 4.2), bevel=0.01)+ p_box(r, "fridge_led", 0.25, 0.01, 0.06, "led", (-0.55, -1.145, 4.55), bevel=0)+ return done(r)+++def washer2(x, y, rot):+ """front-loading washing machine: porthole door with a steel drum behind tinted glass, detergent drawer, dial"""+ r = item("washing_machine", x, y, rot)+ p_box(r, "wm_body", 2.0, 1.9, 2.75, "white_gloss", (0, 0, 0.05), bevel=0.1, seg=3)+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_cyl(r, "wm_foot", 0.08, 0.08, 0.06, "black", (sx * 0.8, sy * 0.75, 0), segs=12)+ p_box(r, "wm_seam", 1.98, 0.02, 0.02, "plastic_white", (0, -0.955, 2.25), bevel=0)+ p_cyl(r, "wm_drum", 0.5, 0.5, 0.01, "steel_brushed", (0, -0.952, 1.2), rot=(90, 0, 0))+ p_cyl(r, "wm_drum_hub", 0.18, 0.18, 0.012, "cast_iron", (0, -0.958, 1.2), rot=(90, 0, 0), segs=24)+ p_torus(r, "wm_bezel", 0.6, 0.08, "plastic_white", (0, -0.97, 1.2), rot=(90, 0, 0))+ p_torus(r, "wm_ring", 0.52, 0.05, "chrome", (0, -1.03, 1.2), rot=(90, 0, 0))+ p_cyl(r, "wm_glass", 0.5, 0.42, 0.1, "glass", (0, -0.97, 1.2), rot=(90, 0, 0))+ p_box(r, "wm_handle", 0.1, 0.06, 0.35, "chrome", (0.52, -1.07, 1.05), bevel=0.02)+ p_box(r, "wm_drawer", 0.6, 0.04, 0.32, "plastic_white", (-0.6, -0.97, 2.32), bevel=0.02)+ p_box(r, "wm_drawer_grip", 0.3, 0.02, 0.05, "black", (-0.6, -0.995, 2.45), bevel=0)+ p_box(r, "wm_display", 0.45, 0.02, 0.22, "black_glass", (0.1, -0.965, 2.37), bevel=0.01)+ p_box(r, "wm_digits", 0.2, 0.01, 0.06, "led", (0.1, -0.978, 2.45), bevel=0)+ p_cyl(r, "wm_dial", 0.15, 0.13, 0.08, "chrome", (0.65, -0.95, 2.48), rot=(90, 0, 0), segs=24)+ return done(r)+++def puja_mandir(x, y, rot, w=3.6):+ """wooden mandir unit: storage base, back panel, pillars and a stepped shikhara canopy."""+ r = item("mandir", x, y, rot)+ p_box(r, "mandir_base", w, 1.5, 2.4, "teak", (0, 0, 0), bevel=0.03)+ _doors(r, w, 1.5, 0.15, 2.3, 1.9, -0.75, n=2)+ p_box(r, "mandir_back", w, 0.15, 4.2, "teak", (0, 0.68, 2.4), bevel=0.02)+ p_box(r, "mandir_led", w - 0.4, 0.03, 3.6, "led", (0, 0.58, 2.6), bevel=0)+ for s in (-1, 1):+ p_cyl(r, "mandir_pillar", 0.1, 0.1, 3.6, "brass", (s * (w / 2 - 0.2), -0.55, 2.4))+ p_box(r, "mandir_lintel", w + 0.1, 1.5, 0.25, "teak", (0, 0, 6.0), bevel=0.03)+ for k, f in enumerate((0.88, 0.72, 0.55)):+ p_box(r, "mandir_step", w * f, 1.2, 0.25, "teak", (0, 0.05, 6.25 + 0.25 * k), bevel=0.02)+ p_cyl(r, "shikhara", w * 0.26, 0.08, 1.4, "brass", (0, 0.05, 7.0), segs=8)+ return done(r)+++# ------------------------------------------------------------------ procedural furniture not in lib+def nightstand(x, y, rot):+ group("nightstand", [(-0.75, 0.75, -0.7, 0.7, 0.2, 2.0, "teak"), (-0.7, 0.7, -0.75, -0.7, 1.15, 1.95, "laminate_w"),+ (-0.2, 0.2, -0.82, -0.75, 1.5, 1.6, "brass")], x, y, rot, bevel=0.02)+ table_lamp(x, y, rot, 2.0)+++def table_lamp(x, y, rot, z):+ r = item("table_lamp", x, y, rot, z=z)+ p_cyl(r, "lamp_base", 0.18, 0.12, 0.7, "ceramic")+ p_cyl(r, "lamp_shade", 0.45, 0.3, 0.6, "curtain", (0, 0, 0.7), solid=0.01)+ done(r, collide=False)+++def sofa(x, y, rot, w=7.0, style="contemporary"):+ """velvet sofa: rounded arms and back, loose seat and back cushions, throw pillows, brass feet.+ style low: a low, square linen sofa on slim oak legs"""+ hw = w / 2+ r = item("sofa", x, y, rot)+ if style == "low":+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_box(r, "sofa_leg", 0.12, 0.12, 0.4, "teak", (sx * (hw - 0.3), sy * 1.15, 0), bevel=0.01)+ p_soft(r, "sofa_base", w, 3.0, 0.5, "linen_sofa", (0, 0, 0.4), r=0.06)+ for s_ in (-1, 1):+ p_soft(r, "sofa_arm", 0.45, 3.0, 1.15, "linen_sofa", (s_ * (hw - 0.22), 0, 0.4), r=0.08)+ p_soft(r, "sofa_back", w - 0.9, 0.5, 1.6, "linen_sofa", (0, 1.25, 0.4), r=0.08)+ n = max(2, round((w - 0.9) / 2.6))+ cw = (w - 0.9) / n+ for i in range(n):+ cx = -hw + 0.45 + cw * (i + 0.5)+ p_soft(r, "seat_cushion", cw - 0.03, 2.45, 0.42, "linen_sofa", (cx, -0.2, 0.9), r=0.1)+ p_soft(r, "back_cushion", cw - 0.05, 0.5, 1.2, "linen_sofa", (cx, 0.78, 1.25), (-8, 0, 0), r=0.14)+ for s_, m in ((-1, "boucle"), (1, "jacquard")):+ p_soft(r, "throw_pillow", 1.25, 0.35, 1.25, m, (s_ * (hw - 1.3), 0.35, 1.38), (-18, 0, -s_ * 10), r=0.2)+ return done(r)+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_cyl(r, "sofa_foot", 0.08, 0.05, 0.35, "brass", (sx * (hw - 0.35), sy * 1.2, 0), segs=16)+ p_soft(r, "sofa_base", w, 3.0, 0.9, "velvet", (0, 0, 0.35), r=0.12)+ for s_ in (-1, 1):+ p_soft(r, "sofa_arm", 0.62, 3.0, 1.9, "velvet", (s_ * (hw - 0.31), 0, 0.35), r=0.25)+ p_soft(r, "sofa_back", w - 1.2, 0.6, 1.55, "velvet", (0, 1.2, 1.2), r=0.2)+ n = max(2, round((w - 1.24) / 2.2))+ cw = (w - 1.24) / n+ for i in range(n):+ cx = -hw + 0.62 + cw * (i + 0.5)+ p_soft(r, "seat_cushion", cw - 0.04, 2.35, 0.55, "velvet", (cx, -0.3, 1.2), r=0.2)+ p_soft(r, "back_cushion", cw - 0.06, 0.55, 1.45, "velvet", (cx, 0.62, 1.68), (-12, 0, 0), r=0.24)+ for s_, m in ((-1, "jacquard"), (1, "boucle")):+ p_soft(r, "throw_pillow", 1.3, 0.38, 1.3, m, (s_ * (hw - 1.45), 0.2, 1.72), (-24, 0, -s_ * 12), r=0.22)+ return done(r)+++def coffee_table(x, y, rot, style="contemporary"):+ """tea table: oval white marble top on a walnut base with a shelf; books and a bowl on top.+ style plank: a chunky reclaimed-plank table with a slatted shelf"""+ r = item("coffee_table", x, y, rot)+ if style == "plank":+ for k in range(3):+ p_box(r, "plank_top", 3.6, 0.64, 0.16, "raw_planks", (0, -0.66 + 0.66 * k, 1.29), bevel=0.02)+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_box(r, "plank_leg", 0.3, 0.3, 1.29, "raw_planks", (sx * 1.5, sy * 0.68, 0), bevel=0.02)+ p_box(r, "plank_rail", 0.22, 1.6, 0.18, "raw_planks", (sx * 1.5, 0, 0.35), bevel=0.01)+ for k in range(4):+ p_box(r, "plank_shelf", 2.8, 0.34, 0.08, "raw_planks", (0, -0.6 + 0.4 * k, 0.45), bevel=0.01)+ p_box(r, "book", 0.9, 0.65, 0.1, "blue_accent", (-0.8, 0.05, 1.45), (0, 0, 8), bevel=0.01)+ p_bowl(r, "bowl", 0.4, 0.25, "ceramic", (0.7, 0.0, 1.7))+ return done(r)+ p_cyl(r, "table_top", 1.0, 1.0, 0.1, "marble_top", (0, 0, 1.35), sx=1.8, segs=64, bevel=0.03, seg=2)+ p_cyl(r, "table_apron", 0.92, 0.92, 0.18, "teak", (0, 0, 1.17), sx=1.82, segs=64)+ p_cyl(r, "table_shelf", 0.85, 0.85, 0.08, "teak", (0, 0, 0.35), sx=1.8, segs=64)+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_cyl(r, "table_leg", 0.07, 0.1, 1.17, "teak", (sx * 1.05, sy * 0.48, 0), segs=16)+ p_box(r, "book", 0.9, 0.65, 0.1, "blue_accent", (-0.7, 0.05, 1.45), (0, 0, 8), bevel=0.01)+ p_box(r, "book", 0.8, 0.6, 0.08, "throw", (-0.68, 0.08, 1.55), (0, 0, -4), bevel=0.01)+ p_bowl(r, "bowl", 0.35, 0.22, "ceramic", (0.75, -0.05, 1.67))+ p_box(r, "tray", 1.0, 0.6, 0.05, "brass", (0.0, 0.0, 0.43), bevel=0.01)+ return done(r)+++def tv_unit(x, y, rot):+ group("tv_unit", [(-2.5, 2.5, -0.7, 0.7, 0.5, 1.9, "teak"), (-2.4, 2.4, -0.72, -0.7, 0.7, 1.7, "laminate_w")],+ x, y, rot, bevel=0.02)+ r = math.radians(rot) # the unit's back (local +y, 0.7 ft) is on the wall: hang the TV 0.1 ft off it+ tv(x - 0.5 * math.sin(r), y + 0.5 * math.cos(r), rot, z=3.4)+++def dining_set(x, y, rot, w=6.0, d=3.2, seats=6, style="contemporary", table=None, chairs=None):+ """table (style oak: slim top on tapered legs; classic: walnut with an apron and turned legs; table: a model+ piece fitted to w x d) and seats/2 chairs a side (chairs: the theme's chair piece)"""+ hw, hd = w / 2, d / 2+ a = math.radians(rot)+ if table:+ place_piece(table, (x - (hw if rot % 180 == 0 else hd), x + (hw if rot % 180 == 0 else hd),+ y - (hd if rot % 180 == 0 else hw), y + (hd if rot % 180 == 0 else hw)), rot, "dining_set")+ elif style == "oak":+ r = item("dining_table", x, y, rot)+ p_box(r, "table_top", w, d, 0.12, "teak", (0, 0, 2.4), bevel=0.03)+ for sx in (-1, 1):+ for sy in (-1, 1):+ p_cyl(r, "table_leg", 0.06, 0.1, 2.4, "teak", (sx * (hw - 0.45), sy * (hd - 0.35), 0), segs=20)+ done(r)+ elif style == "classic":+ r = item("dining_table", x, y, rot)+ p_box(r, "table_top", w, d, 0.16, "teak", (0, 0, 2.36), bevel=0.04)+ p_box(r, "table_apron", w - 0.5, d - 0.5, 0.35, "teak", (0, 0, 2.01), bevel=0.02)+ for sx in (-1, 1):+ for sy in (-1, 1): # turned legs: a vase on a column on a foot+ lx, ly = sx * (hw - 0.4), sy * (hd - 0.4)+ p_cyl(r, "leg_foot", 0.13, 0.1, 0.25, "teak", (lx, ly, 0), segs=24)+ p_cyl(r, "leg_vase", 0.1, 0.19, 0.55, "teak", (lx, ly, 0.25), segs=24)+ p_cyl(r, "leg_vase", 0.19, 0.08, 0.5, "teak", (lx, ly, 0.8), segs=24)+ p_cyl(r, "leg_column", 0.08, 0.1, 0.71, "teak", (lx, ly, 1.3), segs=24)+ p_box(r, "leg_block", 0.28, 0.28, 0.35, "teak", (lx, ly, 2.01), bevel=0.02)+ done(r)+ else:+ group("dining_table", [(-hw, hw, -hd, hd, 2.35, 2.5, "teak"),+ *[(sx * (hw - 0.4) - 0.12, sx * (hw - 0.4) + 0.12, sy * (hd - 0.4) - 0.12, sy * (hd - 0.4) + 0.12, 0, 2.35, "teak")+ for sx in (-1, 1) for sy in (-1, 1)]], x, y, rot, bevel=0.02)+ cw, cd = (themes.LIBRARY[chairs][1:3] if chairs in themes.LIBRARY else (1.6, 1.7))+ per = seats // 2+ for side in (-1, 1):+ for i in range(per):+ cx = -hw + w * (i + 0.5) / per+ cy = side * (hd + (cd / 2 - 0.25 if chairs in themes.LIBRARY or chairs == "upload" else 0.6))+ wx, wy = x + cx * math.cos(a) - cy * math.sin(a), y + cx * math.sin(a) + cy * math.cos(a)+ crot = rot + (180 if side < 0 else 0) # backrest on the side away from the table+ if chairs in themes.LIBRARY or chairs == "upload":+ place_piece(chairs, (wx - cw / 2, wx + cw / 2, wy - cd / 2, wy + cd / 2) if crot % 180 == 0 else+ (wx - cd / 2, wx + cd / 2, wy - cw / 2, wy + cw / 2), crot, "dining_chair")+ elif chairs == "@plain":+ plain_chair(wx, wy, crot)+ else:+ chair(wx, wy, crot)+++def plain_chair(x, y, rot):+ """oak frame, linen seat pad, a gently curved two-rail back (front faces -y at rot 0, back on +y)"""+ r = item("chair", x, y, rot)+ for sx in (-1, 1):+ p_cyl(r, "chair_leg", 0.05, 0.065, 1.45, "teak", (sx * 0.62, -0.6, 0), segs=16)+ p_box(r, "chair_back_leg", 0.12, 0.12, 2.95, "teak", (sx * 0.62, 0.62, 0), (-6, 0, 0), bevel=0.02)+ p_box(r, "chair_seat", 1.5, 1.5, 0.12, "teak", (0, 0, 1.45), bevel=0.03)+ p_soft(r, "chair_pad", 1.38, 1.38, 0.14, "linen_sofa", (0, -0.02, 1.57), r=0.05)+ for z in (2.2, 2.65):+ p_box(r, "chair_rail", 1.36, 0.08, 0.22, "teak", (0, 0.7, z), bevel=0.02)+ return done(r, collide=False)+++def chair(x, y, rot):+ """at rot 0 the backrest ends up on +y (drawn on -y, turned 180 by the group call)"""+ group("chair", [(-0.8, 0.8, -0.8, 0.8, 1.45, 1.6, "teak"), (-0.8, 0.8, -0.85, -0.7, 1.6, 3.2, "teak"),+ *[(sx * 0.65 - 0.07, sx * 0.65 + 0.07, sy * 0.65 - 0.07, sy * 0.65 + 0.07, 0, 1.45, "teak")+ for sx in (-1, 1) for sy in (-1, 1)],+ (-0.7, 0.7, -0.7, 0.7, 1.6, 1.75, "cushion")], x, y, rot + 180, bevel=0.02, collide=False)+++def car(x, y, rot):+ place("covered_car", x, y, rot, collide=False)++++++def blob(root, name, r, loc, mn="foliage", sub=2, jitter=0.18, seed=0):+ """lumpy low-poly sphere (icosphere with random push) for foliage"""+ rnd = random.Random(seed)+ bm = bmesh.new()+ bmesh.ops.create_icosphere(bm, subdivisions=sub, radius=r * lib.F)+ for v in bm.verts:+ v.co *= 1 + rnd.uniform(-jitter, jitter)+ return _mesh_obj(name, bm, mn, root, loc, smooth=60)+++def tree(x, y, s=1.0, seed=0):+ """light procedural tree: tapered trunk + clustered canopy (~3k triangles)"""+ rnd = random.Random(seed)+ r = item("tree", x, y, 0)+ p_cyl(r, "trunk", 0.45 * s, 0.25 * s, 9 * s, "bark", segs=12)+ for k in range(6):+ a = rnd.uniform(0, 2 * math.pi)+ d = rnd.uniform(1.0, 3.2) * s+ blob(r, "canopy", rnd.uniform(2.6, 3.8) * s, (math.cos(a) * d, math.sin(a) * d, (9 + rnd.uniform(0, 4)) * s),+ sub=3, seed=seed * 10 + k)+ return done(r, collide=False)+++def plant(x, y, rot):+ r = item("plant", x, y, rot)+ p_cyl(r, "pot", 0.55, 0.75, 1.5, "terracotta", segs=24)+ p_cyl(r, "soil", 0.68, 0.68, 0.05, "concrete", (0, 0, 1.4), segs=24)+ for k, (dx, dy, dz, rr) in enumerate(((0, 0, 2.6, 0.9), (0.4, 0.2, 3.3, 0.7), (-0.35, -0.2, 3.1, 0.65), (0, 0.1, 3.9, 0.55))):+ blob(r, "leaves", rr, (dx, dy, dz), sub=2, jitter=0.25, seed=int(rot) + k)+ return done(r, collide=False)+++def outdoor_set(x, y, rot):+ place("outdoor_table_chair_set_01", x, y, rot, collide=False)+++++def bathtub(x, y, rot, l=5.4, w=2.6, h=1.8):+ """built-in tub along a wall (long side on local x, back against +y)"""+ t, hl, hw = 0.25, l / 2, w / 2+ group("bathtub", [(-hl, hl, -hw, hw, 0, 0.35, "white_gloss"),+ (-hl, hl, -hw, -hw + t, 0.35, h, "white_gloss"), (-hl, hl, hw - t, hw, 0.35, h, "white_gloss"),+ (-hl, -hl + t, -hw + t, hw - t, 0.35, h, "white_gloss"), (hl - t, hl, -hw + t, hw - t, 0.35, h, "white_gloss"),+ (-hl + 0.4, -hl + 0.55, hw - 0.5, hw - 0.3, h, h + 0.6, "chrome"),+ (-hl + 0.4, -hl + 0.95, hw - 0.42, hw - 0.36, h + 0.5, h + 0.58, "chrome")], x, y, rot, bevel=0.04)+++def round_rug(x, y, r=4.0):+ root = item("round_rug", x, y, 0)+ p_cyl(root, "rug", r, r, 0.05, "rug_round", segs=72)+ done(root, collide=False)+++def model(name, x, y, rot=0, z=0.0, scale=1.0):+ place(name, x, y, rot, z=z, scale=scale, collide=False)+++UPLOADS, BOUNDS = {}, {}+++def protos(piece, slot):+ """the template objects of a theme piece (imported once, then copied per use: glTF instancing), or None"""+ if piece in themes.LIBRARY:+ return load_model(piece)+ key = themes.upload_path(T["id"], slot)+ if key not in UPLOADS:+ before = set(bpy.data.objects)+ try:+ bpy.ops.import_scene.gltf(filepath=key)+ except RuntimeError as e: # a broken file: the built-in piece stands in for it+ print("UPLOADED MODEL FAILED", slot, e, flush=True)+ new = [o for o in bpy.data.objects if o not in before]+ for o in new:+ for c in list(o.users_collection):+ c.objects.unlink(o)+ for m in {sl.material for sl in o.material_slots if sl.material and sl.material.use_nodes} if o.type == "MESH" else ():+ glass_from_transmission(m)+ UPLOADS[key] = new if any(o.type == "MESH" for o in new) else None+ return UPLOADS[key]+++def place_piece(piece, box, rot, slot, z=0.0):+ """a model piece fitted into box (feet; its front facing the way rot says), standing z ft above the floor.+ -> its height in feet as placed, or None if there is no model to place"""+ objs = protos(piece, slot)+ if not objs:+ return None+ if id(objs) not in BOUNDS:+ bpy.context.view_layer.update()+ pts = [o.matrix_world @ Vector(c) for o in objs if o.type == "MESH" for c in o.bound_box]+ BOUNDS[id(objs)] = (Vector([min(p[i] for p in pts) for i in range(3)]), Vector([max(p[i] for p in pts) for i in range(3)]))+ lo, hi = BOUNDS[id(objs)]+ turn = themes.LIBRARY[piece][4] if piece in themes.LIBRARY else 0+ mw, md = (hi.y - lo.y, hi.x - lo.x) if turn % 180 == 90 else (hi.x - lo.x, hi.y - lo.y)+ x0, x1, y0, y1 = box+ bw, bd = (y1 - y0, x1 - x0) if round(rot) % 180 == 90 else (x1 - x0, y1 - y0)+ s = min(bw * lib.F / max(mw, 1e-6), bd * lib.F / max(md, 1e-6))+ root = bpy.data.objects.new(f"{slot}_{piece}", None)+ collection("furniture").objects.link(root)+ copies = {}+ for o in objs:+ c = o.copy() # shares the mesh+ copies[o] = c+ collection("furniture").objects.link(c)+ for o, c in copies.items():+ if o.parent in copies:+ c.parent = copies[o.parent]+ else:+ c.parent = root+ c.location = o.location - Vector(((lo.x + hi.x) / 2, (lo.y + hi.y) / 2, lo.z)) # centred, standing on z+ root.scale = (s, s, s)+ root.rotation_euler = (0, 0, math.radians(rot + turn))+ root.location = ((x0 + x1) / 2 * lib.F, (y0 + y1) / 2 * lib.F, (lib.LEVEL + z) * lib.F)+ return (hi.z - lo.z) * s / lib.F+++def build(it):+ """one furniture item in the theme's piece for its slot: a model fitted to the item's box, or a built-in piece"""+ kw = dict(it["kw"])+ fn, piece = it["fn"], kw.pop("piece", None) or T["slots"].get(it["fn"])+ x, y, rot = it["args"][:3] if len(it["args"]) >= 3 else (0, 0, 0)+ if fn == "dining_set":+ chairs = T["slots"]["dining_chair"]+ if piece in themes.LIBRARY or piece == "upload":+ return dining_set(*it["args"], **{k: v for k, v in kw.items() if k in ("w", "d", "seats")}, table=piece, chairs=chairs)+ return dining_set(*it["args"], **kw, chairs=chairs, **({"style": piece[1:]} if piece and piece != "@contemporary" else {}))+ if piece in themes.LIBRARY or piece == "upload":+ h = place_piece(piece, it["box"], rot, fn, z=kw.get("z", 0.0))+ if h is not None:+ if fn == "nightstand":+ table_lamp(x, y, rot, h)+ if fn == "tv_unit" and T["tv"]: # the TV on top, against the wall behind the unit+ b, r = it["box"], math.radians(rot)+ back = ((b[3] - b[2]) if round(rot) % 180 == 0 else (b[1] - b[0])) / 2+ tv(x - (back - 0.2) * math.sin(r), y + (back - 0.2) * math.cos(r), rot, z=h + 0.4)+ return+ piece = None # (an upload that failed: the built-in piece)+ if fn not in CALLS: # decor of a theme no longer chosen+ return+ if piece and piece.startswith("@") and piece != "@contemporary" and fn in STYLED:+ kw["style"] = piece[1:]+ kw.pop("z", None)+ CALLS[fn](*it["args"], **kw)+++def hang_light(piece, cx, cy):+ """a theme's ceiling light over (cx, cy): hung from the ceiling, its bottom no lower than 7.2 ft"""+ w, d, h = themes.LIBRARY[piece][1:4] if piece in themes.LIBRARY else (2.0, 2.0, 2.5)+ s = min(1.0, max(0.3, (lib.WALL_H - 7.2) / h))+ place_piece(piece, (cx - w * s / 2, cx + w * s / 2, cy - d * s / 2, cy + d * s / 2), 0, "light", z=lib.WALL_H - h * s)+++CALLS = dict(bathtub=bathtub, round_rug=round_rug, model=model, bed=bed, nightstand=nightstand, wardrobe=wardrobe, desk=desk, wc2=wc2, vanity=vanity,+ shower2=lambda x, y, rot: shower2(x, y, rot), geyser=lambda x, y, rot: geyser(x, y, rot),+ kitchen_run=kitchen_run, chimney=chimney, wall_cab2=wall_cab2, fridge2=fridge2, washer2=washer2,+ puja_mandir=puja_mandir, sofa=sofa, coffee_table=coffee_table, rug=rug, tv_unit=tv_unit,+ dining_set=dining_set, car=car, plant=plant, outdoor_set=outdoor_set)+CALLS.update(light=lambda x, y, rot, z=0.0: ceiling_light(x, y, 0.7), # (only the Themes page's preview row+ dining_chair=lambda x, y, rot, style=None: (plain_chair if style == "plain" else chair)(x, y, rot)) # has these)+STYLED = {"bed", "wardrobe", "sofa", "coffee_table", "dining_chair"} # builders that take style= (dining_set: see build)diff --git a/blender/gen.py b/blender/gen.pyindex 42c95fd..78aa9ee 100644--- a/blender/gen.py+++ b/blender/gen.py@@ -1,7 +1,7 @@"""plan JSON -> furnished 3D house -> baked lighting -> web model (house.glb + lightmaps + meta.json).Run: blender -b --factory-startup -P planto3d/blender/gen.py -- plan.json OUTDIR [--quality draft|high] [--no-bake]Progress lines: 'PROGRESS <0-100> <message>' on stdout (read by the server)."""-import json, math, os, shutil, sys, time+import json, math, os, shutil, sysHERE = os.path.dirname(os.path.abspath(__file__))sys.path[:0] = [HERE, os.path.join(HERE, "..")]@@ -12,12 +12,13 @@ BAKE = "--no-bake" not in argvos.makedirs(JOB, exist_ok=True)import bpy # noqa: E402-from lib import * # noqa: E402,F401import lib # noqa: E402+from lib import ASSETS, MATS, collection, disjoint, mat, mesh_from_boxes, overlap, scene, set_level, subtract_all # noqa: E402+import furniture # noqa: E402+from furniture import build, ceiling_light, done, hang_light, item, p_cyl, p_pleats, place, tree # noqa: E402import furnish # noqa: E402from quantities import FLOOR_MAT, TILE_H, opening_heights, zone_at # noqa: E402 (shared with the quantities sheet)import themes # noqa: E402-from mathutils import Vector # noqa: E402def progress(p, msg):@@ -31,6 +32,7 @@ lib.WALL_H = Hfloors = plan["floors"]NF = len(floors)T = themes.load(plan.get("theme")) # furniture, decor, colours and floors of the interior theme+furniture.T = Tprogress(2, "building walls")mat("tile", tex="large_floor_tiles_02", tile=1.5)@@ -189,338 +191,6 @@ def stair_parts(f, rise):return boxes, meta-# ------------------------------------------------------------------ procedural furniture not in lib-def nightstand(x, y, rot):- group("nightstand", [(-0.75, 0.75, -0.7, 0.7, 0.2, 2.0, "teak"), (-0.7, 0.7, -0.75, -0.7, 1.15, 1.95, "laminate_w"),- (-0.2, 0.2, -0.82, -0.75, 1.5, 1.6, "brass")], x, y, rot, bevel=0.02)- table_lamp(x, y, rot, 2.0)---def table_lamp(x, y, rot, z):- r = item("table_lamp", x, y, rot, z=z)- p_cyl(r, "lamp_base", 0.18, 0.12, 0.7, "ceramic")- p_cyl(r, "lamp_shade", 0.45, 0.3, 0.6, "curtain", (0, 0, 0.7), solid=0.01)- done(r, collide=False)---def sofa(x, y, rot, w=7.0, style="contemporary"):- """velvet sofa: rounded arms and back, loose seat and back cushions, throw pillows, brass feet.- style low: a low, square linen sofa on slim oak legs"""- hw = w / 2- r = item("sofa", x, y, rot)- if style == "low":- for sx in (-1, 1):- for sy in (-1, 1):- p_box(r, "sofa_leg", 0.12, 0.12, 0.4, "teak", (sx * (hw - 0.3), sy * 1.15, 0), bevel=0.01)- p_soft(r, "sofa_base", w, 3.0, 0.5, "linen_sofa", (0, 0, 0.4), r=0.06)- for s_ in (-1, 1):- p_soft(r, "sofa_arm", 0.45, 3.0, 1.15, "linen_sofa", (s_ * (hw - 0.22), 0, 0.4), r=0.08)- p_soft(r, "sofa_back", w - 0.9, 0.5, 1.6, "linen_sofa", (0, 1.25, 0.4), r=0.08)- n = max(2, round((w - 0.9) / 2.6))- cw = (w - 0.9) / n- for i in range(n):- cx = -hw + 0.45 + cw * (i + 0.5)- p_soft(r, "seat_cushion", cw - 0.03, 2.45, 0.42, "linen_sofa", (cx, -0.2, 0.9), r=0.1)- p_soft(r, "back_cushion", cw - 0.05, 0.5, 1.2, "linen_sofa", (cx, 0.78, 1.25), (-8, 0, 0), r=0.14)- for s_, m in ((-1, "boucle"), (1, "jacquard")):- p_soft(r, "throw_pillow", 1.25, 0.35, 1.25, m, (s_ * (hw - 1.3), 0.35, 1.38), (-18, 0, -s_ * 10), r=0.2)- return done(r)- for sx in (-1, 1):- for sy in (-1, 1):- p_cyl(r, "sofa_foot", 0.08, 0.05, 0.35, "brass", (sx * (hw - 0.35), sy * 1.2, 0), segs=16)- p_soft(r, "sofa_base", w, 3.0, 0.9, "velvet", (0, 0, 0.35), r=0.12)- for s_ in (-1, 1):- p_soft(r, "sofa_arm", 0.62, 3.0, 1.9, "velvet", (s_ * (hw - 0.31), 0, 0.35), r=0.25)- p_soft(r, "sofa_back", w - 1.2, 0.6, 1.55, "velvet", (0, 1.2, 1.2), r=0.2)- n = max(2, round((w - 1.24) / 2.2))- cw = (w - 1.24) / n- for i in range(n):- cx = -hw + 0.62 + cw * (i + 0.5)- p_soft(r, "seat_cushion", cw - 0.04, 2.35, 0.55, "velvet", (cx, -0.3, 1.2), r=0.2)- p_soft(r, "back_cushion", cw - 0.06, 0.55, 1.45, "velvet", (cx, 0.62, 1.68), (-12, 0, 0), r=0.24)- for s_, m in ((-1, "jacquard"), (1, "boucle")):- p_soft(r, "throw_pillow", 1.3, 0.38, 1.3, m, (s_ * (hw - 1.45), 0.2, 1.72), (-24, 0, -s_ * 12), r=0.22)- return done(r)---def coffee_table(x, y, rot, style="contemporary"):- """tea table: oval white marble top on a walnut base with a shelf; books and a bowl on top.- style plank: a chunky reclaimed-plank table with a slatted shelf"""- r = item("coffee_table", x, y, rot)- if style == "plank":- for k in range(3):- p_box(r, "plank_top", 3.6, 0.64, 0.16, "raw_planks", (0, -0.66 + 0.66 * k, 1.29), bevel=0.02)- for sx in (-1, 1):- for sy in (-1, 1):- p_box(r, "plank_leg", 0.3, 0.3, 1.29, "raw_planks", (sx * 1.5, sy * 0.68, 0), bevel=0.02)- p_box(r, "plank_rail", 0.22, 1.6, 0.18, "raw_planks", (sx * 1.5, 0, 0.35), bevel=0.01)- for k in range(4):- p_box(r, "plank_shelf", 2.8, 0.34, 0.08, "raw_planks", (0, -0.6 + 0.4 * k, 0.45), bevel=0.01)- p_box(r, "book", 0.9, 0.65, 0.1, "blue_accent", (-0.8, 0.05, 1.45), (0, 0, 8), bevel=0.01)- p_bowl(r, "bowl", 0.4, 0.25, "ceramic", (0.7, 0.0, 1.7))- return done(r)- p_cyl(r, "table_top", 1.0, 1.0, 0.1, "marble_top", (0, 0, 1.35), sx=1.8, segs=64, bevel=0.03, seg=2)- p_cyl(r, "table_apron", 0.92, 0.92, 0.18, "teak", (0, 0, 1.17), sx=1.82, segs=64)- p_cyl(r, "table_shelf", 0.85, 0.85, 0.08, "teak", (0, 0, 0.35), sx=1.8, segs=64)- for sx in (-1, 1):- for sy in (-1, 1):- p_cyl(r, "table_leg", 0.07, 0.1, 1.17, "teak", (sx * 1.05, sy * 0.48, 0), segs=16)- p_box(r, "book", 0.9, 0.65, 0.1, "blue_accent", (-0.7, 0.05, 1.45), (0, 0, 8), bevel=0.01)- p_box(r, "book", 0.8, 0.6, 0.08, "throw", (-0.68, 0.08, 1.55), (0, 0, -4), bevel=0.01)- p_bowl(r, "bowl", 0.35, 0.22, "ceramic", (0.75, -0.05, 1.67))- p_box(r, "tray", 1.0, 0.6, 0.05, "brass", (0.0, 0.0, 0.43), bevel=0.01)- return done(r)---def tv_unit(x, y, rot):- group("tv_unit", [(-2.5, 2.5, -0.7, 0.7, 0.5, 1.9, "teak"), (-2.4, 2.4, -0.72, -0.7, 0.7, 1.7, "laminate_w")],- x, y, rot, bevel=0.02)- r = math.radians(rot) # the unit's back (local +y, 0.7 ft) is on the wall: hang the TV 0.1 ft off it- tv(x - 0.5 * math.sin(r), y + 0.5 * math.cos(r), rot, z=3.4)---def dining_set(x, y, rot, w=6.0, d=3.2, seats=6, style="contemporary", table=None, chairs=None):- """table (style oak: slim top on tapered legs; classic: walnut with an apron and turned legs; table: a model- piece fitted to w x d) and seats/2 chairs a side (chairs: the theme's chair piece)"""- hw, hd = w / 2, d / 2- a = math.radians(rot)- if table:- place_piece(table, (x - (hw if rot % 180 == 0 else hd), x + (hw if rot % 180 == 0 else hd),- y - (hd if rot % 180 == 0 else hw), y + (hd if rot % 180 == 0 else hw)), rot, "dining_set")- elif style == "oak":- r = item("dining_table", x, y, rot)- p_box(r, "table_top", w, d, 0.12, "teak", (0, 0, 2.4), bevel=0.03)- for sx in (-1, 1):- for sy in (-1, 1):- p_cyl(r, "table_leg", 0.06, 0.1, 2.4, "teak", (sx * (hw - 0.45), sy * (hd - 0.35), 0), segs=20)- done(r)- elif style == "classic":- r = item("dining_table", x, y, rot)- p_box(r, "table_top", w, d, 0.16, "teak", (0, 0, 2.36), bevel=0.04)- p_box(r, "table_apron", w - 0.5, d - 0.5, 0.35, "teak", (0, 0, 2.01), bevel=0.02)- for sx in (-1, 1):- for sy in (-1, 1): # turned legs: a vase on a column on a foot- lx, ly = sx * (hw - 0.4), sy * (hd - 0.4)- p_cyl(r, "leg_foot", 0.13, 0.1, 0.25, "teak", (lx, ly, 0), segs=24)- p_cyl(r, "leg_vase", 0.1, 0.19, 0.55, "teak", (lx, ly, 0.25), segs=24)- p_cyl(r, "leg_vase", 0.19, 0.08, 0.5, "teak", (lx, ly, 0.8), segs=24)- p_cyl(r, "leg_column", 0.08, 0.1, 0.71, "teak", (lx, ly, 1.3), segs=24)- p_box(r, "leg_block", 0.28, 0.28, 0.35, "teak", (lx, ly, 2.01), bevel=0.02)- done(r)- else:- group("dining_table", [(-hw, hw, -hd, hd, 2.35, 2.5, "teak"),- *[(sx * (hw - 0.4) - 0.12, sx * (hw - 0.4) + 0.12, sy * (hd - 0.4) - 0.12, sy * (hd - 0.4) + 0.12, 0, 2.35, "teak")- for sx in (-1, 1) for sy in (-1, 1)]], x, y, rot, bevel=0.02)- cw, cd = (themes.LIBRARY[chairs][1:3] if chairs in themes.LIBRARY else (1.6, 1.7))- per = seats // 2- for side in (-1, 1):- for i in range(per):- cx = -hw + w * (i + 0.5) / per- cy = side * (hd + (cd / 2 - 0.25 if chairs in themes.LIBRARY or chairs == "upload" else 0.6))- wx, wy = x + cx * math.cos(a) - cy * math.sin(a), y + cx * math.sin(a) + cy * math.cos(a)- crot = rot + (180 if side < 0 else 0) # backrest on the side away from the table- if chairs in themes.LIBRARY or chairs == "upload":- place_piece(chairs, (wx - cw / 2, wx + cw / 2, wy - cd / 2, wy + cd / 2) if crot % 180 == 0 else- (wx - cd / 2, wx + cd / 2, wy - cw / 2, wy + cw / 2), crot, "dining_chair")- elif chairs == "@plain":- plain_chair(wx, wy, crot)- else:- chair(wx, wy, crot)---def plain_chair(x, y, rot):- """oak frame, linen seat pad, a gently curved two-rail back (front faces -y at rot 0, back on +y)"""- r = item("chair", x, y, rot)- for sx in (-1, 1):- p_cyl(r, "chair_leg", 0.05, 0.065, 1.45, "teak", (sx * 0.62, -0.6, 0), segs=16)- p_box(r, "chair_back_leg", 0.12, 0.12, 2.95, "teak", (sx * 0.62, 0.62, 0), (-6, 0, 0), bevel=0.02)- p_box(r, "chair_seat", 1.5, 1.5, 0.12, "teak", (0, 0, 1.45), bevel=0.03)- p_soft(r, "chair_pad", 1.38, 1.38, 0.14, "linen_sofa", (0, -0.02, 1.57), r=0.05)- for z in (2.2, 2.65):- p_box(r, "chair_rail", 1.36, 0.08, 0.22, "teak", (0, 0.7, z), bevel=0.02)- return done(r, collide=False)---def chair(x, y, rot):- """at rot 0 the backrest ends up on +y (drawn on -y, turned 180 by the group call)"""- group("chair", [(-0.8, 0.8, -0.8, 0.8, 1.45, 1.6, "teak"), (-0.8, 0.8, -0.85, -0.7, 1.6, 3.2, "teak"),- *[(sx * 0.65 - 0.07, sx * 0.65 + 0.07, sy * 0.65 - 0.07, sy * 0.65 + 0.07, 0, 1.45, "teak")- for sx in (-1, 1) for sy in (-1, 1)],- (-0.7, 0.7, -0.7, 0.7, 1.6, 1.75, "cushion")], x, y, rot + 180, bevel=0.02, collide=False)---def car(x, y, rot):- place("covered_car", x, y, rot, collide=False)---import bmesh, random # noqa: E402--mat("foliage", rough=0.9, tex="leafy_grass", tile=0.6, tint=(0.35, 0.55, 0.25))-mat("bark", (0.25, 0.18, 0.12), 0.9)---def blob(root, name, r, loc, mn="foliage", sub=2, jitter=0.18, seed=0):- """lumpy low-poly sphere (icosphere with random push) for foliage"""- rnd = random.Random(seed)- bm = bmesh.new()- bmesh.ops.create_icosphere(bm, subdivisions=sub, radius=r * lib.F)- for v in bm.verts:- v.co *= 1 + rnd.uniform(-jitter, jitter)- return lib._mesh_obj(name, bm, mn, root, loc, smooth=60)---def tree(x, y, s=1.0, seed=0):- """light procedural tree: tapered trunk + clustered canopy (~3k triangles)"""- rnd = random.Random(seed)- r = item("tree", x, y, 0)- p_cyl(r, "trunk", 0.45 * s, 0.25 * s, 9 * s, "bark", segs=12)- for k in range(6):- a = rnd.uniform(0, 2 * math.pi)- d = rnd.uniform(1.0, 3.2) * s- blob(r, "canopy", rnd.uniform(2.6, 3.8) * s, (math.cos(a) * d, math.sin(a) * d, (9 + rnd.uniform(0, 4)) * s),- sub=3, seed=seed * 10 + k)- return done(r, collide=False)---def plant(x, y, rot):- r = item("plant", x, y, rot)- p_cyl(r, "pot", 0.55, 0.75, 1.5, "terracotta", segs=24)- p_cyl(r, "soil", 0.68, 0.68, 0.05, "concrete", (0, 0, 1.4), segs=24)- for k, (dx, dy, dz, rr) in enumerate(((0, 0, 2.6, 0.9), (0.4, 0.2, 3.3, 0.7), (-0.35, -0.2, 3.1, 0.65), (0, 0.1, 3.9, 0.55))):- blob(r, "leaves", rr, (dx, dy, dz), sub=2, jitter=0.25, seed=int(rot) + k)- return done(r, collide=False)---def outdoor_set(x, y, rot):- place("outdoor_table_chair_set_01", x, y, rot, collide=False)---mat("rug_round", rough=1.0, tex="caban", tile=0.9, tint=(0.86, 0.83, 0.78))---def bathtub(x, y, rot, l=5.4, w=2.6, h=1.8):- """built-in tub along a wall (long side on local x, back against +y)"""- t, hl, hw = 0.25, l / 2, w / 2- group("bathtub", [(-hl, hl, -hw, hw, 0, 0.35, "white_gloss"),- (-hl, hl, -hw, -hw + t, 0.35, h, "white_gloss"), (-hl, hl, hw - t, hw, 0.35, h, "white_gloss"),- (-hl, -hl + t, -hw + t, hw - t, 0.35, h, "white_gloss"), (hl - t, hl, -hw + t, hw - t, 0.35, h, "white_gloss"),- (-hl + 0.4, -hl + 0.55, hw - 0.5, hw - 0.3, h, h + 0.6, "chrome"),- (-hl + 0.4, -hl + 0.95, hw - 0.42, hw - 0.36, h + 0.5, h + 0.58, "chrome")], x, y, rot, bevel=0.04)---def round_rug(x, y, r=4.0):- root = item("round_rug", x, y, 0)- p_cyl(root, "rug", r, r, 0.05, "rug_round", segs=72)- done(root, collide=False)---def model(name, x, y, rot=0, z=0.0, scale=1.0):- place(name, x, y, rot, z=z, scale=scale, collide=False)---UPLOADS, BOUNDS = {}, {}---def protos(piece, slot):- """the template objects of a theme piece (imported once, then copied per use: glTF instancing), or None"""- if piece in themes.LIBRARY:- return lib.load_model(piece)- key = themes.upload_path(T["id"], slot)- if key not in UPLOADS:- before = set(bpy.data.objects)- try:- bpy.ops.import_scene.gltf(filepath=key)- except RuntimeError as e: # a broken file: the built-in piece stands in for it- print("UPLOADED MODEL FAILED", slot, e, flush=True)- new = [o for o in bpy.data.objects if o not in before]- for o in new:- for c in list(o.users_collection):- c.objects.unlink(o)- for m in {sl.material for sl in o.material_slots if sl.material and sl.material.use_nodes} if o.type == "MESH" else ():- lib.glass_from_transmission(m)- UPLOADS[key] = new if any(o.type == "MESH" for o in new) else None- return UPLOADS[key]---def place_piece(piece, box, rot, slot, z=0.0):- """a model piece fitted into box (feet; its front facing the way rot says), standing z ft above the floor.- -> its height in feet as placed, or None if there is no model to place"""- objs = protos(piece, slot)- if not objs:- return None- if id(objs) not in BOUNDS:- bpy.context.view_layer.update()- pts = [o.matrix_world @ Vector(c) for o in objs if o.type == "MESH" for c in o.bound_box]- BOUNDS[id(objs)] = (Vector([min(p[i] for p in pts) for i in range(3)]), Vector([max(p[i] for p in pts) for i in range(3)]))- lo, hi = BOUNDS[id(objs)]- turn = themes.LIBRARY[piece][4] if piece in themes.LIBRARY else 0- mw, md = (hi.y - lo.y, hi.x - lo.x) if turn % 180 == 90 else (hi.x - lo.x, hi.y - lo.y)- x0, x1, y0, y1 = box- bw, bd = (y1 - y0, x1 - x0) if round(rot) % 180 == 90 else (x1 - x0, y1 - y0)- s = min(bw * lib.F / max(mw, 1e-6), bd * lib.F / max(md, 1e-6))- root = bpy.data.objects.new(f"{slot}_{piece}", None)- collection("furniture").objects.link(root)- copies = {}- for o in objs:- c = o.copy() # shares the mesh- copies[o] = c- collection("furniture").objects.link(c)- for o, c in copies.items():- if o.parent in copies:- c.parent = copies[o.parent]- else:- c.parent = root- c.location = o.location - Vector(((lo.x + hi.x) / 2, (lo.y + hi.y) / 2, lo.z)) # centred, standing on z- root.scale = (s, s, s)- root.rotation_euler = (0, 0, math.radians(rot + turn))- root.location = ((x0 + x1) / 2 * lib.F, (y0 + y1) / 2 * lib.F, (lib.LEVEL + z) * lib.F)- return (hi.z - lo.z) * s / lib.F---def build(it):- """one furniture item in the theme's piece for its slot: a model fitted to the item's box, or a built-in piece"""- kw = dict(it["kw"])- fn, piece = it["fn"], kw.pop("piece", None) or T["slots"].get(it["fn"])- x, y, rot = it["args"][:3] if len(it["args"]) >= 3 else (0, 0, 0)- if fn == "dining_set":- chairs = T["slots"]["dining_chair"]- if piece in themes.LIBRARY or piece == "upload":- return dining_set(*it["args"], **{k: v for k, v in kw.items() if k in ("w", "d", "seats")}, table=piece, chairs=chairs)- return dining_set(*it["args"], **kw, chairs=chairs, **({"style": piece[1:]} if piece and piece != "@contemporary" else {}))- if piece in themes.LIBRARY or piece == "upload":- h = place_piece(piece, it["box"], rot, fn, z=kw.get("z", 0.0))- if h is not None:- if fn == "nightstand":- table_lamp(x, y, rot, h)- if fn == "tv_unit" and T["tv"]: # the TV on top, against the wall behind the unit- b, r = it["box"], math.radians(rot)- back = ((b[3] - b[2]) if round(rot) % 180 == 0 else (b[1] - b[0])) / 2- tv(x - (back - 0.2) * math.sin(r), y + (back - 0.2) * math.cos(r), rot, z=h + 0.4)- return- piece = None # (an upload that failed: the built-in piece)- if fn not in CALLS: # decor of a theme no longer chosen- return- if piece and piece.startswith("@") and piece != "@contemporary" and fn in STYLED:- kw["style"] = piece[1:]- kw.pop("z", None)- CALLS[fn](*it["args"], **kw)---def hang_light(piece, cx, cy):- """a theme's ceiling light over (cx, cy): hung from the ceiling, its bottom no lower than 7.2 ft"""- w, d, h = themes.LIBRARY[piece][1:4] if piece in themes.LIBRARY else (2.0, 2.0, 2.5)- s = min(1.0, max(0.3, (H - 7.2) / h))- place_piece(piece, (cx - w * s / 2, cx + w * s / 2, cy - d * s / 2, cy + d * s / 2), 0, "light", z=H - h * s)---CALLS = dict(bathtub=bathtub, round_rug=round_rug, model=model, bed=bed, nightstand=nightstand, wardrobe=wardrobe, desk=desk, wc2=wc2, vanity=vanity,- shower2=lambda x, y, rot: shower2(x, y, rot), geyser=lambda x, y, rot: geyser(x, y, rot),- kitchen_run=kitchen_run, chimney=chimney, wall_cab2=wall_cab2, fridge2=fridge2, washer2=washer2,- puja_mandir=puja_mandir, sofa=sofa, coffee_table=coffee_table, rug=rug, tv_unit=tv_unit,- dining_set=dining_set, car=car, plant=plant, outdoor_set=outdoor_set)-CALLS.update(light=lambda x, y, rot, z=0.0: ceiling_light(x, y, 0.7), # (only the Themes page's preview row- dining_chair=lambda x, y, rot, style=None: (plain_chair if style == "plain" else chair)(x, y, rot)) # has these)-STYLED = {"bed", "wardrobe", "sofa", "coffee_table", "dining_chair"} # builders that take style= (dining_set: see build)LIGHT_ROOMS = {"living", "dining", "bedroom", "study", "dress", "passage"} # where a theme hangs its own light@@ -797,7 +467,7 @@ import bake # noqa: E402# imported models (scans, uploads) get a light map of their own per storey: their many small faces crowd the walls'# map (blurry walls) and can make its unwrap fail. Baked first: joining keeps their world place before their parents go.-model_meshes = {o.data for objs in [*lib.PROTO.values(), *UPLOADS.values()] if objs for o in objs if o.type == "MESH"}+model_meshes = {o.data for objs in [*furniture.PROTO.values(), *furniture.UPLOADS.values()] if objs for o in objs if o.type == "MESH"}groups = {}for k, objs in by_floor.items():models = [o for o in objs if o.type == "MESH" and o.data in model_meshes]diff --git a/blender/lib.py b/blender/lib.pyindex eb7f0fa..ab19445 100644--- a/blender/lib.py+++ b/blender/lib.py@@ -1,9 +1,7 @@-"""Blender building blocks used by gen.py: materials, box meshes, furniture and fixtures (units: feet).+"""Blender building blocks used by gen.py and furniture.py: materials and box meshes (units: feet).Textures and models come from ../assets (python3 fetch_assets.py)."""-import bpy, bmesh, json, math, os, sys+import bpy, bmesh, osimport numpy as np-import random-from mathutils import Vector, MatrixHERE = os.path.dirname(os.path.abspath(__file__))@@ -270,244 +268,7 @@ _d = disjoint([(0, 2, 0, 1, 0, 1), (1, 3, 0, 1, 0, 1)])assert abs(sum((b[1]-b[0])*(b[3]-b[2])*(b[5]-b[4]) for b in _d) - 3) < 1e-9, _dassert abs(sum((b[1]-b[0])*(b[3]-b[2])*(b[5]-b[4]) for b in _t) - (40 - 7)) < 1e-9, _t-COLLIDERS = [] # (x0,x1,y0,y1) feet, 2D footprint blocking the walker-FURN = [] # (name, footprint) of furniture, for the doorway clearance check-# ---------------------------------------------------------------- furniture-PROTO = {}---def load_model(name):- if name in PROTO:- return PROTO[name]- if not name.replace("_", "").isalnum():- raise ValueError(f"bad model name {name!r}")- path = os.path.join(ASSETS, "models", name, f"{name}_1k.gltf")- before = set(bpy.data.objects)- bpy.ops.import_scene.gltf(filepath=path)- new = [o for o in bpy.data.objects if o not in before]- for o in new:- for c in list(o.users_collection):- c.objects.unlink(o)- for m in {sl.material for sl in o.material_slots if sl.material and sl.material.use_nodes} if o.type == "MESH" else ():- glass_from_transmission(m)- PROTO[name] = new- return new---def glass_from_transmission(m):- """scanned glass uses transmission, which the web viewer cannot show, and the glTF exporter can drop its map and- break the whole file: make it plain see-through glass instead"""- p = m.node_tree.nodes.get("Principled BSDF")- t = p and p.inputs.get("Transmission Weight")- if t and (t.is_linked or t.default_value > 0.5):- for link in list(t.links):- m.node_tree.links.remove(link)- t.default_value = 0.0- p.inputs["Alpha"].default_value = 0.25- if hasattr(m, "surface_render_method"):- m.surface_render_method = "BLENDED"---def place(name, x, y, rot=0, z=0.0, scale=1.0, collide=True):- """x,y,z in feet; rot = degrees, 0 = model's default facing."""- objs = load_model(name)- root = bpy.data.objects.new(f"{name}_at_{x:.1f}_{y:.1f}", None)- collection("furniture").objects.link(root)- mapping = {}- for o in objs:- c = o.copy() # shares mesh data -> glTF instancing- mapping[o] = c- collection("furniture").objects.link(c)- for o, c in mapping.items():- c.parent = mapping.get(o.parent, root)- root.location = (x * F, y * F, (z + LEVEL) * F)- root.rotation_euler = (0, 0, math.radians(rot))- root.scale = (scale,) * 3- if collide:- bpy.context.view_layer.update()- add_collider([c for c in mapping.values() if c.type == "MESH"])- return root---def add_collider(objs):- pts = [o.matrix_world @ Vector(c) for o in objs for c in o.bound_box]- if not pts:- return- # furniture does not block the walker; footprints are only used for the doorway clearance check- FURN.append((objs[0].name, (min(p.x for p in pts) / F, max(p.x for p in pts) / F,- min(p.y for p in pts) / F, max(p.y for p in pts) / F)))---def group(name, boxes, x, y, rot=0, bevel=0.0, collide=True, z=0.0, seg=2):- """procedural item: boxes in local feet, front faces -Y, origin at footprint centre."""- ob = mesh_from_boxes(name, boxes, coll="furniture", bevel=bevel * F, seg=seg)- ob.location = (x * F, y * F, (z + LEVEL) * F)- ob.rotation_euler = (0, 0, math.radians(rot))- if collide:- bpy.context.view_layer.update()- add_collider([ob])- return ob---# rot convention for procedural items: 0 -> front faces -y, 90 -> +x, 180 -> +y, 270 -> -x-def bed(x, y, rot, w=5.0, l=6.6, blanket="throw", panel=None, style="contemporary"):- """bed made up with sheet, duvet, a throw and pillows. blanket: material of the throw at the foot.- panel: width of the wood panel on the wall behind the bed (default bed + 1.8 ft each side; 0 = none)- style: contemporary (upholstered, tufted headboard), low (oak platform, slab headboard), farmhouse (painted planks)"""- hw = w / 2- yb, yf = l / 2, -l / 2 # head (back) at +y local- hp = hw + 1.8 if panel is None else panel / 2- if hp > 0:- group("accent_panel", [(-hp, hp, yb - 0.03, yb, 0, WALL_H - 0.01, "wall_accent")],- x, y, rot, collide=False) # panel sits flush on the wall behind the headboard- if blanket not in MATS:- blanket = "throw"- seed = round(x * 7 + y * 13)- r = item("bed", x, y, rot)- dz = 0.0 # mattress height against the contemporary bed's- if style == "low": # oak platform on a recessed plinth, slab headboard- dz = -0.35- p_box(r, "bed_plinth", w - 0.8, l - 1.2, 0.18, "black", (0, -0.3, 0), bevel=0.01)- p_box(r, "bed_base", w + 0.5, l + 0.1, 0.52, "teak", (0, -0.05, 0.18), bevel=0.03)- p_box(r, "headboard", w + 0.5, 0.22, 3.0, "teak", (0, yb - 0.11, 0), bevel=0.03)- elif style == "farmhouse": # painted posts, plank head- and footboard, side rails- for sx in (-1, 1):- p_box(r, "bed_post", 0.32, 0.32, 4.3, "painted_light", (sx * (hw + 0.05), yb - 0.16, 0), bevel=0.04)- p_box(r, "bed_post", 0.32, 0.32, 2.7, "painted_light", (sx * (hw + 0.05), yf + 0.16, 0), bevel=0.04)- p_box(r, "bed_rail", 0.18, l - 0.6, 0.55, "painted_light", (sx * (hw - 0.02), -0.05, 0.6), bevel=0.02)- for yy, top in ((yb - 0.16, 3.9), (yf + 0.16, 2.3)):- n = max(4, round(w / 0.55))- for i in range(n): # vertical planks with a hairline gap- p_box(r, "bed_plank", w / n - 0.03, 0.12, top - 0.9, "painted_light", (-hw + w * (i + 0.5) / n, yy, 0.9), bevel=0.01)- p_box(r, "bed_cap", w + 0.6, 0.26, 0.16, "painted_light", (0, yy, top), bevel=0.03)- p_box(r, "bed_base", w - 0.1, l - 0.7, 0.45, "painted_light", (0, -0.05, 0.6), bevel=0.02)- if blanket == "throw":- blanket = "quilt"- else:- p_box(r, "bed_plinth", w - 0.5, l - 0.8, 0.3, "black", (0, -0.2, 0), bevel=0.01)- p_box(r, "bed_base", w, l - 0.35, 0.75, "teak", (0, -0.175, 0.3), bevel=0.05)- p_box(r, "headboard_back", w + 0.3, 0.15, 3.9, "teak", (0, yb - 0.1, 0), bevel=0.03)- n = max(3, round((w + 0.3) / 0.9)) # channel-tufted upholstered headboard- cw = (w + 0.3) / n- for i in range(n):- p_soft(r, "headboard", cw - 0.02, 0.4, 2.9, "velvet_head", (-(w + 0.3) / 2 + cw * (i + 0.5), yb - 0.37, 0.95),- r=0.17)- p_soft(r, "mattress", w - 0.15, l - 0.55, 0.75, "sheet", (0, -0.225, 1.05 + dz), r=0.12)- p_drape(r, "duvet", -hw + 0.08, hw - 0.08, yf + 0.1, yb - 2.1, 1.86 + dz, 0.85, "duvet", "xXy", thick=0.06, seed=seed)- p_soft(r, "duvet_fold", w - 0.1, 0.55, 0.3, "duvet", (0, yb - 2.1, 1.72 + dz), r=0.14)- p_drape(r, "throw", -hw, hw, yf + 0.3, yf + 1.7, 1.94 + dz, 1.0, blanket, "xX", thick=0.04, seed=seed)- pw, xs = (w - 0.6, (0,)) if w < 4 else (hw - 0.3, (-w / 4, w / 4))- for px in xs: # sleeping pillows leaning on the headboard- p_soft(r, "pillow", pw, 0.55, 1.35, "sheet", (px, yb - 0.95, 1.75 + dz), (-18, 0, 0), r=0.26)- if w >= 4: # cushions in front of them- for k, s in enumerate((-1, 1)):- p_soft(r, "cushion", 1.25, 0.4, 1.25, "jacquard", (s * (w / 4 - 0.1), yb - 1.45, 1.8 + dz), (-22, 0, s * 6), r=0.2)- p_soft(r, "cushion", 1.1, 0.35, 0.8, "boucle", (0, yb - 1.6, 1.8 + dz), (-15, 0, 0), r=0.16)- return done(r)---def wardrobe(x, y, rot, w=6.0, d=2.0, h=7.0, style="contemporary"):- """style: contemporary (oak, steel bar handles), flat (handleless white), classic (walnut armoire: plinth, cornice,- raised panels, brass knobs), farmhouse (painted plank doors, iron hinges and latches)"""- hw, f = w / 2, -d / 2 # f: the door face- n = max(2, round(w / 1.5))- if style == "flat":- b = [(-hw, hw, f + 0.05, d / 2, 0.25, h, "matte_white"), (-hw + 0.1, hw - 0.1, f + 0.12, d / 2, 0, 0.25, "black")]- for i in range(n):- x0 = -hw + w * i / n- b.append((x0 + 0.01, x0 + w / n - 0.01, f, f + 0.05, 0.27, h - 0.02, "matte_white"))- return group("wardrobe", b, x, y, rot, bevel=0.005)- if style == "classic":- b = [(-hw, hw, f + 0.1, d / 2, 0.5, h - 0.45, "teak"),- (-hw - 0.08, hw + 0.08, f - 0.02, d / 2, 0, 0.5, "teak"), # plinth- (-hw - 0.05, hw + 0.05, f, d / 2, h - 0.45, h - 0.25, "teak"), # cornice, stepping out- (-hw - 0.15, hw + 0.15, f - 0.12, d / 2, h - 0.25, h - 0.12, "teak"),- (-hw - 0.22, hw + 0.22, f - 0.18, d / 2, h - 0.12, h, "teak")]- for i in range(n):- x0, x1 = -hw + w * i / n + 0.03, -hw + w * (i + 1) / n - 0.03- b.append((x0, x1, f + 0.02, f + 0.1, 0.55, h - 0.5, "teak")) # door- for z0, z1 in ((0.85, 3.2), (3.55, h - 0.8)): # raised panels- b.append((x0 + 0.22, x1 - 0.22, f - 0.03, f + 0.02, z0, z1, "teak"))- kx = x1 - 0.2 if i % 2 == 0 else x0 + 0.2- b.append((kx - 0.06, kx + 0.06, f - 0.12, f - 0.03, 3.3, 3.42, "brass")) # knob- return group("wardrobe", b, x, y, rot, bevel=0.02)- if style == "farmhouse":- b = [(-hw, hw, f + 0.1, d / 2, 0.35, h - 0.3, "painted_wood"),- (-hw - 0.05, hw + 0.05, f + 0.02, d / 2, 0, 0.35, "painted_wood"),- (-hw - 0.12, hw + 0.12, f - 0.06, d / 2, h - 0.3, h, "painted_wood")]- for i in range(n):- x0, x1 = -hw + w * i / n + 0.03, -hw + w * (i + 1) / n - 0.03- k = max(2, round((x1 - x0) / 0.45))- for j in range(k): # tongue-and-groove planks- b.append((x0 + (x1 - x0) * j / k + 0.01, x0 + (x1 - x0) * (j + 1) / k - 0.01, f + 0.02, f + 0.1, 0.4, h - 0.35, "painted_wood"))- for z in (0.8, h - 1.0): # battens and iron strap hinges- b.append((x0 + 0.05, x1 - 0.05, f - 0.02, f + 0.02, z, z + 0.3, "painted_wood"))- hx0, hx1 = (x0, x0 + 0.7) if i % 2 == 0 else (x1 - 0.7, x1)- b.append((hx0, hx1, f - 0.05, f - 0.02, z + 0.1, z + 0.2, "iron"))- lx = x1 - 0.15 if i % 2 == 0 else x0 + 0.15- b.append((lx - 0.04, lx + 0.04, f - 0.14, f - 0.02, 3.2, 3.9, "iron")) # latch handle- return group("wardrobe", b, x, y, rot, bevel=0.01)- b = [(-hw, hw, -d / 2 + 0.05, d / 2, 0, h, "laminate_w")]- for i in range(n):- x0 = -hw + w * i / n- b.append((x0 + 0.03, x0 + w / n - 0.03, -d / 2, -d / 2 + 0.05, 0.3, h - 0.05, "laminate_w"))- hx = x0 + (w / n - 0.25 if i % 2 == 0 else 0.25)- b.append((hx - 0.04, hx + 0.04, -d / 2 - 0.08, -d / 2, 3.0, 4.2, "steel"))- group("wardrobe", b, x, y, rot, bevel=0.01)---def wall_cabinet(x0, x1, y0, y1):- mesh_from_boxes("wall_cabinet", [(x0, x1, y0, y1, 5.0, 7.2, "laminate_w")], coll="furniture",- bevel=0.01 * F)---def fridge(x, y, rot):- group("fridge", [(-1.15, 1.15, -1.0, 1.1, 0, 5.8, "appliance"),- (0.9, 0.98, -1.1, -1.0, 2.5, 4.5, "steel"), (0.9, 0.98, -1.1, -1.0, 4.7, 5.5, "steel"),- (-1.14, 1.14, -1.02, -1.0, 4.55, 4.6, "black")], x, y, rot, bevel=0.06)---def wc(x, y, rot):- group("wc", [(-0.6, 0.6, -1.0, 0.5, 0, 1.3, "ceramic"), (-0.7, 0.7, 0.5, 1.15, 0, 2.6, "ceramic"),- (-0.65, 0.65, -1.05, 0.5, 1.3, 1.38, "white_gloss")], x, y, rot, bevel=0.15, seg=3)---def basin(x, y, rot):- group("basin", [(-0.9, 0.9, -0.7, 0.6, 2.4, 2.85, "ceramic"), (-0.15, 0.15, -0.2, 0.6, 0, 2.4, "ceramic"),- (-0.05, 0.05, 0.25, 0.4, 2.85, 3.3, "steel"),- (-1.0, 1.0, 0.58, 0.62, 3.6, 6.0, "glass"), (-1.05, 1.05, 0.62, 0.66, 3.55, 6.05, "steel")],- x, y, rot, bevel=0.05, collide=True)---def shower(x, y):- group("shower", [(-0.04, 0.04, -0.04, 0.04, 0, 6.8, "steel"), (-0.35, 0.35, -0.35, 0.35, 6.7, 6.78, "steel")],- x, y, 0, bevel=0.01, collide=False)---def tv(x, y, rot, z=3.2):- group("tv", [(-2.0, 2.0, -0.05, 0.1, z, z + 2.3, "black"), (-1.95, 1.95, -0.07, -0.05, z + 0.05, z + 2.25, "screen")],- x, y, rot, bevel=0.01, collide=False)---def rug(x0, x1, y0, y1, m="rug"):- mesh_from_boxes("rug", [(x0, x1, y0, y1, 0, 0.04, m)], coll="furniture", bevel=0.01 * F)---def desk(x, y, rot):- group("desk", [(-1.75, 1.75, -1.0, 1.0, 2.4, 2.55, "teak"), (-1.75, -1.6, -1.0, 1.0, 0, 2.4, "teak"),- (1.6, 1.75, -1.0, 1.0, 0, 2.4, "teak"), (0.3, 1.6, -0.9, 1.0, 1.4, 2.4, "teak")],- x, y, rot, bevel=0.02)---def ceiling_light(x, y, s=0.9):- mesh_from_boxes("ceiling_light", [(x - s, x + s, y - s, y + s, WALL_H - 0.08, WALL_H, "light")],- coll="furniture")-----# ---------------------------------------------------------------- real fixtures: curved, bevelled meshes-# (items are assembled from parts under an empty; local feet, front faces -y at rot 0, back against +y)+# furniture materials (blender/furniture.py)mat("chrome", (0.92, 0.92, 0.94), 0.06, 1.0)mat("mirror", (0.96, 0.96, 0.96), 0.0, 1.0)mat("black_glass", (0.01, 0.01, 0.012), 0.04)@@ -520,419 +281,6 @@ mat("led", (1.0, 0.86, 0.66), 0.5, emit=8.0)mat("plastic_white", (0.9, 0.9, 0.88), 0.35)mat("water", (0.55, 0.7, 0.75), 0.02, alpha=0.5)mat("blue_accent", (0.1, 0.35, 0.75), 0.3)---def _box_uv(bm, tile):- """texture UVs in metres / tile, projected along each face's main axis (as mesh_from_boxes does)"""- uv = bm.loops.layers.uv.new("UVMap")- for f in bm.faces:- f.normal_update()- ax = max(range(3), key=lambda i: abs(f.normal[i]))- a, c = [(1, 2), (0, 2), (0, 1)][ax]- for lp in f.loops:- lp[uv].uv = (lp.vert.co[a] / tile, lp.vert.co[c] / tile)---def _mesh_obj(name, bm, mn, parent, loc=(0, 0, 0), rot=(0, 0, 0), smooth=40, bevel=0.0, seg=3, solid=0.0, soft=0):- """soft: subdivision levels after the bevel, for upholstery and pillows"""- if not bm.loops.layers.uv:- _box_uv(bm, MATS[mn]["tile"])- me = bpy.data.meshes.new(name)- bm.to_mesh(me)- bm.free()- me.materials.append(MATS[mn])- if smooth:- me.shade_smooth()- me.set_sharp_from_angle(angle=math.radians(smooth))- ob = bpy.data.objects.new(name, me)- collection("furniture").objects.link(ob)- ob.parent = parent- ob.location = Vector(loc) * F- ob.rotation_euler = [math.radians(a) for a in rot]- if solid:- md = ob.modifiers.new("solid", "SOLIDIFY")- md.thickness = solid * F- if bevel:- md = ob.modifiers.new("bevel", "BEVEL")- md.width, md.segments, md.limit_method = bevel * F, seg, "ANGLE"- md.harden_normals = False- if soft:- md = ob.modifiers.new("soft", "SUBSURF")- md.levels = md.render_levels = soft- return ob---def p_soft(root, name, sx, sy, sz, mn, loc=(0, 0, 0), rot=(0, 0, 0), r=0.15):- """upholstered block: a box with round edges r (feet), smoothed into a slightly puffy cushion"""- return p_box(root, name, sx, sy, sz, mn, loc, rot, bevel=min(r, sx / 2.2, sy / 2.2, sz / 2.2), seg=2, soft=2,- smooth=180)---def p_drape(root, name, x0, x1, y0, y1, top, hang, mn, sides="xX y", thick=0.07, seed=0, step=0.12):- """cloth lying on the rectangle x0..x1, y0..y1 at height top and falling over the edges named in sides- (x = -x, X = +x, y = -y, Y = +y) by hang feet, with soft folds; local feet like p_box"""- rnd = random.Random(seed)- ex = [hang if "x" in sides else 0, hang if "X" in sides else 0, hang if "y" in sides else 0, hang if "Y" in sides else 0]- us = [x0 - ex[0] + i * step for i in range(int((x1 - x0 + ex[0] + ex[1]) / step) + 1)]- vs = [y0 - ex[2] + j * step for j in range(int((y1 - y0 + ex[2] + ex[3]) / step) + 1)]- ph = [rnd.uniform(0, 6.3) for _ in range(4)]- bm = bmesh.new()- uv = bm.loops.layers.uv.new("UVMap")- grid = []- for v in vs:- row = []- for u in us:- dx, dy = max(0, x0 - u, u - x1), max(0, y0 - v, v - y1)- drop = math.hypot(dx, dy)- flare = 0.04 + 0.1 * drop # cloth stands off the edge a little more as it falls- px = min(max(u, x0 - flare), x1 + flare)- py = min(max(v, y0 - flare), y1 + flare)- k = min(drop, 0.6) / 0.6 # vertical folds: they vary along the edge, not down it- px += 0.05 * k * math.sin(v * 3.1 + ph[0]) * (1 if u > x1 else -1 if u < x0 else 0)- py += 0.05 * k * math.sin(u * 3.1 + ph[1]) * (1 if v > y1 else -1 if v < y0 else 0)- pz = top - drop + (0.02 * math.sin(u * 2.3 + ph[2]) * math.sin(v * 1.9 + ph[3]) if drop == 0 else 0)- row.append(bm.verts.new((px * F, py * F, pz * F)))- grid.append(row)- for j in range(len(vs) - 1):- for i in range(len(us) - 1):- f = bm.faces.new((grid[j][i], grid[j][i + 1], grid[j + 1][i + 1], grid[j + 1][i]))- for lp, (a, b) in zip(f.loops, ((i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1))):- lp[uv].uv = (us[a] * F / MATS[mn]["tile"], vs[b] * F / MATS[mn]["tile"])- return _mesh_obj(name, bm, mn, root, smooth=80, solid=thick, soft=1)---def p_pleats(root, name, width, h, mn, loc=(0, 0, 0), rot=(0, 0, 0), depth=0.14, pitch=0.42):- """curtain panel hanging from its top edge: width along local x (centred), pleated in y, bottom at z=0"""- n = max(2, round(width / pitch * 8))- bm = bmesh.new()- uv = bm.loops.layers.uv.new("UVMap")- cols = []- for i in range(n + 1):- x = -width / 2 + width * i / n- y = depth / 2 * math.sin(2 * math.pi * x / pitch)- cols.append([bm.verts.new((x * F, (y + (depth * 0.4 * math.sin(x * 1.3) if z == 0 else 0)) * F, z * F))- for z in (0, h)])- t = MATS[mn]["tile"]- for i in range(n):- f = bm.faces.new((cols[i][0], cols[i + 1][0], cols[i + 1][1], cols[i][1]))- for lp, (a, b) in zip(f.loops, ((i, 0), (i + 1, 0), (i + 1, 1), (i, 1))):- lp[uv].uv = ((-width / 2 + width * a / n) * 1.3 * F / t, b * h * F / t)- return _mesh_obj(name, bm, mn, root, loc, rot, smooth=80, solid=0.03)---def _bm_xform(bm, scale=(1, 1, 1), lift=0.0):- for v in bm.verts:- v.co = Vector((v.co.x * scale[0], v.co.y * scale[1], v.co.z * scale[2] + lift))- return bm---def p_cyl(root, name, r1, r2, h, mn, loc=(0, 0, 0), rot=(0, 0, 0), sx=1.0, sy=1.0, segs=48, **kw):- """cone/cylinder, base at local z=0 before rotation; sx/sy squash the cross-section."""- bm = bmesh.new()- bmesh.ops.create_cone(bm, cap_ends=True, cap_tris=False, segments=segs, radius1=r1 * F, radius2=r2 * F,- depth=h * F)- return _mesh_obj(name, _bm_xform(bm, (sx, sy, 1), h * F / 2), mn, root, loc, rot, **kw)---def p_box(root, name, sx, sy, sz, mn, loc=(0, 0, 0), rot=(0, 0, 0), bevel=0.02, seg=3, **kw):- """box centred on x/y, base at z=0 (feet)."""- bm = bmesh.new()- bmesh.ops.create_cube(bm, size=1.0)- kw.setdefault("smooth", 30)- return _mesh_obj(name, _bm_xform(bm, (sx * F, sy * F, sz * F), sz * F / 2), mn, root, loc, rot,- bevel=bevel, seg=seg, **kw)---def p_frustum(root, name, w0, d0, w1, d1, h, mn, loc=(0, 0, 0), **kw):- """tapered box: bottom w0 x d0, top w1 x d1 (feet), base at z=0."""- bm = bmesh.new()- bmesh.ops.create_cube(bm, size=1.0)- for v in bm.verts:- top = v.co.z > 0- v.co = Vector((v.co.x * (w1 if top else w0) * F, v.co.y * (d1 if top else d0) * F, (v.co.z + 0.5) * h * F))- return _mesh_obj(name, bm, mn, root, loc, smooth=0, **kw)---def p_bowl(root, name, r, depth, mn, loc=(0, 0, 0), sy=1.0, wall=0.04):- """open bowl (lower half sphere), rim at local z=0."""- bm = bmesh.new()- bmesh.ops.create_uvsphere(bm, u_segments=48, v_segments=24, radius=r * F)- bmesh.ops.delete(bm, geom=[v for v in bm.verts if v.co.z > 1e-5], context="VERTS")- _bm_xform(bm, (1, sy, depth / r))- return _mesh_obj(name, bm, mn, root, loc, smooth=80, solid=wall)---def p_torus(root, name, R, r, mn, loc=(0, 0, 0), rot=(0, 0, 0), sy=1.0, u=48, v=16):- bm = bmesh.new()- rings = []- for i in range(u):- a = 2 * math.pi * i / u- ring = []- for j in range(v):- b = 2 * math.pi * j / v- d = R + r * math.cos(b)- ring.append(bm.verts.new((d * math.cos(a) * F, d * math.sin(a) * sy * F, r * math.sin(b) * F)))- rings.append(ring)- for i in range(u):- for j in range(v):- a, b = rings[i], rings[(i + 1) % u]- bm.faces.new((a[j], b[j], b[(j + 1) % v], a[(j + 1) % v]))- return _mesh_obj(name, bm, mn, root, loc, rot, smooth=80)---def p_tube(root, name, pts, radius, mn):- """swept round tube through local points (feet); used for taps, rails and hoses."""- cu = bpy.data.curves.new(name, "CURVE")- cu.dimensions = "3D"- cu.bevel_depth, cu.bevel_resolution, cu.use_fill_caps = radius * F, 4, True- sp = cu.splines.new("NURBS")- sp.points.add(len(pts) - 1)- for p, c in zip(sp.points, pts):- p.co = (c[0] * F, c[1] * F, c[2] * F, 1)- sp.use_endpoint_u, sp.order_u = True, min(4, len(pts))- tmp = bpy.data.objects.new(name + "_crv", cu)- scene.collection.objects.link(tmp)- bpy.context.view_layer.update()- me = bpy.data.meshes.new_from_object(tmp.evaluated_get(bpy.context.evaluated_depsgraph_get()))- bpy.data.objects.remove(tmp)- bpy.data.curves.remove(cu)- me.materials.clear()- me.materials.append(MATS[mn])- me.shade_smooth()- ob = bpy.data.objects.new(name, me)- collection("furniture").objects.link(ob)- ob.parent = root- return ob---def item(name, x, y, rot=0, z=0.0):- root = bpy.data.objects.new(name, None)- collection("furniture").objects.link(root)- root.location = (x * F, y * F, (z + LEVEL) * F)- root.rotation_euler = (0, 0, math.radians(rot))- return root---def done(root, collide=True):- if collide:- bpy.context.view_layer.update()- add_collider([c for c in root.children_recursive if c.type == "MESH"])- return root---def arc(c, r, a0, a1, n=10, plane="yz"):- """points on a circular arc (degrees) around centre c in the given plane."""- pts = []- for i in range(n + 1):- a = math.radians(a0 + (a1 - a0) * i / n)- u, v = r * math.cos(a), r * math.sin(a)- pts.append((c[0], c[1] + u, c[2] + v) if plane == "yz" else (c[0] + u, c[1], c[2] + v))- return pts---def wc2(x, y, rot):- """floor-mounted one-piece EWC with closed soft-close lid, cistern, chrome flush and health faucet."""- r = item("wc", x, y, rot)- p_cyl(r, "wc_body", 0.42, 0.6, 1.22, "ceramic", (0, -0.2, 0), sy=1.38, bevel=0.06, seg=4)- p_box(r, "wc_skirt", 1.05, 0.7, 1.22, "ceramic", (0, 0.5, 0), bevel=0.12, seg=4)- p_cyl(r, "wc_seat", 0.62, 0.62, 0.07, "white_gloss", (0, -0.2, 1.22), sy=1.38, bevel=0.03)- p_cyl(r, "wc_lid", 0.6, 0.6, 0.07, "white_gloss", (0, -0.17, 1.29), sy=1.36, bevel=0.03)- p_box(r, "wc_cistern", 1.32, 0.55, 1.15, "ceramic", (0, 0.78, 1.25), bevel=0.14, seg=5)- p_box(r, "wc_cistern_lid", 1.38, 0.6, 0.08, "ceramic", (0, 0.78, 2.4), bevel=0.03)- p_cyl(r, "wc_flush", 0.11, 0.11, 0.03, "chrome", (0, 0.78, 2.48))- # health faucet on the side wall-ish bracket with a coiled hose- p_box(r, "hf_holder", 0.12, 0.08, 0.2, "chrome", (0.95, 0.98, 2.0), bevel=0.02)- p_cyl(r, "hf_head", 0.05, 0.06, 0.45, "chrome", (0.95, 0.9, 1.95), rot=(15, 0, 0))- p_tube(r, "hf_hose", [(0.95, 0.95, 1.95), (1.0, 0.9, 1.2), (0.9, 0.85, 0.6), (0.85, 0.95, 1.6),- (0.9, 1.0, 2.3)], 0.025, "chrome")- return done(r)---def vanity(x, y, rot, w=2.6):- """wall-hung vanity: laminate cabinet, quartz top, vessel basin, tall mixer, backlit mirror."""- r = item("vanity", x, y, rot)- d = 1.6- p_box(r, "vanity_cab", w, d, 1.5, "laminate_w", (0, 0.05, 1.05), bevel=0.02)- p_box(r, "vanity_groove", w - 0.1, 0.02, 0.04, "black", (0, 0.05 - d / 2 - 0.005, 2.25), bevel=0)- p_box(r, "vanity_top", w + 0.1, d + 0.1, 0.12, "quartz", (0, 0.05, 2.55), bevel=0.02)- p_bowl(r, "basin_bowl", 0.62, 0.5, "ceramic", (0, -0.1, 3.15), sy=0.82)- p_cyl(r, "basin_drain", 0.06, 0.06, 0.01, "chrome", (0, -0.1, 2.68))- p_cyl(r, "tap_base", 0.07, 0.07, 0.15, "chrome", (0, 0.55, 2.67))- p_tube(r, "tap_spout", [(0, 0.55, 2.7), (0, 0.55, 3.55)] + arc((0, 0.35, 3.55), 0.2, 0, 160, 8)[1:] +- [(0, 0.2, 3.35)], 0.045, "chrome")- p_box(r, "tap_lever", 0.06, 0.25, 0.05, "chrome", (0.1, 0.5, 3.2), rot=(-20, 0, 0), bevel=0.01)- p_box(r, "mirror_glow", w * 0.9 + 0.1, 0.03, 3.1, "led", (0, d / 2 + 0.02, 3.95), bevel=0)- p_box(r, "mirror", w * 0.9, 0.06, 3.0, "mirror", (0, d / 2 - 0.02, 4.0), bevel=0.05)- p_box(r, "soap_dish", 0.35, 0.25, 0.05, "black_glass", (w / 2 - 0.35, 0.4, 2.67), bevel=0.02)- return done(r)---def shower2(x, y, rot, glass=None):- """rain shower on a wall arm + concealed mixer + floor drain; glass = (x0,x1,y0,y1) world screen."""- r = item("shower", x, y, rot)- p_tube(r, "shower_arm", [(0, 0.0, 7.4), (0, -0.4, 7.4), (0, -0.9, 7.3), (0, -1.1, 7.15)], 0.05, "chrome")- p_box(r, "shower_head", 0.85, 0.85, 0.05, "chrome", (0, -1.1, 7.05), bevel=0.02)- p_cyl(r, "mixer_plate", 0.28, 0.28, 0.04, "chrome", (0, -0.09, 3.8), rot=(90, 0, 0))- p_box(r, "mixer_lever", 0.06, 0.12, 0.4, "chrome", (0, -0.16, 3.75), bevel=0.02)- p_box(r, "floor_drain", 0.4, 0.4, 0.015, "steel", (0, -1.1, 0.05), bevel=0)- p_cyl(r, "hand_shower", 0.05, 0.07, 0.6, "chrome", (0.5, -0.15, 3.9), rot=(0, 0, 0))- p_tube(r, "hand_hose", [(0.5, -0.15, 3.9), (0.55, -0.25, 2.8), (0.4, -0.2, 2.2), (0.3, -0.1, 3.6)], 0.025,- "chrome")- p_box(r, "shower_wall", 2.6, 0.04, 7.6, "stone_wall", (0, -0.07, 0), bevel=0) # in front of the 0.03 ft wall tiles- p_box(r, "shower_tray", 2.5, 2.5, 0.05, "pebble_floor", (0, -1.27, 0), bevel=0.005)- done(r, collide=False)- if glass:- x0, x1, y0, y1 = glass- mesh_from_boxes("shower_glass", [(x0, x1, y0, y1, 0.05, 6.8, "glass")], coll="furniture")- mesh_from_boxes("shower_glass_trim", [(x0, x1, y0, y1, 6.8, 6.85, "chrome"),- (x0, x1, y0, y1, 0.0, 0.05, "chrome")], coll="furniture")- return r---def geyser(x, y, rot, z=6.4):- r = item("geyser", x, y, rot, z=z)- p_cyl(r, "geyser_tank", 0.5, 0.5, 1.7, "plastic_white", (-0.85, -0.55, 0.5), rot=(0, 90, 0), bevel=0.12,- seg=4)- p_box(r, "geyser_panel", 0.5, 0.05, 0.25, "blue_accent", (0, -1.04, 0.35), bevel=0.02)- return done(r, collide=False)---def _doors(r, length, depth, z0, z1, handle_at, front_y, n=None):- """shaker-ish door fronts with bar handles along local x; front_y = local y of the cabinet face."""- n = n or max(1, round(length / 1.5))- dw = length / n- for i in range(n):- cx = -length / 2 + dw * (i + 0.5)- p_box(r, "door", dw - 0.04, 0.06, z1 - z0 - 0.04, "laminate_w", (cx, front_y - 0.03, z0 + 0.02), bevel=0.01)- p_box(r, "door_panel", dw - 0.4, 0.02, z1 - z0 - 0.5, "laminate_w", (cx, front_y - 0.065, z0 + 0.25),- bevel=0.005)- hx = cx + (dw / 2 - 0.25 if i % 2 == 0 else -dw / 2 + 0.25)- p_box(r, "handle", 0.05, 0.08, 0.7, "chrome", (hx, front_y - 0.1, handle_at - 0.35), bevel=0.015)---def kitchen_run(x0, x1, y0, y1, front, sink=None, hob=None, doors=True):- """base cabinets + granite top in world feet; front = side the cook stands ('-y','+y','+x','-x').- sink / hob = position along the run (world x for runs along x, world y for runs along y)."""- along_x = front in ("-y", "+y")- L, Dp = (x1 - x0, y1 - y0) if along_x else (y1 - y0, x1 - x0)- rot = {"-y": 0, "+x": 90, "+y": 180, "-x": 270}[front]- r = item("kitchen_run", (x0 + x1) / 2, (y0 + y1) / 2, rot)- flip = 1 if front in ("-y", "+x") else -1 # local x vs world axis direction after rotation- c = (x0 + x1) / 2 if along_x else (y0 + y1) / 2-- def local(u): # world coordinate along the run -> local x- return (u - c) * flip-- p_box(r, "plinth", L, Dp - 0.3, 0.33, "black", (0, 0.15, 0), bevel=0)- p_box(r, "carcass", L, Dp - 0.05, 2.42, "laminate_w", (0, 0.025, 0.33), bevel=0.005)- if doors:- _doors(r, L, Dp, 0.36, 2.72, 2.55, -Dp / 2 + 0.05)- # granite top with a hole for the sink bowl- top = [(-L / 2, L / 2, -Dp / 2 - 0.08, Dp / 2, 2.75, 2.9)]- holes = []- if sink is not None:- sx = local(sink)- holes.append((sx - 0.9, sx + 0.9, -0.55, 0.55, 2.0, 3.0))- top = subtract_all(top, holes)- for i, b in enumerate(top):- p_box(r, "counter_top", b[1] - b[0], b[3] - b[2], 0.15, "stone_top",- ((b[0] + b[1]) / 2, (b[2] + b[3]) / 2, 2.75), bevel=0.01)- p_box(r, "backsplash", L, 0.04, 2.0, "walltile", (0, Dp / 2 - 0.02, 2.9), bevel=0)- if sink is not None:- sx = local(sink)- for bx, by, w_, d_ in ((sx, -0.55, 1.8, 0.04), (sx, 0.55, 1.8, 0.04), (sx - 0.9, 0, 0.04, 1.1),- (sx + 0.9, 0, 0.04, 1.1)):- p_box(r, "sink_wall", w_, d_, 0.7, "steel", (bx, by, 2.2), bevel=0)- p_box(r, "sink_floor", 1.8, 1.1, 0.03, "steel", (sx, 0, 2.2), bevel=0)- p_cyl(r, "sink_drain", 0.12, 0.12, 0.01, "cast_iron", (sx, 0, 2.235))- p_box(r, "sink_rim", 1.95, 1.25, 0.02, "steel", (sx, 0, 2.9), bevel=0)- p_box(r, "sink_rim_cut", 1.8, 1.1, 0.03, "steel", (sx, 0, 2.9), bevel=0)- p_cyl(r, "faucet_base", 0.08, 0.08, 0.1, "chrome", (sx, 0.75, 2.9))- p_tube(r, "faucet", [(sx, 0.75, 2.95), (sx, 0.75, 3.9)] + arc((sx, 0.45, 3.9), 0.3, 0, 170, 10)[1:] +- [(sx, 0.15, 3.6)], 0.05, "chrome")- if hob is not None:- hx = local(hob)- p_box(r, "hob_glass", 2.4, 1.5, 0.08, "black_glass", (hx, 0.05, 2.9), bevel=0.02)- for k, bx in enumerate((-0.75, 0.0, 0.75)):- rb = 0.22 if k != 1 else 0.28- p_cyl(r, "burner", rb, rb * 0.9, 0.07, "brass", (hx + bx, 0.15, 2.98))- p_cyl(r, "burner_cap", rb * 0.6, rb * 0.5, 0.05, "cast_iron", (hx + bx, 0.15, 3.05))- for a in (0, 45, 90, 135):- p_box(r, "pan_support", 0.06, rb * 2 + 0.25, 0.08, "cast_iron", (hx + bx, 0.15, 3.0),- rot=(0, 0, a), bevel=0.01)- p_cyl(r, "knob", 0.07, 0.07, 0.07, "chrome", (hx + bx, -0.6, 2.98))- return done(r)---def wall_cab2(x0, x1, y0, y1, front, z0=4.9, z1=7.2):- along_x = front in ("-y", "+y")- L, Dp = (x1 - x0, y1 - y0) if along_x else (y1 - y0, x1 - x0)- rot = {"-y": 0, "+x": 90, "+y": 180, "-x": 270}[front]- r = item("wall_cabinet", (x0 + x1) / 2, (y0 + y1) / 2, rot)- p_box(r, "wall_carcass", L, Dp - 0.05, z1 - z0, "laminate_w", (0, 0.025, z0), bevel=0.005)- _doors(r, L, Dp, z0, z1, z0 + 0.55, -Dp / 2 + 0.05)- p_box(r, "under_led", L - 0.2, 0.05, 0.02, "led", (0, -Dp / 2 + 0.3, z0 - 0.02), bevel=0)- return done(r, collide=False)---def chimney(x, y, rot):- """kitchen hood: steel pyramid + black glass skirt + flue box up to the ceiling."""- r = item("chimney", x, y, rot)- p_frustum(r, "hood", 2.5, 1.5, 0.95, 0.85, 0.9, "steel", (0, -0.6, 5.05), bevel=0.01)- p_box(r, "hood_skirt", 2.5, 1.5, 0.2, "black_glass", (0, -0.6, 4.85), bevel=0.01)- p_box(r, "flue", 0.9, 0.8, WALL_H - 5.9, "steel", (0, -0.55, 5.9), bevel=0.01)- return done(r, collide=False)---def fridge2(x, y, rot):- """brushed steel French-door fridge with a freezer drawer"""- r = item("fridge", x, y, rot)- p_box(r, "fridge_body", 2.3, 2.1, 5.8, "steel_brushed", (0, 0.05, 0), bevel=0.08, seg=3)- p_box(r, "fridge_gap", 2.2, 0.05, 5.6, "black", (0, -1.0, 0.1), bevel=0)- for s in (-1, 1):- p_box(r, "fridge_door", 1.12, 0.14, 3.6, "steel_brushed", (s * 0.575, -1.05, 2.15), bevel=0.05, seg=3)- p_tube(r, "fridge_handle", [(s * 0.1, -1.25, 2.9), (s * 0.1, -1.25, 5.0)], 0.04, "chrome")- p_box(r, "freezer_drawer", 2.27, 0.14, 1.95, "steel_brushed", (0, -1.05, 0.15), bevel=0.05, seg=3)- p_tube(r, "freezer_handle", [(-0.7, -1.25, 1.85), (0.7, -1.25, 1.85)], 0.04, "chrome")- p_box(r, "fridge_display", 0.45, 0.02, 0.6, "black_glass", (-0.55, -1.13, 4.2), bevel=0.01)- p_box(r, "fridge_led", 0.25, 0.01, 0.06, "led", (-0.55, -1.145, 4.55), bevel=0)- return done(r)---def washer2(x, y, rot):- """front-loading washing machine: porthole door with a steel drum behind tinted glass, detergent drawer, dial"""- r = item("washing_machine", x, y, rot)- p_box(r, "wm_body", 2.0, 1.9, 2.75, "white_gloss", (0, 0, 0.05), bevel=0.1, seg=3)- for sx in (-1, 1):- for sy in (-1, 1):- p_cyl(r, "wm_foot", 0.08, 0.08, 0.06, "black", (sx * 0.8, sy * 0.75, 0), segs=12)- p_box(r, "wm_seam", 1.98, 0.02, 0.02, "plastic_white", (0, -0.955, 2.25), bevel=0)- p_cyl(r, "wm_drum", 0.5, 0.5, 0.01, "steel_brushed", (0, -0.952, 1.2), rot=(90, 0, 0))- p_cyl(r, "wm_drum_hub", 0.18, 0.18, 0.012, "cast_iron", (0, -0.958, 1.2), rot=(90, 0, 0), segs=24)- p_torus(r, "wm_bezel", 0.6, 0.08, "plastic_white", (0, -0.97, 1.2), rot=(90, 0, 0))- p_torus(r, "wm_ring", 0.52, 0.05, "chrome", (0, -1.03, 1.2), rot=(90, 0, 0))- p_cyl(r, "wm_glass", 0.5, 0.42, 0.1, "glass", (0, -0.97, 1.2), rot=(90, 0, 0))- p_box(r, "wm_handle", 0.1, 0.06, 0.35, "chrome", (0.52, -1.07, 1.05), bevel=0.02)- p_box(r, "wm_drawer", 0.6, 0.04, 0.32, "plastic_white", (-0.6, -0.97, 2.32), bevel=0.02)- p_box(r, "wm_drawer_grip", 0.3, 0.02, 0.05, "black", (-0.6, -0.995, 2.45), bevel=0)- p_box(r, "wm_display", 0.45, 0.02, 0.22, "black_glass", (0.1, -0.965, 2.37), bevel=0.01)- p_box(r, "wm_digits", 0.2, 0.01, 0.06, "led", (0.1, -0.978, 2.45), bevel=0)- p_cyl(r, "wm_dial", 0.15, 0.13, 0.08, "chrome", (0.65, -0.95, 2.48), rot=(90, 0, 0), segs=24)- return done(r)---def puja_mandir(x, y, rot, w=3.6):- """wooden mandir unit: storage base, back panel, pillars and a stepped shikhara canopy."""- r = item("mandir", x, y, rot)- p_box(r, "mandir_base", w, 1.5, 2.4, "teak", (0, 0, 0), bevel=0.03)- _doors(r, w, 1.5, 0.15, 2.3, 1.9, -0.75, n=2)- p_box(r, "mandir_back", w, 0.15, 4.2, "teak", (0, 0.68, 2.4), bevel=0.02)- p_box(r, "mandir_led", w - 0.4, 0.03, 3.6, "led", (0, 0.58, 2.6), bevel=0)- for s in (-1, 1):- p_cyl(r, "mandir_pillar", 0.1, 0.1, 3.6, "brass", (s * (w / 2 - 0.2), -0.55, 2.4))- p_box(r, "mandir_lintel", w + 0.1, 1.5, 0.25, "teak", (0, 0, 6.0), bevel=0.03)- for k, f in enumerate((0.88, 0.72, 0.55)):- p_box(r, "mandir_step", w * f, 1.2, 0.25, "teak", (0, 0.05, 6.25 + 0.25 * k), bevel=0.02)- p_cyl(r, "shikhara", w * 0.26, 0.08, 1.4, "brass", (0, 0.05, 7.0), segs=8)- return done(r)-+mat("foliage", rough=0.9, tex="leafy_grass", tile=0.6, tint=(0.35, 0.55, 0.25))+mat("bark", (0.25, 0.18, 0.12), 0.9)+mat("rug_round", rough=1.0, tex="caban", tile=0.9, tint=(0.86, 0.83, 0.78))diff --git a/furnish.py b/furnish.pyindex 821a069..b9b7b77 100644--- a/furnish.py+++ b/furnish.py@@ -1,8 +1,8 @@"""Rule-based furniture layout (no ML): for each detected room zone pick items for its type and place them againstreal walls, away from doors, windows (for tall items), each other, and other rooms.-Pure python so it can be tested without Blender. Units: feet. Output items are calls into blender/lib.py.+Pure python so it can be tested without Blender. Units: feet. Output items are calls into blender/furniture.py.-Rotation (lib.py convention): 0 = item front faces -y (back to a wall on +y), 90 = +x, 180 = +y, 270 = -x.+Rotation (furniture.py convention): 0 = item front faces -y (back to a wall on +y), 90 = +x, 180 = +y, 270 = -x."""import math