diff options
Diffstat (limited to 'blender/bloques.py')
| -rw-r--r-- | blender/bloques.py | 306 |
1 files changed, 153 insertions, 153 deletions
diff --git a/blender/bloques.py b/blender/bloques.py index 19e9991..d88c7c4 100644 --- a/blender/bloques.py +++ b/blender/bloques.py @@ -1,39 +1,39 @@ # -*- coding: utf-8 -*- -"""BLOQUES - la pila armonica que sobresale de la mesa sin limite. +"""BLOQUES - the harmonic stack that overhangs the table without limit. -Blender disena (mesa, piso, bloques de madera) y exporta a glTF; Godot (Jolt) -decide que se sostiene y que se cae. Hay tres experimentos, cada uno en su -propio carril de la mesa (Y distinta) para que no se toquen; al renderizar se -trae al frente el que toca y se ocultan los otros. +Blender designs (table, floor, wooden blocks) and exports to glTF; Godot +(Jolt) decides what holds and what falls. There are three experiments, each +in its own lane of the table (different Y) so they do not touch; when +rendering, the current one is brought to the front and the others hidden. - A: la escalera "obvia", cada bloque corrido medio largo -> se cae - B: 5 bloques armonicos (1/2, 1/4, 1/6, 1/8, 1/10) -> se sostiene - C: 16 bloques armonicos; en el beat 8 se le apoya uno mas -> se cae todo + A: the "obvious" staircase, each block shifted half a length -> falls + B: 5 harmonic blocks (1/2, 1/4, 1/6, 1/8, 1/10) -> holds + C: 16 harmonic blocks; at beat 8 one more is put on top -> all fall -Los corrimientos armonicos se escalan por C_SEG = 0,95: asi el centro de masa -de cada subpila queda (1-C)/2 = 2,5 % de largo adentro del borde que la -sostiene, en todos los pisos. Sin ese margen, el solver decide al azar. +The harmonic shifts are scaled by SHIFT_SCALE = 0.95: that way the centre of +mass of each sub-stack stays (1-C)/2 = 2.5 % of a length inside the edge +holding it, on every level. Without that margin, the solver decides at random. """ import math, os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import bpy from base import * -NOMBRE = "bloques" -L, H, P = 1.0, 0.16, 0.50 # largo, alto, profundidad del bloque -C_SEG = 0.95 -C_TORRE = float(os.environ.get("T_C", C_SEG)) # la torre alta necesita mas margen con el solver -Z_PISO = -3.5 # mesa de 70 cm con bloques de 20 cm -GRAV = 49.0 # 9,81 / 0,2: bloques de 20 cm de verdad -CARRIL = {"a": 0.0, "b": 3.0, "c": 6.0, "x": 6.0} +NAME_KEY = "bloques" +L, H, P = 1.0, 0.16, 0.50 # block length, height, depth +SHIFT_SCALE = 0.95 +C_TOWER = float(os.environ.get("T_C", SHIFT_SCALE)) # the tall tower needs more margin with the solver +Z_FLOOR = -3.5 # 70 cm table with 20 cm blocks +GRAVITY = 49.0 # 9.81 / 0.2: real 20 cm blocks +LANE = {"a": 0.0, "b": 3.0, "c": 6.0, "x": 6.0} N_B, N_C = 5, 16 -DX_EXTRA = float(os.environ.get("T_DX", -0.45)) # donde se apoya el bloque de mas +DX_EXTRA = float(os.environ.get("T_DX", -0.45)) # where the extra block rests -def pila(n, c=C_SEG): - """Centros x de una pila armonica de n bloques (indice 0 = el de arriba).""" +def block_stack(n, c=SHIFT_SCALE): + """Centre x of a harmonic stack of n blocks (index 0 = the top one).""" xs = [0.0] * n - x = -L / 2 # la mesa hace de bloque n+1 + x = -L / 2 # the table acts as block n+1 for k in range(n, 0, -1): x += c * L / (2 * k) xs[k - 1] = x @@ -41,145 +41,145 @@ def pila(n, c=C_SEG): def z_de(n, k): - """Altura del centro del bloque k (0 = arriba) en una pila de n.""" + """Height of the centre of block k (0 = top) in a stack of n.""" return (n - 1 - k) * H + H / 2 -def construir(): - escena(muestras=int(os.environ.get("MUESTRAS", 24))) - mundo_estudio(fuerza=0.55) +def build_scene(): + scene_setup(samples=int(env("SAMPLES", "MUESTRAS", 24))) + studio_world(force=0.55) M = { - "bloque": madera("bloque", claro="#E2B880", oscuro="#B07A45", escala=5.0, rug=0.5), - "arriba": material("arriba", "ambar", rug=0.35), - "mesa": madera("mesa", claro="#6B4426", oscuro="#2E1A0C", escala=2.5, rug=0.35, - veta=(0.4, 1, 1)), - "piso": madera("piso", claro="#3A3F4B", oscuro="#1E222B", escala=1.5, rug=0.6, - veta=(0.3, 1, 1)), + "bloque": madera("bloque", light_c="#E2B880", dark="#B07A45", scale_to=5.0, rough=0.5), + "arriba": material("arriba", "ambar", rough=0.35), + "mesa": madera("mesa", light_c="#6B4426", dark="#2E1A0C", scale_to=2.5, rough=0.35, + grain=(0.4, 1, 1)), + "piso": madera("piso", light_c="#3A3F4B", dark="#1E222B", scale_to=1.5, rough=0.6, + grain=(0.3, 1, 1)), "invisible": material("inv", "gris"), } - fis = [] - mesa = caja("caja_mesa", (6.0, 9.0, 0.3), (-3.0, 3.0, -0.15), M["invisible"]) - piso = caja("caja_piso", (30.0, 30.0, 0.2), (0.0, 3.0, Z_PISO - 0.1), M["invisible"]) - mesa.hide_render = piso.hide_render = True - fis += [mesa, piso] - # lo que se ve de la mesa y el piso: solo el carril del frente - caja("mesa_v", (6.0, 2.4, 0.3), (-3.0, 0.4, -0.15), M["mesa"]) - m_pata = metal("pata", "#2A2F3A", rug=0.4) - caja("pata", (0.14, 0.14, -Z_PISO - 0.3), (-0.35, -0.55, (Z_PISO - 0.3) / 2), m_pata) - caja("pata2", (0.14, 0.14, -Z_PISO - 0.3), (-0.35, 1.3, (Z_PISO - 0.3) / 2), m_pata) - caja("piso_v", (14.0, 5.0, 0.2), (1.0, 1.0, Z_PISO - 0.1), M["piso"]) + phys = [] + table = box_obj("box_mesa", (6.0, 9.0, 0.3), (-3.0, 3.0, -0.15), M["invisible"]) + floor_obj = box_obj("box_piso", (30.0, 30.0, 0.2), (0.0, 3.0, Z_FLOOR - 0.1), M["invisible"]) + table.hide_render = floor_obj.hide_render = True + phys += [table, floor_obj] + # what shows of the table and the floor: only the front lane + box_obj("mesa_v", (6.0, 2.4, 0.3), (-3.0, 0.4, -0.15), M["mesa"]) + m_leg = metal("pata", "#2A2F3A", rough=0.4) + box_obj("pata", (0.14, 0.14, -Z_FLOOR - 0.3), (-0.35, -0.55, (Z_FLOOR - 0.3) / 2), m_leg) + box_obj("pata2", (0.14, 0.14, -Z_FLOOR - 0.3), (-0.35, 1.3, (Z_FLOOR - 0.3) / 2), m_leg) + box_obj("piso_v", (14.0, 5.0, 0.2), (1.0, 1.0, Z_FLOOR - 0.1), M["piso"]) bl = {} - # A: escalera "obvia" + # A: "obvious" staircase for k in range(5): x = -0.25 + 0.5 * (4 - k) - bl[f"a{k}"] = caja(f"bloque_a{k}", (L, P, H), (x, CARRIL["a"], z_de(5, k)), M["bloque"]) - # B: armonica de 5 - for k, x in enumerate(pila(N_B)): - bl[f"b{k}"] = caja(f"bloque_b{k}", (L, P, H), (x, CARRIL["b"], z_de(N_B, k)), + bl[f"a{k}"] = box_obj(f"block_a{k}", (L, P, H), (x, LANE["a"], z_de(5, k)), M["bloque"]) + # B: harmonic stack of 5 + for k, x in enumerate(block_stack(N_B)): + bl[f"b{k}"] = box_obj(f"block_b{k}", (L, P, H), (x, LANE["b"], z_de(N_B, k)), M["arriba"] if k == 0 else M["bloque"]) - # C: armonica de 16 y el bloque de mas, esperando arriba - xc = pila(N_C, C_TORRE) + # C: harmonic stack of 16 and the extra block, waiting on top + xc = block_stack(N_C, C_TOWER) for k, x in enumerate(xc): - bl[f"c{k:02d}"] = caja(f"bloque_c{k:02d}", (L, P, H), (x, CARRIL["c"], z_de(N_C, k)), + bl[f"c{k:02d}"] = box_obj(f"block_c{k:02d}", (L, P, H), (x, LANE["c"], z_de(N_C, k)), M["arriba"] if k == 0 else M["bloque"]) - bl["x"] = caja("bloque_x", (L, P, H), (xc[0] + DX_EXTRA, CARRIL["x"], z_de(N_C, 0) + H + 0.005), + bl["x"] = box_obj("block_x", (L, P, H), (xc[0] + DX_EXTRA, LANE["x"], z_de(N_C, 0) + H + 0.005), M["bloque"]) - fis += list(bl.values()) - return dict(fis=fis, bl=bl, M=M) + phys += list(bl.values()) + return dict(phys=phys, bl=bl, M=M) -def eventos(T): +def events(T): t = lambda f: (f - 1) / FPS - fa = T.rango(1)[0] + int(0.55 * (T.rango(1)[1] - T.rango(1)[0])) - fx = T.rango(8)[0] + int(0.70 * (T.rango(8)[1] - T.rango(8)[0])) - return {"a": fa, "b": T.rango(4)[0], "c": T.rango(7)[0], "x": fx}, [ - {"t": t(fa), "accion": "soltar", "prefijo": "bloque_a"}, - {"t": t(T.rango(4)[0]), "accion": "soltar", "prefijo": "bloque_b"}, - {"t": t(T.rango(7)[0]), "accion": "soltar", "prefijo": "bloque_c"}, - {"t": t(fx), "accion": "soltar", "prefijo": "bloque_x"}, + fa = T.span(1)[0] + int(0.55 * (T.span(1)[1] - T.span(1)[0])) + fx = T.span(8)[0] + int(0.70 * (T.span(8)[1] - T.span(8)[0])) + return {"a": fa, "b": T.span(4)[0], "c": T.span(7)[0], "x": fx}, [ + {"t": t(fa), "action": "release", "prefix": "block_a"}, + {"t": t(T.span(4)[0]), "action": "release", "prefix": "block_b"}, + {"t": t(T.span(7)[0]), "action": "release", "prefix": "block_c"}, + {"t": t(fx), "action": "release", "prefix": "block_x"}, ] -def simular(T, obj): - _, ev = eventos(T) +def simulate(T, obj): + _, ev = events(T) cfg = { - "duracion": T.n_frames / FPS + 0.2, - "gravedad": GRAV, - "escala": float(os.environ.get("T_ESC", 5.0)), + "duration": T.n_frames / FPS + 0.2, + "gravity": GRAVITY, + "scale": float(env("T_SCALE", None, 5.0)), "hz": int(os.environ.get("T_HZ", 1920)), - "congelados": ["bloque_"], - "reglas": { - "bloque_": {"friccion": 0.55, "rebote": 0.08, "densidad": 600}, - "caja_": {"friccion": 0.6, "rebote": 0.05}, + "frozen": ["block_"], + "rules": { + "block_": {"friction": 0.55, "bounce": 0.08, "density": 600}, + "box_": {"friction": 0.6, "bounce": 0.05}, }, - "eventos": ev, + "events": ev, } - correr_godot(exportar_fisica(NOMBRE, obj["fis"], cfg)) + run_godot(export_physics(NAME_KEY, obj["phys"], cfg)) def main(): - T = Tiempo(NOMBRE) - obj = construir() - if os.environ.get("MODO") == "sim": - simular(T, obj) + T = Timeline(NAME_KEY) + obj = build_scene() + if env("MODE", "MODO") == "sim": + simulate(T, obj) return - idx, D = cargar_sim(NOMBRE) + idx, D = load_sim(NAME_KEY) bl, M = obj["bl"], obj["M"] - suelta, _ = eventos(T) - xc = pila(N_C, C_TORRE) + release_at, _ = events(T) + xc = block_stack(N_C, C_TOWER) - # reporte: que quedo en pie + # report: what is still standing for g, n in (("a", 5), ("b", N_B), ("c", N_C)): - nom = [k for k in bl if k.startswith(g) and k != "x"] - zmin = min(D[-1, idx[bl[k].name], 2] for k in nom) - zmin_antes = min(D[min(len(D) - 1, suelta["x"] - 5), idx[bl[k].name], 2] for k in nom) - print(f"[{NOMBRE}] pila {g}: z minima al final {zmin:+.2f} " - f"(antes del bloque extra {zmin_antes:+.2f})") - print(f"[{NOMBRE}] B: el de arriba arranca en x={pila(N_B)[0] - L / 2:+.3f} " - f"(sobresale {pila(N_B)[0] + L / 2:.3f}); C sobresale {xc[0] + L / 2:.3f}") + nm_key = [k for k in bl if k.startswith(g) and k != "x"] + zmin = min(D[-1, idx[bl[k].name], 2] for k in nm_key) + zmin_before = min(D[min(len(D) - 1, release_at["x"] - 5), idx[bl[k].name], 2] for k in nm_key) + print(f"[{NAME_KEY}] stack {g}: minimum z at the end {zmin:+.2f} " + f"(before the extra block {zmin_before:+.2f})") + print(f"[{NAME_KEY}] B: the top one starts at x={block_stack(N_B)[0] - L / 2:+.3f} " + f"(overhangs {block_stack(N_B)[0] + L / 2:.3f}); C overhangs {xc[0] + L / 2:.3f}") - # --- dibujos ------------------------------------------------------------- - m_borde = material("borde", "celeste", emis=2.5) - borde = curva_poly("borde", [[(0, -0.30, z0), (0, -0.30, z0 + 0.09)] + # --- drawings ------------------------------------------------------------- + m_edge = material("borde", "celeste", emit=2.5) + edge = curve_poly("borde", [[(0, -0.30, z0), (0, -0.30, z0 + 0.09)] for z0 in [0.02 + 0.16 * i for i in range(18)]], - grosor=0.012, mat=m_borde) - etiquetas = [] - xb = pila(N_B) + thickness_px=0.012, mat=m_edge) + label_list = [] + xb = block_stack(N_B) for k in range(N_B): - num = texto(f"1/{2 * (k + 1)}", tam=0.15, color="ambar") - # el corrimiento de este bloque respecto del de abajo, a su derecha + num = txt_m(f"1/{2 * (k + 1)}", size_u=0.15, color="ambar") + # shift of this block relative to the one below, to its right num.location = (xb[k] + L / 2 + 0.16, -0.30, z_de(N_B, k)) - etiquetas.append(num) - aire = curva_poly("aire", [[(0, -0.3, 0.9), (xb[0] - L / 2, -0.3, 0.9)]], - grosor=0.02, mat=material("aire", "rosa", emis=3.0)) - aire_t = texto("en el aire", tam=0.16, color="rosa") - aire_t.location = (0.45, -0.3, 1.08) - # centros de masa de cada subpila, sobre el borde que la sostiene + label_list.append(num) + air = curve_poly("aire", [[(0, -0.3, 0.9), (xb[0] - L / 2, -0.3, 0.9)]], + thickness_px=0.02, mat=material("aire", "rosa", emit=3.0)) + air_t = txt_m("en el aire", size_u=0.16, color="rosa") + air_t.location = (0.45, -0.3, 1.08) + # centres of mass of each sub-stack, over the edge that holds it cms = [] for k in range(1, N_B + 1): x_cm = sum(xb[:k]) / k - e = esfera(f"cm{k}", 0.045, material(f"mcm{k}", "ambar", emis=3.0)) + e = sphere(f"cm{k}", 0.045, material(f"mcm{k}", "ambar", emit=3.0)) e.location = (x_cm, -0.30, z_de(N_B, k - 1) - H / 2) cms.append(e) - cuenta = texto("16 bloques", tam=0.22, color="blanco") - sobre = texto("sobresale 1,6 bloques", tam=0.18, color="ambar") - regla = curva_poly("regla", [[(0, -0.3, 2.78), (xc[0] + L / 2, -0.3, 2.78)]], - grosor=0.018, mat=material("regla", "ambar", emis=3.0)) + count = txt_m("16 bloques", size_u=0.22, color="blanco") + onto = txt_m("sobresale 1,6 bloques", size_u=0.18, color="ambar") + ruler = curve_poly("regla", [[(0, -0.3, 2.78), (xc[0] + L / 2, -0.3, 2.78)]], + thickness_px=0.018, mat=material("regla", "ambar", emit=3.0)) - lente = 50.0 - cam = camara((0, -8, 1), (0, 0, 0), lente=lente) + lens = 50.0 + cam = camera_obj((0, -8, 1), (0, 0, 0), lens=lens) cam.data.sensor_fit = 'VERTICAL' cam.data.sensor_height = 36.0 - luz("key", 'AREA', (-3.0, -4.0, 5.0), 1400, "blanco", tam=4.0, mira=(0.5, 0, 0.8)) - luz("fill", 'AREA', (4.0, -3.0, 0.5), 350, "blanco", tam=4.0, mira=(0.5, 0, 0.5)) - luz("rim", 'AREA', (1.0, 4.0, 4.0), 600, "blanco", tam=3.0, mira=(0.5, 0, 1.0)) + light_obj("key", 'AREA', (-3.0, -4.0, 5.0), 1400, "blanco", size_u=4.0, sight=(0.5, 0, 0.8)) + light_obj("fill", 'AREA', (4.0, -3.0, 0.5), 350, "blanco", size_u=4.0, sight=(0.5, 0, 0.5)) + light_obj("rim", 'AREA', (1.0, 4.0, 4.0), 600, "blanco", size_u=3.0, sight=(0.5, 0, 1.0)) - # (frame, x centro, z del contenido, alto visible) - r = T.rango + # (frame, centre x, content z, visible height) + r = T.span tomas = [ (1, 0.55, 0.40, 5.6), - (suelta["a"] + 5, 0.55, 0.40, 5.6), + (release_at["a"] + 5, 0.55, 0.40, 5.6), (r(1)[1] + 10, 0.9, -1.7, 7.4), (r(2)[0] + 18, 0.45, 0.45, 4.8), (r(5)[1], 0.45, 0.45, 4.8), @@ -190,7 +190,7 @@ def main(): (T.n_frames, 1.2, -1.4, 8.6), ] - def camara_en(f): + def camera_at(f): for (f0, *a), (f1, *b) in zip(tomas, tomas[1:]): if f0 <= f <= f1: u = suave((f - f0) / max(1, f1 - f0)) @@ -198,35 +198,35 @@ def main(): return tomas[-1][1:] def n_c(f): - """Cuantos bloques tiene la pila C mientras se arma (beat 6).""" + """How many blocks stack C has while it is being built (beat 6).""" return 5 + (N_C - 5) * suave(T.p(f, 6) / 0.8) - def actualizar(f): + def refresh(f): i = min(f - 1, len(D) - 1) - grupo = "a" if f < r(2)[0] + 10 else "b" if f < r(6)[0] else "c" + group_m = "a" if f < r(2)[0] + 10 else "b" if f < r(6)[0] else "c" for k, ob in bl.items(): g = "x" if k == "x" else k[0] - vis = (g == grupo) or (g == "x" and grupo == "c") - ob.hide_render = not vis - if not vis: + visible = (g == group_m) or (g == "x" and group_m == "c") + ob.hide_render = not visible + if not visible: continue - fila = list(D[i, idx[ob.name]]) - fila[1] -= CARRIL[g] - poner_pose(ob, fila) - if g == "a" and f < suelta["a"]: - # de la pila prolija a la escalera, antes de soltarla + row = list(D[i, idx[ob.name]]) + row[1] -= LANE[g] + set_pose(ob, row) + if g == "a" and f < release_at["a"]: + # from the neat stack to the staircase, before releasing it kk = int(k[1:]) u = suave(T.p(f, 1) / 0.45) - ob.location.x = mezcla(-0.45, fila[0], u) - elif g == "b" and f < suelta["b"]: - # se arma desde arriba: cada bloque entra desde la izquierda + ob.location.x = mix_m(-0.45, row[0], u) + elif g == "b" and f < release_at["b"]: + # built from the top: each block comes in from the left kk = int(k[1:]) u = suave((T.p(f, 3) - kk * 0.17) / 0.16) if f >= r(3)[0] else 0.0 if f < r(3)[0]: u = suave(T.p(f, 2) / 0.6) if kk == 0 else 0.0 - ob.location.x = mezcla(fila[0] - 3.5, fila[0], u) + ob.location.x = mix_m(row[0] - 3.5, row[0], u) ob.hide_render = u <= 0.001 - elif g == "c" and f < suelta["c"]: + elif g == "c" and f < release_at["c"]: kk = int(k[1:]) n = n_c(f) n0 = int(math.floor(n)) @@ -234,46 +234,46 @@ def main(): def pos(nn): if kk >= nn: return None - return pila(nn, C_TORRE)[kk], z_de(nn, kk) + return block_stack(nn, C_TOWER)[kk], z_de(nn, kk) p0, p1 = pos(n0), pos(min(N_C, n0 + 1)) if p0 is None and p1 is None: ob.hide_render = True continue - if p0 is None: # el bloque nuevo entra por abajo desde la izquierda + if p0 is None: # the new block comes in from below on the left p0 = (p1[0] - 3.0, p1[1]) x = p0[0] + (p1[0] - p0[0]) * u if p1 else p0[0] z = p0[1] + (p1[1] - p0[1]) * u if p1 else p0[1] ob.location = (x, 0.0, z) - elif g == "x" and f < suelta["x"]: + elif g == "x" and f < release_at["x"]: u = suave((T.p(f, 8) - 0.15) / 0.45) ob.hide_render = u <= 0.001 - ob.location.z = fila[2] + 2.0 * (1 - u) + ob.location.z = row[2] + 2.0 * (1 - u) - # dibujos - v_borde = T.p(f, 3) > 0 and f < r(9)[0] - borde.hide_render = not v_borde - for k, t in enumerate(etiquetas): + # drawings + v_edge = T.p(f, 3) > 0 and f < r(9)[0] + edge.hide_render = not v_edge + for k, t in enumerate(label_list): t.hide_render = not (r(3)[0] + (k * 0.17 + 0.08) * (r(3)[1] - r(3)[0]) <= f < r(6)[0]) - v_aire = r(4)[0] + 20 <= f < r(6)[0] - aire.hide_render = aire_t.hide_render = not v_aire + v_air = r(4)[0] + 20 <= f < r(6)[0] + air.hide_render = air_t.hide_render = not v_air for k, e in enumerate(cms): e.hide_render = not (r(5)[0] + k * 8 <= f < r(6)[0]) v_c = r(6)[0] + 5 <= f < r(8)[1] - cuenta.hide_render = not v_c + count.hide_render = not v_c n = n_c(f) - cuenta.data.body = f"{int(round(n))} bloques" + count.data.body = f"{int(round(n))} bloques" top_z = z_de(int(round(n)), 0) + H / 2 - cuenta.location = (-0.45, -0.3, top_z + 0.35) - v_regla = r(7)[0] <= f < r(8)[1] - regla.hide_render = sobre.hide_render = not v_regla - sobre.location = (1.2, -0.3, 2.98) + count.location = (-0.45, -0.3, top_z + 0.35) + v_ruler = r(7)[0] <= f < r(8)[1] + ruler.hide_render = onto.hide_render = not v_ruler + onto.location = (1.2, -0.3, 2.98) - cx, cz, h = camara_en(f) - dist = h * lente / 36.0 - zc = cz - 0.10 * h # el contenido queda arriba de los subtitulos - apuntar(cam, (cx - 0.16 * h, -dist, zc + 0.16 * h), (cx, 0, zc)) + cx, cz, h = camera_at(f) + dist = h * lens / 36.0 + zc = cz - 0.10 * h # the content stays above the subtitles + aim_at(cam, (cx - 0.16 * h, -dist, zc + 0.16 * h), (cx, 0, zc)) - render_secuencia(NOMBRE, T, actualizar) + render_sequence(NAME_KEY, T, refresh) main() |