cntrl/tools/blender/import_level.py
dandovlatov 849b34922d FBC: NIGHT SHIFT - two-handed agent weapon grip, standalone level editor and validation
- Pin both wrists to the rifle's real grip surfaces via a shared IK pass in
  every animation clip; hand bones follow the barrel and Fire recoil pivots
  the weapon/arms as one assembly.
- Nudge the weapon forward so the stock clears the vest; make the generator
  reproducible (clears stale actions) and regenerate .blend, GLB and renders.
- Add standalone level editor with JSON level data, validation and baking,
  plus editor docs and regression fixtures.
2026-11-24 21:34:30 +04:00

206 lines
7.3 KiB
Python

import bpy
import json
import math
import os
from mathutils import Vector
REPO = "/home/dandovlatov/Documents/cntrl"
LEVEL = os.environ.get("CNTRL_LEVEL", REPO + "/assets/levels/floor01.json")
OUTPUT = os.environ.get("CNTRL_OUT", REPO + "/assets/blender/fbc_level01_blockout.blend")
ACTOR_PATHS = {
"TV": "assets/models/props/tv.glb",
"Hotline": "assets/models/props/hotline.glb",
"Pedestal": "assets/models/props/pedestal.glb",
"Duck": "assets/models/props/duck.glb",
"Debris": "assets/models/props/debris.glb",
"GarbageCan": "assets/models/props/garbage_can.glb",
"DocumentStack": "assets/models/props/document_stack.glb",
"LooseSheet": "assets/models/props/loose_sheet.glb",
"OfficeDesk": "assets/models/props/desk.glb",
"Computer": "assets/models/props/computer.glb",
"OfficeChair": "assets/models/props/chair.glb",
"Partition": "assets/models/props/partition.glb",
"MeetingTable": "assets/models/props/meeting_table.glb",
"Sofa": "assets/models/props/sofa.glb",
"FloorLamp": "assets/models/props/lamp.glb",
"Plant": "assets/models/props/plant.glb",
"Cabinet": "assets/models/props/cabinet.glb",
"Crate": "assets/models/props/crate.glb",
"ArchiveShelf": "assets/models/props/archive_shelf.glb",
"ThermoFrame": "assets/models/props/thermo_frame.glb",
"ThermoPanel": "assets/models/props/thermo_panel.glb",
}
KIND_COLORS = {
"Gate": (0.36, 0.56, 0.75, 1),
"Plaza": (0.50, 0.16, 0.20, 1),
"Projector": (0.78, 0.62, 0.14, 1),
"OfficeSuite": (0.20, 0.42, 0.36, 1),
"Silos": (0.26, 0.44, 0.60, 1),
"Stairwell": (0.60, 0.32, 0.54, 1),
"Side": (0.38, 0.38, 0.42, 1),
}
def material(name, color):
m = bpy.data.materials.get(name)
if m is None:
m = bpy.data.materials.new(name)
m.diffuse_color = color
m.use_nodes = True
bsdf = m.node_tree.nodes.get("Principled BSDF")
if bsdf:
bsdf.inputs["Base Color"].default_value = color
bsdf.inputs["Roughness"].default_value = 0.85
return m
def collection(name):
c = bpy.data.collections.get(name)
if c is None:
c = bpy.data.collections.new(name)
bpy.context.scene.collection.children.link(c)
return c
def move_to(obj, col):
for old in list(obj.users_collection):
old.objects.unlink(obj)
col.objects.link(obj)
def box(name, center, size, mat, col):
bpy.ops.mesh.primitive_cube_add(size=1, location=center)
obj = bpy.context.object
obj.name = name
obj.dimensions = size
obj.data.materials.append(mat)
move_to(obj, col)
return obj
def empty(name, location, col):
obj = bpy.data.objects.new(name, None)
obj.empty_display_type = "SPHERE"
obj.empty_display_size = 1.5
obj.location = location
col.objects.link(obj)
return obj
def segments(a0, a1, gaps):
out = []
cur = a0
for g in gaps:
if g[0] > cur:
out.append((cur, min(g[0], a1)))
cur = max(cur, g[1])
if cur < a1:
out.append((cur, a1))
return out
def main():
with open(LEVEL, "r") as handle:
level = json.load(handle)
scale = level["scale"]
half_w = level["map_width"] * 0.5
half_h = level["map_height"] * 0.5
def to_world(x, y):
return ((x - half_w) * scale, (half_h - y) * scale)
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete(use_global=False)
for col in list(bpy.data.collections):
if col.name.startswith("LEVEL_"):
bpy.data.collections.remove(col)
walls_col = collection("LEVEL_Walls")
rooms_col = collection("LEVEL_Rooms")
props_col = collection("LEVEL_Props")
anchors_col = collection("LEVEL_Anchors")
wall_mat = material("LEVEL_Wall", (0.62, 0.63, 0.66, 1))
door_mat = material("LEVEL_Door", (0.78, 0.22, 0.18, 1))
for index, w in enumerate(level["walls"]):
for span in segments(w["from"], w["to"], w["gaps"]):
if w["axis"] == 0:
a = to_world(span[0], w["coord"])
b = to_world(span[1], w["coord"])
else:
a = to_world(w["coord"], span[0])
b = to_world(w["coord"], span[1])
x0, x1 = min(a[0], b[0]), max(a[0], b[0])
z0, z1 = min(a[1], b[1]), max(a[1], b[1])
box("Wall_%03d" % index, ((x0 + x1) * 0.5, -(z0 + z1) * 0.5, w["height"] * 0.5),
(max(x1 - x0, 0.05), max(z1 - z0, 0.05), w["height"]), wall_mat, walls_col)
for index, d in enumerate(level["doors"]):
a = to_world(d["rect"][0], d["rect"][3])
b = to_world(d["rect"][2], d["rect"][1])
x0, x1 = min(a[0], b[0]), max(a[0], b[0])
z0, z1 = min(a[1], b[1]), max(a[1], b[1])
box("Door_%s" % d["id"], ((x0 + x1) * 0.5, -(z0 + z1) * 0.5, d["height"] * 0.5),
(max(x1 - x0, 0.1), max(z1 - z0, 0.1), d["height"]), door_mat, walls_col)
room_centers = {}
for index, r in enumerate(level["rooms"]):
a = to_world(r["rect"][0], r["rect"][3])
b = to_world(r["rect"][2], r["rect"][1])
x0, x1 = min(a[0], b[0]), max(a[0], b[0])
z0, z1 = min(a[1], b[1]), max(a[1], b[1])
room_centers[r["kind"]] = ((r["rect"][0] + r["rect"][2]) * 0.5,
(r["rect"][1] + r["rect"][3]) * 0.5)
mat = material("LEVEL_Room_" + r["kind"], KIND_COLORS.get(r["kind"], KIND_COLORS["Side"]))
obj = box("Room_%02d_%s" % (index, r["name"].replace(" ", "_")),
((x0 + x1) * 0.5, -(z0 + z1) * 0.5, 0.02),
(x1 - x0, z1 - z0, 0.04), mat, rooms_col)
obj["room_kind"] = r["kind"]
obj["flow"] = r["flow"]
for index, p in enumerate(level["props"]):
model = p.get("model", "")
if not model:
continue
path = ACTOR_PATHS.get(model, model) if p.get("room_kind") else model
path = path if os.path.isabs(path) else os.path.join(REPO, path)
if not os.path.exists(path):
continue
pos = p["pos"]
if p.get("room_kind") and p["room_kind"] in room_centers:
center = room_centers[p["room_kind"]]
pos = [center[0] + pos[0], center[1] + pos[1]]
world = to_world(pos[0], pos[1])
before = set(bpy.data.objects)
bpy.ops.import_scene.gltf(filepath=path)
imported = [o for o in bpy.data.objects if o not in before]
if not imported:
continue
root = bpy.data.objects.new("Prop_%03d_%s" % (index, model), None)
props_col.objects.link(root)
root.location = (world[0], -world[1], p.get("y", 0.0))
root.rotation_euler.z = -p.get("yaw", 0.0)
root.scale = (p.get("scale", 1.0),) * 3
for obj in imported:
obj.parent = root
move_to(obj, props_col)
anchors = level["anchors"]
for name in ("spawn", "key", "exit"):
world = to_world(anchors[name][0], anchors[name][1])
empty("Anchor_" + name.upper(), (world[0], -world[1], 1.0), anchors_col)
for r in level["rituals"]:
world = to_world(r["pos"][0], r["pos"][1])
empty("Ritual_" + r["type"], (world[0], -world[1], 1.5), anchors_col)
os.makedirs(os.path.dirname(OUTPUT), exist_ok=True)
bpy.ops.wm.save_as_mainfile(filepath=OUTPUT)
print("imported %s: %d wall lines, %d rooms, %d props" %
(LEVEL, len(level["walls"]), len(level["rooms"]), len(level["props"])))
print("saved", OUTPUT)
main()