"""What a region of a demo map contains, bay by bay (for cutting the pack's demo rooms into reusable units). python build/study_region.py x0 x1 y0 y1 [axis=x] [bay=3.6] [--list] Region and positions in Blender metres. Every mesh instance, light and decal whose origin lies in the region is counted in the bay its origin falls into (bays run along `axis` from the region's low end). """ import collections import json import math import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) KIT = os.path.join(os.path.dirname(HERE), "kit_export") args = [a for a in sys.argv[1:] if not a.startswith("--")] name = args[0] x0, x1, y0, y1 = (float(v) for v in args[1:5]) axis = args[5] if len(args) > 5 else "x" bay = float(args[6]) if len(args) > 6 else 3.6 data = json.load(open(os.path.join(KIT, "maps", name + ".json"), encoding="utf-8")) def pos(rows): return rows[3][0] / 100.0, -rows[3][1] / 100.0, rows[3][2] / 100.0 def yaw(rows): return math.degrees(math.atan2(-rows[0][1], rows[0][0])) n_bays = int(math.ceil(((x1 - x0) if axis == "x" else (y1 - y0)) / bay - 1e-6)) table = collections.defaultdict(lambda: [0] * n_bays) rows_out = [] kinds = collections.Counter() for a in data["actors"]: if a.get("hidden") or a.get("hidden_in_game"): continue items = [] for mc in a.get("meshes", []): for rows in (mc.get("instances") or [mc["matrix"]]): items.append(("M " + mc["mesh"].split("/")[2][-9:] + "/" + mc["mesh"].split("/")[-1], rows)) for l in a.get("lights", []): items.append(("L " + l["comp_class"], l["matrix"])) for d in a.get("decals", []): items.append(("D " + (d["material"] or "?").split("/")[-1], d["matrix"])) for key, rows in items: x, y, z = pos(rows) if not (x0 - 1e-3 <= x <= x1 + 1e-3 and y0 - 1e-3 <= y <= y1 + 1e-3): continue t = (x - x0) if axis == "x" else (y - y0) b = min(n_bays - 1, max(0, int(t / bay))) table[key][b] += 1 kinds[key[0]] += 1 rows_out.append((b, key, round(x - x0, 2), round(y - y0, 2), round(z, 2), round(yaw(rows), 0), a["label"])) print("region x %.2f..%.2f y %.2f..%.2f bays %d of %.2f along %s %s" % (x0, x1, y0, y1, n_bays, bay, axis, dict(kinds))) for key in sorted(table): print(" %-46s %s" % (key, " ".join("%2d" % v for v in table[key]))) if "--list" in sys.argv: for r in sorted(rows_out): print(" bay %d %-44s (%6.2f, %6.2f, %6.2f) yaw %5.0f %s" % r)