Files
buryan-infra/ops/home/plans/house_scene2.py
T

214 lines
8.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""Blender: дом «Барнева» 8,5 × 14,5 (блок 8,5 × 12,1 + терраса 2,4).
Палитра: RAL 7024 графит (металл), Tikkurila «Олененок» (дерево фасада),
масло «Графит» (терраса), кровля графит.
Запуск: blender -b -P house_scene.py
"""
import math
import os
import bpy
from mathutils import Vector
OUT = os.path.dirname(os.path.abspath(__file__))
LEN_X, WID_Y = 12.1, 8.5
TER_D = 2.4
Z0, WALL_H, ROOF_H = 0.75, 2.75, 1.85
Z1 = Z0 + WALL_H
ZR = Z1 + ROOF_H
T = 0.16
OVER = 0.42
C_METAL, C_WOOD, C_DECK = "3B3F43", "C9A578", "45474A"
C_ROOF, C_GLASS, C_PILE = "2E3134", "16242C", "B9B3A5"
C_GROUND, C_TRIM, C_PATH = "9FA08F", "2A2D31", "A8A695"
def lin(c):
return c / 12.92 if c <= 0.04045 else ((c + 0.055) / 1.055) ** 2.4
def col(h, a=1.0):
h = h.lstrip("#")
r, g, b = (int(h[i:i + 2], 16) / 255 for i in (0, 2, 4))
return (lin(r), lin(g), lin(b), a)
def mat(name, hexv, rough=0.6, metal=0.0, glass=False):
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 glass:
b.inputs["Transmission Weight"].default_value = 0.7
b.inputs["Roughness"].default_value = 0.06
return m
def box(name, x0, x1, y0, y1, z0, z1, material):
bpy.ops.mesh.primitive_cube_add(size=1)
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(material)
return o
def gable(name, x, y0, y1, z1, zr, th, material):
"""Фронтон: прямоугольник до z1 + треугольник до конька, толщина th."""
x2 = x + th
v = [(x, y0, z1), (x, y1, z1), (x, (y0 + y1) / 2, zr),
(x2, y0, z1), (x2, y1, z1), (x2, (y0 + y1) / 2, zr)]
f = [(0, 1, 2), (5, 4, 3), (0, 3, 4, 1), (1, 4, 5, 2), (2, 5, 3, 0)]
me = bpy.data.meshes.new(name)
me.from_pydata(v, [], f)
me.update()
o = bpy.data.objects.new(name, me)
bpy.context.collection.objects.link(o)
o.data.materials.append(material)
return o
def plane(name, pts, material, thick=0.09):
me = bpy.data.meshes.new(name)
me.from_pydata(pts, [], [(0, 1, 2, 3)])
me.update()
o = bpy.data.objects.new(name, me)
bpy.context.collection.objects.link(o)
o.data.materials.append(material)
s = o.modifiers.new("sol", "SOLIDIFY")
s.thickness = thick
return o
def main():
bpy.ops.wm.read_factory_settings(use_empty=True)
sc = bpy.context.scene
metal = mat("RAL7024", C_METAL, 0.38, 0.55)
wood = mat("Tikkurila_Olenenok", C_WOOD, 0.62)
deck = mat("Oil_Graphite", C_DECK, 0.55)
roof = mat("Roof", C_ROOF, 0.45, 0.25)
glass = mat("Glass", C_GLASS, glass=True)
pile = mat("Pile", C_PILE, 0.88)
ground = mat("Ground", C_GROUND, 0.95)
trim = mat("Trim", C_TRIM, 0.5)
path = mat("Path", C_PATH, 0.95)
# --- сваи ---
xs = [0.6, 2.4, 4.2, 6.0, 7.8, 9.6, 11.5]
ys = [0.35, 2.1, 4.25, 6.4, WID_Y - 0.35]
for x in xs:
for y in (0.35, WID_Y - 0.35):
bpy.ops.mesh.primitive_cylinder_add(radius=0.075, depth=Z0, location=(x, y, Z0 / 2))
bpy.context.object.data.materials.append(pile)
for y in ys[1:-1]:
for x in (0.35, LEN_X - 0.35):
bpy.ops.mesh.primitive_cylinder_add(radius=0.075, depth=Z0, location=(x, y, Z0 / 2))
bpy.context.object.data.materials.append(pile)
# --- пол и стены ---
box("floor", 0, LEN_X, 0, WID_Y, Z0 - 0.25, Z0 - 0.02, trim)
box("wall_south", 0, LEN_X, 0, T, Z0, Z1, metal)
box("wall_north", 0, LEN_X, WID_Y - T, WID_Y, Z0, Z1, metal)
box("wall_west", 0, T, T, WID_Y - T, Z0, Z1, wood)
box("wall_east", LEN_X - T, LEN_X, T, WID_Y - T, Z0, Z1, wood)
gable("gable_west", 0, 0, WID_Y, Z1, ZR, T, wood)
gable("gable_east", LEN_X - T, 0, WID_Y, Z1, ZR, T, wood)
# --- окна юга + дверь ---
for i, (a, b) in enumerate([(1.2, 2.6), (3.4, 4.8), (8.4, 9.9), (10.3, 11.5)]):
box("wg_%d" % i, a, b, -0.05, 0.0, Z0 + 0.95, Z0 + 2.15, glass)
box("wf_s%d" % i, a - 0.07, b + 0.07, -0.07, 0.02, Z0 + 0.88, Z0 + 0.95, trim)
box("wf_t%d" % i, a - 0.07, b + 0.07, -0.07, 0.02, Z0 + 2.15, Z0 + 2.22, trim)
box("wf_l%d" % i, a - 0.07, a, -0.07, 0.02, Z0 + 0.95, Z0 + 2.15, trim)
box("wf_r%d" % i, b, b + 0.07, -0.07, 0.02, Z0 + 0.95, Z0 + 2.15, trim)
box("door", 6.25, 7.25, -0.05, 0.0, Z0, Z0 + 2.15, trim)
box("door_frame", 6.15, 7.35, -0.07, 0.03, Z0, Z0 + 2.25, wood)
# окно западного фронтона (большое, к террасе)
box("wg_west", 0.02, 0.06, 2.4, 6.1, Z1 + 0.15, Z1 + 1.05, glass)
box("wf_west", -0.02, 0.08, 2.3, 6.2, Z1 + 0.05, Z1 + 1.15, trim)
# окно восточного фронтона
box("wg_east", LEN_X - 0.06, LEN_X - 0.02, 3.2, 5.3, Z1 + 0.2, Z1 + 0.95, glass)
# --- кровля ---
plane("roof_south", [(-OVER, -OVER, Z1), (LEN_X + OVER, -OVER, Z1),
(LEN_X + OVER, WID_Y / 2, ZR), (-OVER, WID_Y / 2, ZR)], roof)
plane("roof_north", [(-OVER, WID_Y + OVER, Z1), (LEN_X + OVER, WID_Y + OVER, Z1),
(LEN_X + OVER, WID_Y / 2, ZR), (-OVER, WID_Y / 2, ZR)], roof)
# труба
box("chimney", 2.3, 2.85, 3.1, 3.65, ZR - 0.2, ZR + 0.85, trim)
# --- терраса (запад) ---
box("deck", -TER_D, 0, 0, WID_Y, Z0 - 0.2, Z0 - 0.03, deck)
# столбы и ограждение
for y in (0.15, WID_Y / 2, WID_Y - 0.15):
box("post_%d" % int(y * 10), -TER_D + 0.1, -TER_D + 0.24, y - 0.07, y + 0.07, Z0 - 0.2, Z1, deck)
box("rail_top", -TER_D + 0.1, -TER_D + 0.24, 0.15, WID_Y - 0.15, Z0 + 0.98, Z0 + 1.06, deck)
box("rail_mid", -TER_D + 0.12, -TER_D + 0.22, 0.15, WID_Y - 0.15, Z0 + 0.5, Z0 + 0.56, deck)
for y in (0.15, WID_Y - 0.15):
box("rail_side_%d" % int(y * 10), -TER_D + 0.1, -0.1, y - 0.05, y + 0.05, Z0 + 0.5, Z0 + 0.56, deck)
# --- крыльцо (юг) ---
box("porch", 5.55, 6.75, -2.6, 0, Z0 - 0.2, Z0 - 0.03, deck)
for i in range(3):
box("step_%d" % i, 5.7, 6.6, -2.6 - 0.4 * (i + 1), -2.6 - 0.4 * i,
0.02, 0.52 - 0.17 * i, deck)
# --- тех.пом. (север) ---
box("tech", 4.9, 7.0, WID_Y, WID_Y + 2.1, Z0, Z1 - 0.25, metal)
box("tech_roof", 4.8, 7.1, WID_Y - 0.08, WID_Y + 2.2, Z1 - 0.25, Z1 - 0.15, roof)
# --- земля и дорожка ---
bpy.ops.mesh.primitive_plane_add(size=120, location=(LEN_X / 2, WID_Y / 2, 0))
bpy.context.object.data.materials.append(ground)
box("path", 5.55, 6.75, -14, -2.6, 0.005, 0.02, path)
# --- свет и мир ---
w = bpy.data.worlds.new("W")
sc.world = w
w.use_nodes = True
bg = w.node_tree.nodes["Background"]
bg.inputs[0].default_value = col("AEBEC9")
bg.inputs[1].default_value = 1.15
bpy.ops.object.light_add(type="SUN", location=(-10, -16, 20))
sun = bpy.context.object
sun.data.energy = 4.0
sun.data.angle = math.radians(2.0)
sun.rotation_euler = (math.radians(50), 0, math.radians(35))
# --- камера и рендер ---
cd = bpy.data.cameras.new("C")
cd.lens = 40
cam = bpy.data.objects.new("C", cd)
bpy.context.collection.objects.link(cam)
sc.camera = cam
sc.render.engine = "CYCLES"
sc.cycles.device = "CPU"
sc.cycles.samples = 128
sc.cycles.use_denoising = False
sc.render.resolution_x, sc.render.resolution_y = 1920, 1080
sc.view_settings.view_transform = "AgX"
def shot(name, loc, target):
cam.location = loc
cam.rotation_euler = (Vector(target) - 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)
shot("render-house-front.png", (LEN_X + 13, -15, 6.2), (LEN_X / 2 - 1.5, WID_Y / 2 - 1, 1.6))
shot("render-house-terrace.png", (-12, -11, 5.6), (LEN_X / 2 - 3.5, WID_Y / 2 + 1, 1.5))
print("done")
if __name__ == "__main__":
main()