Single place to convert your 2D floor plans to 3d model (no LLM)
Python
"""Rule-based furniture layout (no ML): for each detected room zone pick items for its type and place them against
real 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/furniture.py.
Rotation (furniture.py convention): 0 = item front faces -y (back to a wall on +y), 90 = +x, 180 = +y, 270 = -x.
"""
import math
import themes
SIDES = { # side of the room rect -> (rot of an item backed onto it, axis along the wall)
"N": 0, "S": 180, "E": 270, "W": 90,
}
INDOOR = {"bedroom", "toilet", "kitchen", "living", "dining", "puja", "study", "store", "passage", "room", "wash", "dress"}
def area(r):
return (r[1] - r[0]) * (r[3] - r[2])
def overlap(a, b, m=0.0):
return a[0] < b[1] - m and b[0] < a[1] - m and a[2] < b[3] - m and b[2] < a[3] - m
def inside(a, b, m=0.05):
return a[0] >= b[0] - m and a[1] <= b[1] + m and a[2] >= b[2] - m and a[3] <= b[3] + m
def subtract_iv(ivs, a, b):
return [x for p, q in ivs for x in ((p, min(q, a)), (max(p, b), q)) if x[1] - x[0] > 0.05]
def union_iv(ivs):
out = []
for a, b in sorted(ivs):
if out and a <= out[-1][1] + 0.3:
out[-1] = (out[-1][0], max(out[-1][1], b))
else:
out.append((a, b))
return out
def max_rect(rects, exclude=None, g=0.25):
"""largest axis-aligned rectangle inside the union of rects (minus `exclude`): rooms come as row slices"""
x0, y0 = min(r[0] for r in rects), min(r[2] for r in rects)
nx, ny = int(round((max(r[1] for r in rects) - x0) / g)), int(round((max(r[3] for r in rects) - y0) / g))
grid = [[False] * nx for _ in range(ny)]
for r in rects:
for j in range(int(round((r[2] - y0) / g)), int(round((r[3] - y0) / g))):
for i in range(int(round((r[0] - x0) / g)), int(round((r[1] - x0) / g))):
grid[j][i] = True
if exclude:
for j in range(ny):
for i in range(nx):
x, y = x0 + (i + 0.5) * g, y0 + (j + 0.5) * g
if exclude[0] < x < exclude[1] and exclude[2] < y < exclude[3]:
grid[j][i] = False
best, hts = (0, None), [0] * nx
for j in range(ny):
hts = [h + 1 if grid[j][i] else 0 for i, h in enumerate(hts)]
st = []
for i in range(nx + 1): # largest rectangle in a histogram
h = hts[i] if i < nx else 0
while st and hts[st[-1]] >= h:
top = st.pop()
left = st[-1] + 1 if st else 0
a = hts[top] * (i - left)
if a > best[0]:
best = (a, (x0 + left * g, x0 + i * g, y0 + (j + 1 - hts[top]) * g, y0 + (j + 1) * g))
st.append(i)
return best[1]
class Room:
def __init__(self, zone, floor, theme=None):
self.zone, self.type, self.floor = zone, zone["type"], floor
self.T = theme or themes.load(None)
self.rect = max_rect(zone["rects"]) or max(zone["rects"], key=area)
x0, x1, y0, y1 = self.rect
self.w, self.d = x1 - x0, y1 - y0
self.items, self.keepout = [], []
walls = [w for w in floor["walls"]]
ops = floor["openings"]
self.free = {} # side -> {"low": intervals, "tall": intervals} (positions along the side)
for side in "NSEW":
along = (x0, x1) if side in "NS" else (y0, y1)
face = {"N": y1, "S": y0, "E": x1, "W": x0}[side]
# wall coverage along this side (open-plan boundaries have no wall)
cov = [] # walls, and windows (a counter or bed can stand under a sill; tall items are cut below)
shut = [] # anything on the edge: walls of any height, windows, doors
for w, full in [(w, w[4] == 0) for w in walls] + [(list(o["rect"]) + [0], o["type"] == "window") for o in ops]:
if side in "NS":
near_ = (w[2] - 0.6 <= face <= w[3] + 0.6) and (w[2] >= face - 0.25 if side == "N" else w[3] <= face + 0.25)
iv = (max(w[0], along[0]), min(w[1], along[1]))
else:
near_ = (w[0] - 0.6 <= face <= w[1] + 0.6) and (w[0] >= face - 0.25 if side == "E" else w[1] <= face + 0.25)
iv = (max(w[2], along[0]), min(w[3], along[1]))
if near_:
shut.append(iv)
if full:
cov.append(iv)
cov = union_iv([c for c in cov if c[1] - c[0] > 0.2])
# a door-wide gap in a wall is a doorway all the same: keep it clear (a wider one, or a whole open side,
# is open plan, not a way through)
p = along[0]
for a, b in union_iv([c for c in shut if c[1] - c[0] > 0.2]) + [(along[1], along[1])]:
if 2.0 <= a - p <= min(5.0, 0.9 * (along[1] - along[0])):
self.keepout.append({"N": (p, a, y1 - 3.0, y1), "S": (p, a, y0, y0 + 3.0),
"E": (x1 - 3.0, x1, p, a), "W": (x0, x0 + 3.0, p, a)}[side])
p = max(p, b)
low, tall = cov, cov
for o in ops:
r = o["rect"]
if side in "NS":
hit = r[2] - 0.8 <= face <= r[3] + 0.8 and r[1] > along[0] and r[0] < along[1]
a, b = r[0], r[1]
else:
hit = r[0] - 0.8 <= face <= r[1] + 0.8 and r[3] > along[0] and r[2] < along[1]
a, b = r[2], r[3]
if not hit:
continue
if o["type"] == "window":
tall = subtract_iv(tall, a - 0.2, b + 0.2)
else:
low = subtract_iv(low, a - 0.4, b + 0.4)
tall = subtract_iv(tall, a - 0.4, b + 0.4)
self.free[side] = {"low": low, "tall": tall}
# fridges and wardrobes must not stand in front of a window on a neighbouring wall either
self.win_front = []
for o in ops:
if o["type"] == "window":
r = o["rect"]
self.win_front.append((r[0] - 0.1, r[1] + 0.1, r[2] - 2.0, r[3] + 2.0) if o["axis"] == "x" else
(r[0] - 2.0, r[1] + 2.0, r[2] - 0.1, r[3] + 0.1))
# stair flights and their landing strip are walkways
for st in floor.get("stairs", []):
r = st["rect"]
self.keepout.append((r[0] - 0.1, r[1] + 0.1, r[2] - 0.1, r[3] + 0.1))
if floor.get("stairs"):
xs = [v for st in floor["stairs"] for v in st["rect"][:2]]
ys = [v for st in floor["stairs"] for v in st["rect"][2:]]
ax = floor["stairs"][0]["axis"]
pad = 4.0
self.keepout.append((min(xs) - (pad if ax == "x" else 0), max(xs) + (pad if ax == "x" else 0),
min(ys) - (pad if ax == "y" else 0), max(ys) + (pad if ax == "y" else 0)))
# door swing / walkway keep-out boxes (both sides of every door touching this room)
for o in ops:
if o["type"] == "window":
continue
r = o["rect"]
box = (r[0] - 0.2, r[1] + 0.2, r[2] - 3.0, r[3] + 3.0) if o["axis"] == "x" else \
(r[0] - 3.0, r[1] + 3.0, r[2] - 0.2, r[3] + 0.2)
if overlap(box, self.rect):
self.keepout.append(box)
# doors of this room: on its edge, with their middle inside the room's span (not a neighbour's door at a corner)
x0, x1, y0, y1 = self.rect
self.doors = [o for o in ops if o["type"] != "window" and overlap(
(o["rect"][0] - 0.8, o["rect"][1] + 0.8, o["rect"][2] - 0.8, o["rect"][3] + 0.8), self.rect)
and (x0 < (o["rect"][0] + o["rect"][1]) / 2 < x1 if o["axis"] == "x" else y0 < (o["rect"][2] + o["rect"][3]) / 2 < y1)]
# ---- placement against a side
def spot(self, side, w, d, tall=False, align="center", gap=0.0):
"""best position (center) for an item of width w / depth d with its back on `side` -> (x, y, rot, box)"""
x0, x1, y0, y1 = self.rect
best = None
for a, b in self.free[side]["tall" if tall else "low"]:
if b - a < w + 2 * gap:
continue
if align == "start":
c = a + gap + w / 2
elif align == "end":
c = b - gap - w / 2
else:
c = (a + b) / 2
for c in (c, a + gap + w / 2, b - gap - w / 2, (a + b) / 2):
if side == "N":
box, xy = (c - w / 2, c + w / 2, y1 - d, y1), (c, y1 - d / 2)
elif side == "S":
box, xy = (c - w / 2, c + w / 2, y0, y0 + d), (c, y0 + d / 2)
elif side == "E":
box, xy = (x1 - d, x1, c - w / 2, c + w / 2), (x1 - d / 2, c)
else:
box, xy = (x0, x0 + d, c - w / 2, c + w / 2), (x0 + d / 2, c)
if self.fits(box) and not (tall and 2 <= d <= 3 and any(overlap(box, k, 0.02) for k in self.win_front)):
score = b - a
if not best or score > best[0]:
best = (score, (*xy, SIDES[side], box))
break
return best[1] if best else None
def fits(self, box, ignore_keepout=False):
if not inside(box, self.rect):
return False
if any(overlap(box, it["box"], 0.02) for it in self.items if it.get("solid", True)):
return False
if not ignore_keepout and any(overlap(box, k, 0.02) for k in self.keepout):
return False
return True
def add(self, fn, box, *args, solid=True, **kw):
self.items.append(dict(fn=fn, args=list(args), kw=kw, box=box, solid=solid, room=self.zone.get("label") or self.type))
def sizes(self, slot, defaults):
"""footprints (w, d, kw) to try for a slot: a theme's model at its own size, a little smaller if need be, then
the built-in piece in the theme's colours (kw piece) where the model does not fit; without a model the defaults"""
m = themes.size(self.T, slot)
if not m:
return [(w, d, {}) for w, d in defaults]
return [(m[0] * k, m[1] * k, {}) for k in (1.0, 0.9, 0.8)] + [(w, d, dict(piece="@contemporary")) for w, d in defaults]
def height(self, slot, default):
m = themes.size(self.T, slot)
return m[2] if m else default
def hang(self, slot, side, c, z, ref=None):
"""a theme's wall piece (art, mirror) on `side`, centred along it at c, its bottom z ft above the floor;
not solid, so it never blocks anything. ref: the item it hangs above (for the walkthrough's furniture tool)"""
m = themes.size(self.T, slot)
if not m or z + m[2] > self.floor.get("floor_height", 10.0) - 0.4:
return
face = {"N": self.rect[3], "S": self.rect[2], "E": self.rect[1], "W": self.rect[0]}[side]
xy = (c, face - m[1] / 2) if side == "N" else (c, face + m[1] / 2) if side == "S" else \
(face - m[1] / 2, c) if side == "E" else (face + m[1] / 2, c)
self.add(slot, rect_at(xy, m[0], m[1], side), *xy, SIDES[side], z=round(z, 2), solid=False)
def sides_by(self, key, tall=False):
"""sides sorted by their longest free run (desc)"""
lens = {s: max((b - a for a, b in self.free[s]["tall" if tall else "low"]), default=0) for s in "NSEW"}
return sorted("NSEW", key=lambda s: -lens[s] if key == "long" else lens[s])
def door_side(self):
"""side of the rect nearest to the (first) door"""
if not self.doors:
return None
r = self.doors[0]["rect"]
cx, cy = (r[0] + r[1]) / 2, (r[2] + r[3]) / 2
x0, x1, y0, y1 = self.rect
return min({"W": abs(cx - x0), "E": abs(cx - x1), "S": abs(cy - y0), "N": abs(cy - y1)}.items(),
key=lambda kv: kv[1])[0]
OPP = {"N": "S", "S": "N", "E": "W", "W": "E"}
def front_offset(side, xy, dist):
"""point `dist` feet in front of an item at xy backed onto `side`"""
dx, dy = {"N": (0, -1), "S": (0, 1), "E": (-1, 0), "W": (1, 0)}[side]
return xy[0] + dx * dist, xy[1] + dy * dist
def rect_at(c, w, d, side):
"""item box of width w (along wall) and depth d centred at c, for an item backed onto `side`"""
if side in "NS":
return (c[0] - w / 2, c[0] + w / 2, c[1] - d / 2, c[1] + d / 2)
return (c[0] - d / 2, c[0] + d / 2, c[1] - w / 2, c[1] + w / 2)
# ------------------------------------------------------------------ rules per room type
def bedroom(R):
double = min(R.w, R.d) >= 9.0
bw, bl = (5.0, 6.6) if double else (3.5, 6.5)
model = themes.size(R.T, "bed")
piece = {}
if model and double:
bw, bl = model[:2]
elif model: # a theme's bed is a double: a single room gets the built-in single bed
piece = dict(piece="@contemporary")
nw, nd, _ = R.sizes("nightstand", [(1.5, 1.4)])[0]
for side in R.sides_by("long", tall=True):
depth_room = R.d if side in "NS" else R.w
if depth_room < bl + 2.2:
continue
p = R.spot(side, bw + 2 * (nw + 0.1), bl, tall=True)
stands = p is not None
p = p or R.spot(side, bw, bl, tall=True)
if not p:
continue
x, y, rot, box = p
bb = rect_at((x, y), bw, bl, side)
# wood panel behind the bed: only as wide as the wall that is free there (never past a door or wall end)
c = x if side in "NS" else y
iv = next(((a, b) for a, b in R.free[side]["tall"] if a - 0.01 <= c <= b + 0.01), (c - bw / 2, c + bw / 2))
panel = max(0.0, min(bw + 3.2 if stands else bw + 1.0, 2 * min(c - iv[0], iv[1] - c) - 0.2))
art = themes.has(R.T, "art") # a theme's picture over the bed instead of the wood panel
R.add("bed", bb, x, y, rot, w=bw, l=bl, panel=0.0 if art else round(panel, 2), **piece)
if stands:
for s in (-1, 1):
off = (bw / 2 + nw / 2 + 0.05) * s
cx, cy = (x + off, y) if side in "NS" else (x, y + off)
cx, cy = front_offset(side, (cx, cy), -(bl / 2 - nd / 2 - 0.1))
R.add("nightstand", rect_at((cx, cy), nw, nd, side), cx, cy, rot)
if art:
R.hang("art", side, c, max(4.6, (model[2] if model and not piece else 3.9) + 0.6))
if themes.has(R.T, "bed_end"): # a bench at the foot of the bed, if a walkway stays beyond it
m = R.sizes("bed_end", [])[0]
if m[0] <= bw:
c2 = front_offset(side, (x, y), bl / 2 + 0.25 + m[1] / 2)
if R.fits(rect_at(c2, m[0], m[1], side)) and inside(rect_at(front_offset(side, c2, 1.25), m[0], m[1] + 2.5, side), R.rect):
R.add("bed_end", rect_at(c2, m[0], m[1], side), *c2, rot)
break
# wardrobe on another wall, towards a corner
for side in R.sides_by("long", tall=True):
for w in (6.0, 5.0, 4.0):
p = R.spot(side, w, 2.0, tall=True, align="end", gap=0.1)
if p:
x, y, rot, box = p
R.add("wardrobe", box, x, y, rot, w=w, d=2.0, h=7.0)
break
else:
continue
break
if R.w * R.d > 120:
desk(R)
# the bed's wall panel stops short of anything else standing against that wall (wardrobe, desk)
for bed in [i for i in R.items if i["fn"] == "bed"]:
bx, by, rot = bed["args"]
side = next(k for k, v in SIDES.items() if v == rot)
c, face = (bx, {"N": R.rect[3], "S": R.rect[2]}.get(side)) if side in "NS" else (by, {"E": R.rect[1], "W": R.rect[0]}[side])
half = bed["kw"]["panel"] / 2
for it in R.items:
if it is bed or it["fn"] in ("nightstand", "art"):
continue
b = it["box"]
lo, hi, n0, n1 = (b[0], b[1], b[2], b[3]) if side in "NS" else (b[2], b[3], b[0], b[1])
if n0 - 0.3 <= face <= n1 + 0.3:
if hi <= c:
half = min(half, c - hi - 0.05)
elif lo >= c:
half = min(half, lo - c - 0.05)
bed["kw"]["panel"] = round(max(0.0, 2 * half), 2)
def along(R, side, w, d, from_hi, tall=False, gap=0.0):
"""first spot for a w x d item backed onto `side`, scanning along the wall from one end
(from_hi: start at the high-x / high-y end). gap: clear space kept on both sides of the item."""
x0, x1, y0, y1 = R.rect
lo, hi = (x0, x1) if side in "NS" else (y0, y1)
n = int((hi - lo - w) / 0.25) + 1
for k in range(max(n, 0)):
c = (hi - w / 2 - k * 0.25) if from_hi else (lo + w / 2 + k * 0.25)
if not any(a - 0.01 <= c - w / 2 and c + w / 2 <= b + 0.01 for a, b in R.free[side]["tall" if tall else "low"]):
continue
box = rect_at(((c, {"N": y1 - d / 2, "S": y0 + d / 2}[side]) if side in "NS" else
({"E": x1 - d / 2, "W": x0 + d / 2}[side], c)), w, d, side)
roomy = rect_at(((box[0] + box[1]) / 2, (box[2] + box[3]) / 2), w + 2 * gap, d, side)
if R.fits(box) and not any(overlap(roomy, it["box"], 0.02) for it in R.items if it.get("solid", True)):
return ((box[0] + box[1]) / 2, (box[2] + box[3]) / 2, SIDES[side], box)
return None
def toilet(R):
"""basin by the door, then the WC, then the shower in the far corner - one wall if it is long enough,
so the WC never faces the basin across a small room"""
ds = R.door_side()
door = R.doors[0]["rect"] if R.doors else None
walls = [s for s in R.sides_by("long") if s != ds]
placed = False
for side in walls:
if door: # which end of this wall is nearer the door
dc = (door[0] + door[1]) / 2 if side in "NS" else (door[2] + door[3]) / 2
lo, hi = (R.rect[0], R.rect[1]) if side in "NS" else (R.rect[2], R.rect[3])
near_hi = abs(dc - hi) < abs(dc - lo)
else:
near_hi = False
v = along(R, side, 2.4, 1.7, near_hi, tall=True)
if not v:
continue
R.add("vanity", v[3], v[0], v[1], v[2], w=2.2)
wc = along(R, side, 1.6, 2.2, near_hi, gap=0.4)
if not wc: # WC beside the basin wall, at the far corner
R.items.pop()
continue
R.add("wc2", wc[3], wc[0], wc[1], wc[2])
placed = True
break
if not placed: # small room: WC facing the door, basin on a side wall next to the door
order = [OPP[ds]] + [s for s in "NSEW" if s not in (ds, OPP[ds])] if ds else list("NSEW")
for side in order:
p = R.spot(side, 1.6, 2.2, align="end", gap=0.3)
if p:
R.add("wc2", p[3], p[0], p[1], p[2])
break
for side in order[1:]:
p = R.spot(side, 2.4, 1.7, tall=True, align="start", gap=0.2)
if p:
R.add("vanity", p[3], p[0], p[1], p[2], w=2.2)
break
for side in [s for s in R.sides_by("long") if s != ds] + ([ds] if ds else []): # shower in a free corner
for from_hi in (True, False):
p = along(R, side, 2.6, 2.6, from_hi)
if p and (abs(p[3][0] - R.rect[0]) < 0.05 or abs(p[3][1] - R.rect[1]) < 0.05 or
abs(p[3][2] - R.rect[2]) < 0.05 or abs(p[3][3] - R.rect[3]) < 0.05) and \
sum(abs(v - e) < 0.05 for v, e in zip(p[3], R.rect)) >= 2:
x, y, rot, box = p
wx, wy = {"N": (x, box[3]), "S": (x, box[2]), "E": (box[1], y), "W": (box[0], y)}[side]
R.add("shower2", box, wx, wy, rot)
R.add("geyser", box, wx, wy, rot, solid=False)
return
def kitchen(R):
"""L-shaped platform: the main run on the window wall with the sink under the window, the return run (hob +
chimney) on the side wall away from the door; fridge on a free tall wall; nothing on the door's wall."""
ds = R.door_side()
x0, x1, y0, y1 = R.rect
FRONT = {"N": "-y", "S": "+y", "E": "-x", "W": "+x"}
def window_at(side): # centre of a window on this wall (a gap in the tall-free spans), or None
t = R.free[side]["tall"]
gaps = [((p + q) / 2, q - p) for p, q in zip([v[1] for v in t], [v[0] for v in t[1:]])]
if not t and R.free[side]["low"]:
a_, b_ = max(R.free[side]["low"], key=lambda iv: iv[1] - iv[0])
gaps = [((a_ + b_) / 2, b_ - a_)]
return max(gaps, key=lambda g: g[1])[0] if gaps else None
def box_on(side, a_, b_):
return {"N": (a_, b_, y1 - 2.0, y1), "S": (a_, b_, y0, y0 + 2.0), "E": (x1 - 2.0, x1, a_, b_),
"W": (x0, x0 + 2.0, a_, b_)}[side]
def fit_run(side, a_, b_, keep, min_len):
"""shrink [a_, b_] from the end away from `keep` ('lo' / 'hi' / None) until it fits"""
while b_ - a_ >= min_len:
if R.fits(box_on(side, a_, b_)):
return a_, b_
if keep == "hi" or (keep is None and (b_ - a_) % 1.0 < 0.5):
a_ += 0.5
else:
b_ -= 0.5
return None
def score(side):
length = max((q - p for p, q in R.free[side]["low"]), default=0)
return length + 5 * (window_at(side) is not None) + 3 * (side == OPP.get(ds)) - 8 * (side == ds)
main = None
for side in sorted("NSEW", key=score, reverse=True):
if side == ds or not R.free[side]["low"]:
continue
a_, b_ = max(R.free[side]["low"], key=lambda iv: iv[1] - iv[0])
r = fit_run(side, a_, b_, None, 4.5)
if r:
main = (side, *r)
break
if not main:
return fridge(R)
side, a_, b_ = main
win = window_at(side)
sink = win if win is not None and a_ + 1.2 < win < b_ - 1.2 else None
# return run on a side wall, starting in the corner next to the main run, on the end away from the door
ret = None
cross = "EW" if side in "NS" else "NS"
face = {"N": y1, "S": y0, "E": x1, "W": x0}[side]
for s2 in sorted(cross, key=lambda q: q == ds):
if s2 == ds:
continue
corner_hi = face in (y1, x1) # the main run sits at the high end of this side wall
for p, q in R.free[s2]["low"]:
if corner_hi:
q2 = min(q, face - 2.0)
if abs(q - face) > 0.3 or q2 - p < 3.0:
continue
r = fit_run(s2, max(p, q2 - 6.0), q2, "hi", 3.0)
else:
p2 = max(p, face + 2.0)
if abs(p - face) > 0.3 or q - p2 < 3.0:
continue
r = fit_run(s2, p2, min(q, p2 + 6.0), "lo", 3.0)
if r:
ret = (s2, *r)
break
if ret:
break
# which end of the main run touches the return (the hob goes there if there is no return)
hob_main = None
if not ret:
hob_main = (b_ - 1.6) if sink is None or abs((b_ - 1.6) - sink) > 2.6 else (a_ + 1.6)
if sink is not None and abs(hob_main - sink) < 2.6:
hob_main = None
R.add("kitchen_run", box_on(side, a_, b_), *box_on(side, a_, b_), FRONT[side],
sink=sink if sink is not None else (a_ + 1.4 if ret is None else (a_ + b_) / 2), hob=hob_main)
runs = [(side, a_, b_, hob_main)]
if ret:
s2, p, q = ret
hob = (p + q) / 2
R.add("kitchen_run", box_on(s2, p, q), *box_on(s2, p, q), FRONT[s2], sink=None, hob=hob)
runs.append((s2, p, q, hob))
for sd, p, q, hob in runs:
if hob is not None:
hx, hy = {"N": (hob, y1), "S": (hob, y0), "E": (x1, hob), "W": (x0, hob)}[sd]
R.add("chimney", (0, 0, 0, 0), hx, hy, SIDES[sd], solid=False)
for u0, v0 in R.free[sd]["tall"]: # wall cabinets where the wall is solid, not over the hob
segs = [(max(u0, p), min(v0, q))]
if hob is not None:
segs = subtract_iv(segs, hob - 1.4, hob + 1.4)
for u, v in segs:
if v - u >= 1.25:
box = {"N": (u, v, y1 - 1.1, y1), "S": (u, v, y0, y0 + 1.1), "E": (x1 - 1.1, x1, u, v), "W": (x0, x0 + 1.1, u, v)}[sd]
R.add("wall_cab2", (0, 0, 0, 0), *box, FRONT[sd], solid=False)
fridge(R)
def fridge(R):
ds = R.door_side()
for side in sorted(R.sides_by("long", tall=True), key=lambda q: q == ds):
p = R.spot(side, 2.4, 2.3, tall=True, align="end", gap=0.1)
if p:
R.add("fridge2", p[3], p[0], p[1], p[2])
return
def living(R, extras=None):
"""sofa, coffee table and rug, a TV unit opposite; an armchair and a corner piece only in a room big enough for them
(extras: None = by the room's size)"""
if extras is None:
extras = R.w * R.d >= 150 # about 12 x 12 ft
unit_d = R.sizes("tv_unit", [(5.0, 1.4)])[0][1]
for side in R.sides_by("long"):
depth = R.w if side in "EW" else R.d # from the sofa's wall to the one opposite
for w, sd, skw in R.sizes("sofa", [(7.0, 3.0), (6.0, 3.0), (5.0, 3.0)]):
p = R.spot(side, w, sd)
if not p:
continue
x, y, rot, box = p
tables = R.sizes("coffee_table", [(3.6, 2.0)])
m = themes.size(R.T, "coffee_table")
if m: # a smaller copy of the theme's own table before a plain one
tables[3:3] = [(m[0] * k, m[1] * k, {}) for k in (0.7, 0.6)]
for cw, cd, ckw in tables:
ct = front_offset(side, (x, y), sd / 2 + 0.5 + cd / 2)
cbox = rect_at(ct, cw, cd, side)
# a 2 ft walk between the table and the unit opposite
if R.fits(cbox) and depth - (sd + 0.5 + cd) >= unit_d + 2.0:
break
else:
continue
R.add("sofa", box, x, y, rot, w=w, **skw)
R.add("coffee_table", cbox, ct[0], ct[1], rot, **ckw)
rbox = rect_at(ct, w + 0.5, max(5.5, cd + 3.0), side)
R.add("rug", rbox, *rbox, solid=False)
R.hang("art", side, x if side in "NS" else y, R.height("sofa", 3.0) + 0.7)
# an armchair at one end of the coffee table, facing it
if extras and themes.has(R.T, "armchair"):
aw, ad, _ = R.sizes("armchair", [])[0]
for s in (1, -1):
along = cw / 2 + 0.6 + ad / 2
c = (ct[0] + s * along, ct[1]) if side in "NS" else (ct[0], ct[1] + s * along)
back = ("E" if s > 0 else "W") if side in "NS" else ("N" if s > 0 else "S")
if R.fits(rect_at(c, aw, ad, back)):
R.add("armchair", rect_at(c, aw, ad, back), *c, SIDES[back])
break
# TV (or a theme's commode with a mirror) on the opposite wall if it is a wall
for tw, td, tkw in R.sizes("tv_unit", [(5.0, 1.4)]):
q = R.spot(OPP[side], tw, td)
if q:
R.add("tv_unit", q[3], q[0], q[1], q[2], **tkw)
m, cq = themes.size(R.T, "tv_unit"), q[0] if side in "NS" else q[1]
top = m[2] * tw / m[0] if m and not tkw else 1.9 # a model is scaled to its box, its top with it
if themes.has(R.T, "decor"): # on the unit: beside the TV, or in the middle with no TV
dw = themes.size(R.T, "decor")[0] if themes.size(R.T, "decor") else 0.7
off = (tw / 2 - dw / 2 - 0.2) * (1 if R.T["tv"] else 0)
dx, dy = (q[0] + off, q[1]) if side in "NS" else (q[0], q[1] + off)
R.add("decor", rect_at((dx, dy), dw, dw, OPP[side]), dx, dy, q[2], z=round(top, 2), solid=False)
if not R.T["tv"]:
R.hang("mirror", OPP[side], cq, top + 0.9)
break
if extras:
corner(R)
return
def corner(R):
"""a theme's tall corner piece (clock, shelves) against a free wall, towards a corner"""
if themes.has(R.T, "corner"):
w, d, _ = R.sizes("corner", [])[0]
for side in R.sides_by("long", tall=True):
p = R.spot(side, w, d, tall=True, align="end", gap=0.3)
if p:
R.add("corner", p[3], p[0], p[1], p[2])
return
def desk(R):
for side in R.sides_by("long"):
for w, d, kw in R.sizes("desk", [(3.5, 2.0)]):
p = R.spot(side, w, d)
if p:
R.add("desk", p[3], p[0], p[1], p[2], **kw)
return
def dining(R):
if R.zone.get("area", R.w * R.d) >= 150: # a big "DRG./DIN." room is drawing + dining: sofa set first
living(R, extras=R.w * R.d >= 250) # (the table needs about 100 sq ft of it)
if not dining_table(R): # no room left beside the sofa: the table goes in the L's other arm
r = max_rect(R.zone["rects"], exclude=R.rect)
if r and min(r[1] - r[0], r[3] - r[2]) >= 6.0:
R2 = Room(dict(R.zone, rects=[r]), R.floor)
if dining_table(R2):
R.items += R2.items
return
dining_table(R)
def dining_table(R):
long_x = R.w >= R.d
# (table w, l, seats, chair clearance); slide along the long axis from the centre outwards
sizes = ((6.0, 3.2, 6, 2.2), (4.5, 3.0, 4, 2.2), (4.5, 3.0, 4, 1.8), (3.0, 3.0, 2, 1.8))
piece = {}
if themes.size(R.T, "dining_set"): # a theme's table: as many chairs a side as fit along it (at most three)
chair = (themes.size(R.T, "dining_chair") or (1.6,))[0]
sizes = [(w, d, 2 * max(1, min(3, int((w - 0.4) / (chair + 0.3)))), 2.2) for w, d, _ in R.sizes("dining_set", [])] + \
[(*q, "fallback") for q in sizes]
for tw, tl, n, m, *fb in sizes:
if fb: # the theme's table does not fit: the built-in one in the theme's colours
piece = dict(piece="@contemporary")
for t, u in sorted(((k / 40, j / 40) for k in range(4, 37) for j in range(14, 27)),
key=lambda q: abs(q[0] - 0.5) + 2 * abs(q[1] - 0.5)):
cx = R.rect[0] + R.w * (t if long_x else u)
cy = R.rect[2] + R.d * (u if long_x else t)
a, b = (tw, tl) if long_x else (tl, tw)
box = (cx - a / 2 - m, cx + a / 2 + m, cy - b / 2 - m, cy + b / 2 + m)
seats = (cx - a / 2 - 1.6, cx + a / 2 + 1.6, cy - b / 2 - 1.6, cy + b / 2 + 1.6)
# room to pull the chairs out from walls and furniture; walkways need only stay clear of the chairs
if R.fits(box, ignore_keepout=True) and R.fits(seats):
R.add("dining_set", seats, cx, cy, 0 if long_x else 90, w=tw, d=tl, seats=n, **piece)
return True
return False
def dress(R):
"""walk-in dressing room: wardrobes along the free walls"""
for side in R.sides_by("long", tall=True)[:2]:
for w in (6.0, 5.0, 4.0, 3.0):
p = R.spot(side, w, 2.0, tall=True)
if p:
R.add("wardrobe", p[3], p[0], p[1], p[2], w=w, d=2.0, h=7.0)
break
def puja(R):
ds = R.door_side()
for side in ([OPP[ds]] if ds else []) + list("NSEW"):
for w in (3.6, 3.0, 2.4):
p = R.spot(side, w, 1.6, tall=True)
if p:
R.add("puja_mandir", p[3], p[0], p[1], p[2], w=w)
return
def wash(R):
for side in R.sides_by("long"):
p = R.spot(side, 2.1, 2.0, align="end", gap=0.2)
if p:
R.add("washer2", p[3], p[0], p[1], p[2])
return
def parking(R):
cx, cy = (R.rect[0] + R.rect[1]) / 2, (R.rect[2] + R.rect[3]) / 2
along_y = R.d >= R.w
L, W = 14.5, 6.2
box = (cx - W / 2, cx + W / 2, cy - L / 2, cy + L / 2) if along_y else (cx - L / 2, cx + L / 2, cy - W / 2, cy + W / 2)
if R.fits(box):
R.add("car", box, cx, cy, 0 if along_y else 90)
def balcony(R):
for side in R.sides_by("long"):
p = R.spot(side, 4.0, 3.5)
if p:
R.add("outdoor_set", p[3], p[0], p[1], p[2])
break
plants(R, 2)
def garden(R):
plants(R, max(1, int(max(R.w, R.d) / 6)))
def plants(R, n):
"""potted plants in the corners, then along the long sides, never out in the middle of the floor"""
x0, x1, y0, y1 = R.rect
m = 0.9 # pot radius 0.75 and a little air
spots = [(x0 + m, y0 + m), (x1 - m, y1 - m), (x1 - m, y0 + m), (x0 + m, y1 - m)]
k = int(max(R.w, R.d) / 6)
for i in range(1, k):
t = i / k
spots += [(x0 + R.w * t, y0 + m), (x0 + R.w * t, y1 - m)] if R.w >= R.d else [(x0 + m, y0 + R.d * t), (x1 - m, y0 + R.d * t)]
box = lambda x, y: (x - 0.8, x + 0.8, y - 0.8, y + 0.8)
placed = []
for k in range(n): # each one as far from the others as it can be
free = [q for q in spots if R.fits(box(*q))]
if not free:
return
x, y = max(free, key=lambda q: min((abs(q[0] - p[0]) + abs(q[1] - p[1]) for p in placed), default=0))
R.add("plant", box(x, y), x, y, k * 70)
placed.append((x, y))
RULES = dict(dress=dress, bedroom=bedroom, toilet=toilet, kitchen=kitchen, living=living, dining=dining, puja=puja, wash=wash,
parking=parking, balcony=balcony, garden=garden, study=lambda R: bedroom_study(R))
def bedroom_study(R):
desk(R)
def furnish_floor(floor, theme=None):
"""-> list of items {fn, args, kw, box, solid, room} for one floor, furnished in `theme` (themes.load result)"""
out = []
for z in floor["rooms"]:
if z["type"] not in RULES or not z["rects"]:
continue
R = Room(z, floor, theme)
if R.w < 2.5 or R.d < 2.5:
continue
RULES[z["type"]](R)
out += R.items
return out
def floor_items(plan, floor):
"""what Blender builds on a floor: nothing if furnishing is off, the review page's own list if it edited this floor
(an empty one means empty rooms), else the rules' choice"""
return [] if plan.get("furnish") is False else floor["items"] if "items" in floor else \
furnish_floor(dict(floor, floor_height=plan.get("floor_height", 10.0)), themes.load(plan.get("theme")))
def clashes(items, floor):
"""sanity check: solid items overlapping each other, or blocking a doorway"""
bad = []
solid = [it for it in items if it["solid"] and it["box"][1] > it["box"][0]]
for i, a in enumerate(solid):
for b in solid[i + 1:]:
if overlap(a["box"], b["box"], 0.05) and a["fn"] != "rug" and b["fn"] != "rug":
bad.append(f'{a["fn"]} x {b["fn"]} in {a["room"]}')
for o in floor["openings"]:
if o["type"] == "window":
continue
r = o["rect"]
for it in solid:
if overlap(it["box"], (r[0] - 0.1, r[1] + 0.1, r[2] - 0.1, r[3] + 0.1)):
bad.append(f'{it["fn"]} blocks a doorway in {it["room"]}')
return bad
if __name__ == "__main__":
import json, sys
P = json.load(open(sys.argv[1]))
for f in P["floors"]:
items = furnish_floor(f)
from collections import Counter
print(f["name"], len(items), dict(Counter(i["fn"] for i in items)))
for b in clashes(items, f):
print(" CLASH", b)