Files
buryan-infra/ops/home/render_blender.py
T

230 lines
9.3 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.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Blender headless: рендер комнаты по данным дома Барнева.
Запуск:
blender -b -P render_blender.py -- <room_key>
Выход:
ops/home/blender/<room_key>.png
"""
import json
import math
import os
import sys
import bpy
import mathutils
ROOMS = {
"bedroom": dict(name="Спальня", w=3.3, d=3.5, items=[
("Кровать", 0.75, 1.0, 1.6, 2.0, 0.5, "textile"),
("Изголовье", 0.75, 0.95, 1.6, 0.12, 0.95, "wood_dark"),
("Тумба", 0.42, 0.95, 0.33, 0.42, 0.48, "wood"),
("Тумба", 2.36, 0.95, 0.33, 0.42, 0.48, "wood"),
("Гардероб", 2.45, 0.35, 0.7, 0.85, 2.15, "wood_dark"),
("Стол", 0.28, 2.55, 1.25, 0.42, 0.74, "wood"),
("ТВ", 2.62, 0.22, 0.55, 0.06, 0.68, "black"),
("Ковёр", 1.0, 1.2, 1.4, 1.6, 0.02, "textile_soft"),
]),
"kitchen-living": dict(name="Кухня-гостиная", w=6.0, d=4.6, items=[
("Кухня", 0.15, 0.16, 3.0, 0.62, 0.9, "wood"),
("Столешница", 0.15, 0.16, 3.0, 0.62, 0.04, "stone"),
("Холодильник", 2.95, 0.16, 0.62, 0.62, 1.9, "white"),
("Вытяжка", 1.35, 0.2, 0.7, 0.45, 0.4, "metal"),
("Стол", 3.5, 2.1, 1.5, 0.95, 0.74, "wood"),
("Диван", 0.45, 3.35, 2.3, 0.95, 0.72, "textile"),
("ТВ 75", 4.75, 2.6, 1.55, 0.07, 0.92, "black"),
("Стеллаж", 5.3, 0.3, 0.55, 2.1, 2.0, "wood_dark"),
("Станция", 3.1, 3.7, 0.32, 0.32, 0.26, "dark"),
]),
"office": dict(name="Кабинет", w=3.2, d=3.5, items=[
("Стол", 0.25, 1.15, 1.75, 0.7, 0.74, "wood"),
("Монитор 1", 0.5, 1.3, 0.55, 0.05, 0.42, "black"),
("Монитор 2", 1.15, 1.3, 0.55, 0.05, 0.42, "black"),
("Кресло", 0.95, 2.15, 0.6, 0.6, 0.95, "textile"),
("ТВ TCL", 2.62, 0.24, 0.48, 0.06, 0.64, "black"),
("PS5", 2.55, 0.95, 0.3, 0.34, 0.38, "white"),
("Кондиционер", 0.2, 0.2, 0.9, 0.24, 0.3, "white"),
("Шкаф", 2.42, 0.3, 0.6, 0.75, 2.1, "wood_dark"),
]),
"bathroom": dict(name="Санузел", w=2.5, d=2.5, items=[
("Ванна", 0.14, 0.14, 1.7, 0.78, 0.52, "white"),
("Душ-поддон", 1.94, 0.14, 0.42, 0.9, 0.06, "metal"),
("Унитаз", 0.45, 1.85, 0.4, 0.52, 0.4, "white"),
("Раковина", 1.5, 1.6, 0.7, 0.42, 0.82, "white"),
("Шкаф-зеркало", 1.55, 2.22, 0.7, 0.16, 0.85, "metal"),
("Стиралка", 1.05, 0.2, 0.6, 0.6, 0.85, "white"),
]),
"corridor": dict(name="Коридор", w=1.6, d=5.2, items=[
("Шкаф", 0.14, 0.3, 0.55, 1.6, 2.1, "wood_dark"),
("Скамья", 0.14, 2.15, 0.42, 0.9, 0.42, "wood"),
("Зеркало", 1.18, 1.2, 0.06, 0.9, 1.6, "metal"),
("Тумба", 1.1, 3.5, 0.34, 0.6, 0.78, "wood"),
("Коврик", 0.35, 4.1, 0.9, 0.6, 0.02, "textile"),
]),
"danina": dict(name="Данина (детская)", w=3.7, d=3.9, items=[
("Кровать", 0.35, 0.4, 1.5, 2.0, 0.5, "textile"),
("Стол", 2.2, 0.35, 1.2, 0.52, 0.74, "wood"),
("Диван", 0.35, 3.0, 1.95, 0.85, 0.72, "textile"),
("ТВ TCL", 2.55, 2.35, 0.9, 0.06, 0.6, "black"),
("Антресоль", 2.35, 1.35, 1.25, 1.35, 0.14, "wood_dark"),
("Лестница", 2.05, 1.45, 0.28, 1.15, 0.92, "metal"),
("Шкаф", 3.28, 0.3, 0.34, 1.0, 2.0, "wood_dark"),
]),
"laundry": dict(name="Прачечная", w=1.8, d=1.9, items=[
("Стиралка", 0.16, 0.16, 0.6, 0.6, 0.85, "white"),
("Сушилка", 0.82, 0.16, 0.6, 0.6, 0.85, "white"),
("Полка", 0.16, 1.3, 1.45, 0.36, 1.8, "wood_dark"),
]),
}
MATERIALS = {
"wood": ((0.42, 0.29, 0.18, 1), 0.55),
"wood_dark": ((0.28, 0.19, 0.12, 1), 0.6),
"textile": ((0.22, 0.28, 0.36, 1), 0.9),
"textile_soft": ((0.30, 0.33, 0.38, 1), 0.95),
"white": ((0.88, 0.90, 0.92, 1), 0.35),
"metal": ((0.62, 0.66, 0.70, 1), 0.22),
"black": ((0.05, 0.06, 0.07, 1), 0.4),
"dark": ((0.16, 0.18, 0.21, 1), 0.6),
"stone": ((0.55, 0.56, 0.58, 1), 0.3),
"wall": ((0.66, 0.68, 0.71, 1), 0.9),
"floor": ((0.36, 0.26, 0.18, 1), 0.5),
}
def get_mat(key):
name = "M_" + key
if name in bpy.data.materials:
return bpy.data.materials[name]
color, rough = MATERIALS.get(key, MATERIALS["dark"])
m = bpy.data.materials.new(name)
m.use_nodes = True
nt = m.node_tree
bsdf = nt.nodes.get("Principled BSDF")
bsdf.inputs["Base Color"].default_value = color
bsdf.inputs["Roughness"].default_value = rough
# процедурная текстура для дерева и пола
if key in ("wood", "wood_dark", "floor"):
tex = nt.nodes.new("ShaderNodeTexNoise")
tex.inputs["Scale"].default_value = 22.0 if key != "floor" else 30.0
tex.inputs["Detail"].default_value = 6.0
ramp = nt.nodes.new("ShaderNodeValToRGB")
r, g, b, _ = color
ramp.color_ramp.elements[0].color = (r * 0.72, g * 0.72, b * 0.72, 1)
ramp.color_ramp.elements[1].color = (min(1, r * 1.25), min(1, g * 1.25), min(1, b * 1.25), 1)
mapping = nt.nodes.new("ShaderNodeMapping")
mapping.inputs["Scale"].default_value = (1.0, 1.0, 0.06) if key == "floor" else (0.08, 1.0, 1.0)
coord = nt.nodes.new("ShaderNodeTexCoord")
nt.links.new(coord.outputs["Object"], mapping.inputs["Vector"])
nt.links.new(mapping.outputs["Vector"], tex.inputs["Vector"])
nt.links.new(tex.outputs["Fac"], ramp.inputs["Fac"])
nt.links.new(ramp.outputs["Color"], bsdf.inputs["Base Color"])
bump = nt.nodes.new("ShaderNodeBump")
bump.inputs["Strength"].default_value = 0.06
nt.links.new(tex.outputs["Fac"], bump.inputs["Height"])
nt.links.new(bump.outputs["Normal"], bsdf.inputs["Normal"])
return m
def add_box(name, x, y, z, w, d, h, mat):
"""Куб с размерами (w,d,h), центр в (x+w/2, y+d/2, z+h/2)."""
bpy.ops.mesh.primitive_cube_add(size=1.0,
location=(x + w / 2, y + d / 2, z + h / 2))
ob = bpy.context.active_object
ob.name = name
ob.scale = (w, d, h)
bpy.ops.object.transform_apply(location=False, rotation=False, scale=True)
ob.data.materials.append(get_mat(mat))
return ob
def build_room(spec):
bpy.ops.wm.read_factory_settings(use_empty=True)
w, d = spec["w"], spec["d"]
# пол
add_box("Floor", 0, 0, -0.06, w, d, 0.06, "floor")
# стены (задние две) + плинтус
add_box("Wall_X", 0, 0, 0, 0.12, d + 0.12, 2.7, "wall")
add_box("Wall_Y", 0, 0, 0, w + 0.12, 0.12, 2.7, "wall")
for name, x, y, iw, idd, ih, mat in spec["items"]:
add_box(name, x, y, 0.0, iw, idd, ih, mat)
# камера: изометрия под 45°, ортографическая
cam_data = bpy.data.cameras.new("Cam")
cam_data.type = "ORTHO"
diag = math.sqrt(w * w + d * d)
cam_data.ortho_scale = diag * 1.18
cam = bpy.data.objects.new("Cam", cam_data)
bpy.context.collection.objects.link(cam)
cx, cy = w / 2, d / 2
dist = max(w, d) * 3.0
az = math.radians(52)
el = math.radians(30)
cam.location = (cx + dist * math.cos(el) * math.cos(az),
cy + dist * math.cos(el) * math.sin(az),
dist * math.sin(el) + 0.6)
direction = mathutils.Vector((cx, cy, 0.9)) - cam.location
cam.rotation_euler = direction.to_track_quat("-Z", "Y").to_euler()
bpy.context.scene.camera = cam
# свет: большая area сверху + мягкий заполняющий
light_data = bpy.data.lights.new("Key", type="AREA")
light_data.energy = 220
light_data.size = max(w, d) * 1.2
light = bpy.data.objects.new("Key", light_data)
light.location = (cx, cy, 2.55)
bpy.context.collection.objects.link(light)
fill_data = bpy.data.lights.new("Fill", type="AREA")
fill_data.energy = 90
fill_data.size = max(w, d) * 1.6
fill = bpy.data.objects.new("Fill", fill_data)
fill.location = (cx + 2.2, cy - 2.2, 1.9)
bpy.context.collection.objects.link(fill)
# мир
world = bpy.data.worlds.new("W")
world.use_nodes = True
bg = world.node_tree.nodes["Background"]
bg.inputs[0].default_value = (0.045, 0.055, 0.07, 1)
bg.inputs[1].default_value = 0.55
bpy.context.scene.world = world
# рендер
sc = bpy.context.scene
sc.render.engine = "CYCLES"
sc.cycles.device = "CPU"
sc.cycles.samples = 128
sc.cycles.use_denoising = False
sc.render.resolution_x = 1600
sc.render.resolution_y = 1200
sc.render.resolution_percentage = 100
sc.render.film_transparent = False
sc.view_settings.view_transform = "Filmic"
def main():
argv = sys.argv
key = None
if "--" in argv:
args = argv[argv.index("--") + 1:]
if args:
key = args[0]
key = key or "bedroom"
spec = ROOMS[key]
out_dir = "/root/buryan-infra/ops/home/blender"
os.makedirs(out_dir, exist_ok=True)
build_room(spec)
bpy.context.scene.render.filepath = os.path.join(out_dir, key + ".png")
bpy.ops.render.render(write_still=True)
print("RENDERED:", bpy.context.scene.render.filepath)
if __name__ == "__main__":
main()