# -*- coding: utf-8 -*- """Интерьеры дома по плану владельца и фото: комнаты, мебель, свет. Запуск: blender -b -P house_interior.py Координаты комнат — из обводки владельца (пиксели плана 916x768), перевод в метры: x = (px-180)/42, y = (py-173)/41.4. """ import json import math import os import bpy from mathutils import Vector OUT = os.path.dirname(os.path.abspath(__file__)) POLY = json.load(open(os.path.join(OUT, "room-zones.json"), encoding="utf-8")) X0, Y0, SX, SY = 180.0, 173.0, 42.0, 41.4 Z0 = 0.80 # уровень пола WH = 2.60 # высота стен внутри TER = 2.45 # терраса (снаружи блока) def to_m(poly): return [((x - X0) / SX, (y - Y0) / SY) for x, y in poly] ROOMS_PX = {k: to_m(v) for k, v in POLY.items()} def lin(c): return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4 def col(h): h = h.lstrip("#") return tuple(lin(int(h[i:i + 2], 16) / 255) for i in (0, 2, 4)) + (1.0,) def mat(name, hexv, rough=0.7, metal=0.0, emit=None, strength=1.0): m = bpy.data.materials.new(name) m.use_nodes = True b = m.node_tree.nodes["Principled BSDF"] b.inputs["Base Color"].default_value = col(hexv) b.inputs["Roughness"].default_value = rough b.inputs["Metallic"].default_value = metal if emit: b.inputs["Emission Color"].default_value = col(emit) b.inputs["Emission Strength"].default_value = strength return m def mat_tex(name, img, rough=0.6, metal=0.0): m = bpy.data.materials.new(name) m.use_nodes = True nt = m.node_tree b = nt.nodes["Principled BSDF"] b.inputs["Roughness"].default_value = rough b.inputs["Metallic"].default_value = metal tex = nt.nodes.new("ShaderNodeTexImage") tex.image = bpy.data.images.load(img) nt.links.new(tex.outputs["Color"], b.inputs["Base Color"]) return m def box(name, x0, x1, y0, y1, z0, z1, m, parent=None): bpy.ops.mesh.primitive_cube_add(size=2) o = bpy.context.object o.name = name o.scale = ((x1 - x0) / 2, (y1 - y0) / 2, (z1 - z0) / 2) o.location = ((x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2) o.data.materials.append(m) if parent: o.parent = parent return o def poly_floor(name, poly, z, th, m, parent=None): n = len(poly) verts = [(x, y, z) for x, y in poly] + [(x, y, z - th) for x, y in poly] faces = [tuple(range(n)), tuple(n + i for i in range(n)[::-1])] for i in range(n): j = (i + 1) % n faces.append((i, j, n + j, n + i)) me = bpy.data.meshes.new(name) me.from_pydata(verts, [], faces) me.update() o = bpy.data.objects.new(name, me) bpy.context.collection.objects.link(o) o.data.materials.append(m) if parent: o.parent = parent return o def main(): bpy.ops.wm.read_factory_settings(use_empty=True) sc = bpy.context.scene T = OUT wood_floor = mat_tex("FloorWood", os.path.join(T, "tex_int_floor.png"), 0.5) tile_floor = mat_tex("FloorTile", os.path.join(T, "tex_int_tile.png"), 0.35) wall_light = mat_tex("WallLight", os.path.join(T, "tex_int_wall.png"), 0.85) wall_green = mat_tex("WallGreen", os.path.join(T, "tex_int_green.png"), 0.8) wall_wood = mat("WallWood", "8A6A45", 0.6) wall_blue = mat_tex("WallBlue", os.path.join(T, "tex_int_blue.png"), 0.8) floor_stone = mat_tex("FloorStone", os.path.join(T, "tex_int_stone.png"), 0.45) tile_dark = mat_tex("TileDark", os.path.join(T, "tex_int_tile.png"), 0.3) white = mat("White", "F2F2EE", 0.5) grey_soft = mat("GreySoft", "8E9A9E", 0.7) fabric = mat("Fabric", "4A3F45", 0.85) wood_f = mat("FurnWood", "A9825A", 0.6) black = mat("Black", "1E2024", 0.5) steel = mat("Steel", "9AA3A8", 0.35, 0.7) lamp = mat("LampWarm", "FFE7C0", 0.4, 0.0, emit="FFD9A0", strength=6.0) FLOOR = { "kitchen": wood_floor, "koridor": floor_stone, "prihozhaya": floor_stone, "tambur": tile_floor, "kryltso": wood_floor, "spalnia": wood_floor, "danina_komnata": wood_floor, "kabinet": wood_floor, "garderob": wood_floor, "kladovaia": tile_floor, "sanuzel": tile_floor, "tualet": tile_floor, "postirochnaia": tile_floor, "terrasa": wood_floor, } WALLS = { "spalnia": wall_green, "danina_komnata": wall_light, "kabinet": wall_blue, "sanuzel": tile_dark, "tualet": tile_dark, "kladovaia": wall_light, "garderob": wall_light, "koridor": wall_light, "kitchen": wall_light, "prihozhaya": wall_light, "tambur": wall_light, "postirochnaia": wall_light, "terrasa": wall_wood, "kryltso": wall_wood, } groups = {} for rid, poly in ROOMS_PX.items(): g = bpy.data.objects.new("room_%s" % rid, None) g.empty_display_size = 0.2 bpy.context.collection.objects.link(g) groups[rid] = g poly_floor("zone_%s" % rid, poly, Z0 - 0.02, 0.06, FLOOR.get(rid, wood_floor), g) poly_floor("ceiling_%s" % rid, poly, Z0 + WH + 0.06, 0.06, wall_light, g) # ---------- мебель ---------- def bbox(rid): xs = [p[0] for p in ROOMS_PX[rid]] ys = [p[1] for p in ROOMS_PX[rid]] return min(xs), max(xs), min(ys), max(ys) # кухня-гостиная: стол, стулья, диван, ТВ, кухонный остров x0, x1, y0, y1 = bbox("kitchen") box("k_table", x0 + 1.4, x0 + 2.6, y0 + 0.9, y0 + 1.9, Z0 + 0.72, Z0 + 0.76, wood_f, groups["kitchen"]) for i in range(4): for j in (0, 1): cx = x0 + 1.55 + i * 0.35 cy = y0 + 0.65 + j * 1.5 box("k_chair_%d%d" % (i, j), cx - 0.18, cx + 0.18, cy - 0.18, cy + 0.18, Z0, Z0 + 0.45, black, groups["kitchen"]) box("k_sofa", x0 + 0.4, x0 + 2.4, y0 + 3.2, y0 + 4.2, Z0, Z0 + 0.45, fabric, groups["kitchen"]) box("k_sofa_back", x0 + 0.4, x0 + 2.4, y0 + 4.05, y0 + 4.2, Z0 + 0.45, Z0 + 0.85, fabric, groups["kitchen"]) box("k_tv", x0 + 0.5, x0 + 1.9, y0 + 5.6, y0 + 5.75, Z0 + 0.5, Z0 + 1.1, black, groups["kitchen"]) box("k_island", x0 + 2.9, x0 + 3.5, y0 + 0.4, y0 + 2.6, Z0, Z0 + 0.9, white, groups["kitchen"]) box("k_counter", x1 - 0.65, x1 - 0.05, y0 + 0.4, y0 + 2.8, Z0, Z0 + 0.9, wood_f, groups["kitchen"]) # спальня: кровать, тумбы, шкаф x0, x1, y0, y1 = bbox("spalnia") box("s_bed", x0 + 0.4, x0 + 2.2, y0 + 1.2, y0 + 3.2, Z0 + 0.1, Z0 + 0.55, grey_soft, groups["spalnia"]) box("s_bed_blanket", x0 + 0.45, x0 + 2.15, y0 + 1.25, y0 + 3.15, Z0 + 0.55, Z0 + 0.62, fabric, groups["spalnia"]) box("s_head", x0 + 0.4, x0 + 2.2, y0 + 3.1, y0 + 3.2, Z0 + 0.55, Z0 + 1.15, grey_soft, groups["spalnia"]) for i, yy in enumerate((y0 + 0.9, y0 + 3.3)): box("s_night_%d" % i, x0 + 2.35, x0 + 2.75, yy - 0.2, yy + 0.2, Z0, Z0 + 0.5, wood_f, groups["spalnia"]) box("s_wardrobe", x1 - 0.65, x1 - 0.05, y0 + 0.3, y0 + 2.9, Z0, Z0 + 2.2, white, groups["spalnia"]) box("s_accent", x0 + 0.3, x0 + 2.3, y0 + 3.25, y0 + 3.35, Z0, Z0 + 2.2, wall_wood, groups["spalnia"]) # комната 12,04 (Данина) x0, x1, y0, y1 = bbox("danina_komnata") box("d_bed", x0 + 0.3, x0 + 1.8, y0 + 1.0, y0 + 3.0, Z0 + 0.1, Z0 + 0.55, grey_soft, groups["danina_komnata"]) box("d_desk", x0 + 2.2, x1 - 0.2, y0 + 0.4, y0 + 1.0, Z0 + 0.7, Z0 + 0.74, wood_f, groups["danina_komnata"]) box("d_chair", x0 + 2.6, x0 + 3.0, y0 + 1.05, y0 + 1.45, Z0, Z0 + 0.45, black, groups["danina_komnata"]) box("d_shelf", x0 + 0.3, x0 + 0.6, y0 + 3.2, y0 + 4.2, Z0, Z0 + 1.8, wood_f, groups["danina_komnata"]) # кабинет (Комната 11,45) x0, x1, y0, y1 = bbox("kabinet") box("o_desk", x0 + 0.5, x0 + 2.5, y0 + 0.5, y0 + 1.1, Z0 + 0.7, Z0 + 0.74, wood_f, groups["kabinet"]) box("o_chair", x0 + 1.2, x0 + 1.6, y0 + 1.2, y0 + 1.6, Z0, Z0 + 0.5, black, groups["kabinet"]) box("o_shelf", x1 - 0.5, x1 - 0.1, y0 + 0.4, y0 + 3.4, Z0, Z0 + 2.0, white, groups["kabinet"]) # санузел: ванна, тумба с раковиной, зеркало x0, x1, y0, y1 = bbox("sanuzel") box("b_bath", x1 - 0.9, x1 - 0.1, y0 + 0.4, y0 + 2.1, Z0, Z0 + 0.55, white, groups["sanuzel"]) box("b_vanity", x0 + 0.3, x0 + 1.0, y0 + 1.6, y0 + 2.4, Z0, Z0 + 0.85, wood_f, groups["sanuzel"]) box("b_sink", x0 + 0.35, x0 + 0.95, y0 + 1.7, y0 + 2.3, Z0 + 0.85, Z0 + 0.95, white, groups["sanuzel"]) box("b_mirror", x0 + 0.35, x0 + 0.95, y0 + 1.62, y0 + 1.66, Z0 + 1.0, Z0 + 1.8, steel, groups["sanuzel"]) # туалет x0, x1, y0, y1 = bbox("tualet") box("t_toilet", x0 + 0.2, x0 + 0.6, y0 + 0.2, y0 + 0.7, Z0, Z0 + 0.45, white, groups["tualet"]) # гардероб / кладовая x0, x1, y0, y1 = bbox("garderob") box("g_shelf", x0 + 0.2, x1 - 0.2, y0 + 0.2, y0 + 0.6, Z0, Z0 + 2.0, wood_f, groups["garderob"]) x0, x1, y0, y1 = bbox("kladovaia") box("k_washer", x0 + 0.2, x0 + 0.8, y0 + 0.3, y0 + 0.9, Z0, Z0 + 0.85, white, groups["kladovaia"]) box("k_shelf", x1 - 0.5, x1 - 0.1, y0 + 0.2, y0 + 1.2, Z0, Z0 + 1.8, wood_f, groups["kladovaia"]) # холл: шкаф и скамья x0, x1, y0, y1 = bbox("koridor") box("h_wardrobe", x0 + 0.2, x0 + 1.2, y0 + 0.2, y0 + 0.8, Z0, Z0 + 2.1, white, groups["koridor"]) box("h_bench", x0 + 1.6, x0 + 2.6, y0 + 0.3, y0 + 0.7, Z0, Z0 + 0.45, wood_f, groups["koridor"]) # прихожая / тамбур x0, x1, y0, y1 = bbox("prihozhaya") box("p_shoe", x0 + 0.2, x1 - 0.2, y0 + 0.2, y0 + 0.5, Z0, Z0 + 0.5, wood_f, groups["prihozhaya"]) x0, x1, y0, y1 = bbox("tambur") box("t_mat", x0 + 0.3, x1 - 0.3, y0 + 0.3, y1 - 0.3, Z0, Z0 + 0.02, grey_soft, groups["tambur"]) # тех.пом.: стойка x0, x1, y0, y1 = bbox("postirochnaia") box("th_rack", x0 + 0.2, x0 + 1.4, y0 + 0.2, y0 + 0.8, Z0, Z0 + 1.8, black, groups["postirochnaia"]) # терраса: стол и стулья x0, x1, y0, y1 = bbox("terrasa") box("ter_table", x0 + 0.4, x0 + 1.3, y0 + 4.4, y0 + 5.3, Z0 + 0.72, Z0 + 0.76, wood_f, groups["terrasa"]) # --- двери (полотно + тёмная полоса, как на фото) --- hx = sum(q[0] for q in ROOMS_PX["koridor"]) / len(ROOMS_PX["koridor"]) hy = sum(q[1] for q in ROOMS_PX["koridor"]) / len(ROOMS_PX["koridor"]) for rid, poly in ROOMS_PX.items(): if rid in ("koridor", "terrasa", "kryltso", "kitchen"): continue n = len(poly) best = None for i in range(n): ax, ay = poly[i] bx, by = poly[(i + 1) % n] if math.hypot(bx - ax, by - ay) < 0.9: continue mx, my = (ax + bx) / 2, (ay + by) / 2 d = (mx - hx) ** 2 + (my - hy) ** 2 if best is None or d < best[0]: best = (d, ax, ay, bx, by, mx, my) if not best: continue _, ax, ay, bx, by, mx, my = best dx, dy = bx - ax, by - ay L = math.hypot(dx, dy) or 1.0 ux, uy = dx / L, dy / L dw = 0.45 if abs(ux) > abs(uy): box("door_%s" % rid, mx - dw, mx + dw, my - 0.03, my + 0.03, Z0 + 0.02, Z0 + 2.05, white, groups[rid]) box("door_stripe_%s" % rid, mx - dw, mx + dw, my - 0.045, my - 0.03, Z0 + 1.05, Z0 + 1.20, black, groups[rid]) else: box("door_%s" % rid, mx - 0.03, mx + 0.03, my - dw, my + dw, Z0 + 0.02, Z0 + 2.05, white, groups[rid]) box("door_stripe_%s" % rid, mx - 0.045, mx - 0.03, my - dw, my + dw, Z0 + 1.05, Z0 + 1.20, black, groups[rid]) # --- детали по фото --- x0, x1, y0, y1 = bbox("spalnia") for i, xx in enumerate((x0 + 0.6, x0 + 2.0)): box("s_sconce_%d" % i, xx - 0.04, xx + 0.04, y0 + 3.34, y0 + 3.40, Z0 + 1.25, Z0 + 1.75, lamp, groups["spalnia"]) box("s_tv", x1 - 0.28, x1 - 0.22, y0 + 1.6, y0 + 2.6, Z0 + 1.1, Z0 + 1.75, black, groups["spalnia"]) x0, x1, y0, y1 = bbox("sanuzel") box("b_shower_glass", x0 + 1.1, x0 + 1.15, y0 + 2.5, y1 - 0.2, Z0, Z0 + 2.0, steel, groups["sanuzel"]) box("b_shower_head", x0 + 0.3, x0 + 0.5, y0 + 2.6, y0 + 2.8, Z0 + 2.02, Z0 + 2.08, steel, groups["sanuzel"]) x0, x1, y0, y1 = bbox("kabinet") box("o_mon1", x0 + 0.9, x0 + 1.7, y0 + 0.60, y0 + 0.66, Z0 + 1.0, Z0 + 1.45, black, groups["kabinet"]) box("o_mon2", x0 + 1.8, x0 + 2.4, y0 + 0.60, y0 + 0.66, Z0 + 1.0, Z0 + 1.40, black, groups["kabinet"]) box("o_kbd", x0 + 1.1, x0 + 2.2, y0 + 0.75, y0 + 0.95, Z0 + 0.74, Z0 + 0.76, grey_soft, groups["kabinet"]) x0, x1, y0, y1 = bbox("koridor") box("h_mirror", x0 + 1.6, x0 + 2.4, y0 + 0.22, y0 + 0.25, Z0 + 1.1, Z0 + 2.0, steel, groups["koridor"]) x0, x1, y0, y1 = bbox("kitchen") for i, yy in enumerate((y0 + 1.0, y0 + 1.6, y0 + 2.2)): box("k_pend_%d" % i, x0 + 3.05, x0 + 3.35, yy - 0.05, yy + 0.05, Z0 + WH - 0.45, Z0 + WH - 0.15, lamp, groups["kitchen"]) # ---------- перегородки ---------- for rid, poly in ROOMS_PX.items(): n = len(poly) for i in range(n): ax, ay = poly[i] bx, by = poly[(i + 1) % n] length = math.hypot(bx - ax, by - ay) if length < 0.2: continue mx, my = (ax + bx) / 2, (ay + by) / 2 dx, dy = (bx - ax) / length, (by - ay) / length nx, ny = -dy, dx t = 0.05 if abs(dx) > abs(dy): box("part_%s_%d" % (rid, i), min(ax, bx), max(ax, bx), my - t, my + t, Z0, Z0 + WH, WALLS.get(rid, wall_light), groups[rid]) else: box("part_%s_%d" % (rid, i), mx - t, mx + t, min(ay, by), max(ay, by), Z0, Z0 + WH, WALLS.get(rid, wall_light), groups[rid]) # ---------- свет ---------- for rid, poly in ROOMS_PX.items(): cx = sum(p[0] for p in poly) / len(poly) cy = sum(p[1] for p in poly) / len(poly) box("ceiling_lamp_%s" % rid, cx - 0.15, cx + 0.15, cy - 0.15, cy + 0.15, Z0 + WH - 0.06, Z0 + WH, lamp, groups[rid]) # ---------- свет сцены ---------- w = bpy.data.worlds.new("W") sc.world = w w.use_nodes = True w.node_tree.nodes["Background"].inputs["Strength"].default_value = 0.5 w.node_tree.nodes["Background"].inputs["Color"].default_value = col("9FB4C8") bpy.ops.object.light_add(type="SUN", location=(2, 2, 12)) sun = bpy.context.object sun.data.energy = 2.5 sun.rotation_euler = (math.radians(35), 0, math.radians(30)) # ---------- рендер интерьеров ---------- cd = bpy.data.cameras.new("Cam") cd.lens = 16 cam = bpy.data.objects.new("Cam", cd) bpy.context.collection.objects.link(cam) sc.camera = cam sc.render.engine = "CYCLES" sc.cycles.device = "CPU" sc.cycles.samples = 96 sc.cycles.use_denoising = False sc.render.resolution_x, sc.render.resolution_y = 1100, 760 sc.view_settings.view_transform = "AgX" sc.view_settings.exposure = 0.2 def shot(name, loc, tgt): cam.location = loc cam.rotation_euler = (Vector(tgt) - Vector(loc)).to_track_quat("-Z", "Y").to_euler() sc.render.filepath = os.path.join(OUT, name) bpy.ops.render.render(write_still=True) print("render:", name, flush=True) def center(rid): p = ROOMS_PX[rid] return sum(q[0] for q in p) / len(p), sum(q[1] for q in p) / len(p) def bbox_m(rid): p = ROOMS_PX[rid] xs = [q[0] for q in p] ys = [q[1] for q in p] return min(xs), max(xs), min(ys), max(ys) # разрез сверху: видно все комнаты и мебель for o in bpy.data.objects: if o.name.startswith("ceiling_") or o.name.startswith("ceiling_lamp_"): o.hide_render = True bpy.ops.object.light_add(type="SUN", location=(6, 3, 18)) top = bpy.context.object top.data.energy = 4.0 top.rotation_euler = (math.radians(12), 0, math.radians(20)) shot("int-cutaway.png", (4.5, -1.5, 11.0), (7.2, 4.2, 1.0)) shot("int-cutaway-2.png", (13.5, 11.0, 9.0), (7.2, 4.2, 1.0)) for o in bpy.data.objects: if o.name.startswith("ceiling_") or o.name.startswith("ceiling_lamp_"): o.hide_render = False for rid, fname in (): poly = ROOMS_PX[rid] # точка внутри комнаты, максимально далёкая от стен def inside(px, py): c = False j = len(poly) - 1 for i in range(len(poly)): xi, yi = poly[i] xj, yj = poly[j] if ((yi > py) != (yj > py)) and (px < (xj - xi) * (py - yi) / (yj - yi + 1e-9) + xi): c = not c j = i return c xs = [p[0] for p in poly] ys = [p[1] for p in poly] best, bx, by = -1.0, sum(xs) / len(xs), sum(ys) / len(ys) step = 0.15 yy = min(ys) + step while yy < max(ys): xx = min(xs) + step while xx < max(xs): if inside(xx, yy): d = min(min(abs(xx - q[0]), abs(yy - q[1])) for q in poly) d2 = min(((xx - q[0]) ** 2 + (yy - q[1]) ** 2) ** 0.5 for q in poly) if d2 > best: best, bx, by = d2, xx, yy xx += step yy += step far = max(poly, key=lambda q: (q[0] - bx) ** 2 + (q[1] - by) ** 2) loc = (bx, by, Z0 + 1.65) tgt = (far[0], far[1], Z0 + 1.05) bpy.ops.object.light_add(type="AREA", location=(cx, cy, Z0 + 2.3)) fl = bpy.context.object fl.data.energy = 900 fl.data.size = 3.0 shot(fname, loc, tgt) # ---------- UV для текстур интерьера ---------- uv_map = {"FloorWood": 1.2, "FloorTile": 1.0, "WallLight": 1.5, "WallGreen": 0.9, "TileDark": 0.8, "WallBlue": 0.9, "FloorStone": 1.0} bpy.ops.object.select_all(action="DESELECT") for o in bpy.data.objects: if o.type != "MESH" or not o.data.materials: continue size = uv_map.get(o.data.materials[0].name) if size is None: continue o.select_set(True) bpy.context.view_layer.objects.active = o bpy.ops.object.transform_apply(location=False, rotation=False, scale=True) bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action="SELECT") bpy.ops.uv.cube_project(cube_size=size) bpy.ops.object.mode_set(mode="OBJECT") o.select_set(False) # ---------- экспорт только интерьера ---------- # ---------- экспорт ---------- bpy.ops.object.select_all(action="SELECT") glb = os.path.join(OUT, "house-interior-only.glb") bpy.ops.export_scene.gltf(filepath=glb, export_format="GLB", export_apply=True, use_selection=True, export_yup=True) print("glb:", os.path.getsize(glb), flush=True) print("done") if __name__ == "__main__": main()