Files
2026-11-17 20:19:55 +03:00

69 lines
2.9 KiB
Python

# Sample the island ground of map_refined_benches.blend into a column grid for Roblox Terrain.
# Run: blender -b map_refined_benches.blend --python sample_terrain.py
# Output: terrain_columns.json (Blender units == studs; Blender Z-up -> Roblox Y-up handled in Luau)
import bpy, json, os
from mathutils import Vector
from mathutils.bvhtree import BVHTree
OUT = os.path.join(os.path.dirname(bpy.data.filepath), "roblox", "terrain_columns.json")
CELL = 4.0
GROUND_COLLS = {"Terrain", "Landscape_Snowdrifts", "Asphalt_Edge_Snowdrifts", "Continuous_Curb_Snow"}
dg = bpy.context.evaluated_depsgraph_get()
trees = [] # (bvh, per-face material name list, object name)
lo = Vector((1e9, 1e9, 1e9)); hi = -lo
for o in bpy.data.objects:
if o.type != "MESH" or not ({c.name for c in o.users_collection} & GROUND_COLLS):
continue
ev = o.evaluated_get(dg)
me = ev.to_mesh()
me.transform(o.matrix_world)
verts = [v.co.copy() for v in me.vertices]
polys = [tuple(p.vertices) for p in me.polygons]
mats = [(o.material_slots[p.material_index].material.name
if o.material_slots and o.material_slots[p.material_index].material else "") for p in me.polygons]
ev.to_mesh_clear()
trees.append((BVHTree.FromPolygons(verts, polys), mats, o.name))
for v in verts:
lo = Vector(map(min, lo, v)); hi = Vector(map(max, hi, v))
def code_for(obj, mat):
m = mat.lower(); n = obj.lower()
if "rock" in m or "cliff" in m:
return "R"
if "curb" in m or "curb" in n and "snow" not in m:
return "C"
if "asphalt" in m:
return "A"
if "tile" in m or "path" in m or "plaza" in n:
return "P"
return "S" # snow (ground, drifts)
x0 = (int(lo.x // CELL) - 1) * CELL
y0 = (int(lo.y // CELL) - 1) * CELL
nx = int((hi.x - x0) // CELL) + 2
ny = int((hi.y - y0) // CELL) + 2
top, bot, mat = [], [], []
for j in range(ny):
for i in range(nx):
x = x0 + (i + 0.5) * CELL
y = y0 + (j + 0.5) * CELL
best_t, best_c = None, "."
best_b = None
for bvh, mats, name in trees:
hit = bvh.ray_cast(Vector((x, y, hi.z + 10)), Vector((0, 0, -1)))
if hit[0] is not None and (best_t is None or hit[0].z > best_t):
best_t = hit[0].z
best_c = code_for(name, mats[hit[2]])
hb = bvh.ray_cast(Vector((x, y, lo.z - 10)), Vector((0, 0, 1)))
if hb[0] is not None and (best_b is None or hb[0].z < best_b):
best_b = hb[0].z
top.append(round(best_t, 2) if best_t is not None else None)
bot.append(round(best_b, 2) if best_b is not None else None)
mat.append(best_c)
os.makedirs(os.path.dirname(OUT), exist_ok=True)
json.dump({"cell": CELL, "x0": x0, "y0": y0, "nx": nx, "ny": ny, "top": top, "bot": bot, "mat": "".join(mat),
"zmin": lo.z, "zmax": hi.z}, open(OUT, "w"))
from collections import Counter
print("SAMPLED", nx, ny, dict(Counter(mat)), round(lo.z, 1), round(hi.z, 1))