# Export the non-terrain parts of map_refined_benches.blend as Roblox-ready GLBs + a manifest. # Run: blender -b map_refined_benches.blend --python export_meshes.py # # * Zone 1 (fence, lights, fence snow) is exported once; Studio clones it 10x rotated about the island centre. # * Plaza benches / lanterns are merged per material in world space. # * The procedural fence wood is baked (Cycles, diffuse colour) into a 1024px PNG on a fresh UV map. # * Every mesh is re-centred on its bounding-box centre; the manifest stores that centre (Blender coords). import bpy, bmesh, json, math, os from mathutils import Vector, Matrix HERE = os.path.dirname(bpy.data.filepath) OUT = os.path.join(HERE, "roblox", "meshes") os.makedirs(OUT, exist_ok=True) MAX_TRIS = 18000 ZONE_ANGLE, ZONE_HALF = 90.0, 18.0 # zone 1 sector (degrees) BAKE_SIZE = 1024 dg = bpy.context.evaluated_depsgraph_get() export_coll = bpy.data.collections.new("RobloxExport") bpy.context.scene.collection.children.link(export_coll) manifest = [] def obj_center(o): cs = [o.matrix_world @ Vector(c) for c in o.bound_box] return sum(cs, Vector()) / 8 def in_zone1(o): c = obj_center(o) a = math.degrees(math.atan2(c.y, c.x)) return abs(((a - ZONE_ANGLE + 180) % 360) - 180) <= ZONE_HALF def collect_bm(objs, keep_uv=False, mat_filter=None, drop_modifiers=()): """Evaluated, world-space geometry of objs joined into one bmesh (optionally only faces of one material).""" bm = bmesh.new() for o in objs: saved = [] for m in o.modifiers: if m.type in drop_modifiers and m.show_render: m.show_viewport = False; saved.append(m) dg2 = bpy.context.evaluated_depsgraph_get(); dg2.update() ev = o.evaluated_get(dg2) me = bpy.data.meshes.new_from_object(ev) for m in saved: m.show_viewport = True me.transform(o.matrix_world) if mat_filter is not None: tmp = bmesh.new(); tmp.from_mesh(me) names = [s.material.name if s.material else "" for s in o.material_slots] or [""] bmesh.ops.delete(tmp, geom=[f for f in tmp.faces if names[min(f.material_index, len(names) - 1)] != mat_filter], context="FACES") tmp.to_mesh(me); tmp.free() bm.from_mesh(me) bpy.data.meshes.remove(me) return bm def finish(name, bm, info): """bmesh -> object, decimate to MAX_TRIS, re-centre, register in the manifest (export later).""" bmesh.ops.triangulate(bm, faces=bm.faces[:]) me = bpy.data.meshes.new(name) bm.to_mesh(me); bm.free() ob = bpy.data.objects.new(name, me) export_coll.objects.link(ob) tris = len(me.polygons) if tris > MAX_TRIS: dec = ob.modifiers.new("Dec", "DECIMATE"); dec.ratio = MAX_TRIS / tris * 0.97 d2 = bpy.context.evaluated_depsgraph_get(); d2.update() nm = bpy.data.meshes.new_from_object(ob.evaluated_get(d2)) ob.modifiers.clear(); ob.data = nm; bpy.data.meshes.remove(me); me = nm cs = [v.co for v in me.vertices] lo = Vector((min(c.x for c in cs), min(c.y for c in cs), min(c.z for c in cs))) hi = Vector((max(c.x for c in cs), max(c.y for c in cs), max(c.z for c in cs))) ctr = (lo + hi) / 2 me.transform(Matrix.Translation(-ctr)) for p in me.polygons: p.use_smooth = info.pop("smooth", True) info.update({"name": name, "center": [round(v, 4) for v in ctr], "size": [round(v, 3) for v in hi - lo], "tris": len(me.polygons), "file": name + ".glb"}) manifest.append(info) return ob def base_color(mat): if mat and mat.node_tree: b = next((n for n in mat.node_tree.nodes if n.type == "BSDF_PRINCIPLED"), None) if b: c = b.inputs["Base Color"].default_value return [round(c[0], 4), round(c[1], 4), round(c[2], 4)] return [0.8, 0.8, 0.8] def lin_to_srgb255(c): f = lambda v: 12.92 * v if v <= 0.0031308 else 1.055 * v ** (1 / 2.4) - 0.055 return [max(0, min(255, round(f(v) * 255))) for v in c] # ------------------------------------------------------------------ zone 1: fence (baked wood) fence_src = bpy.data.objects["Zone_01_Fence"] wood_mat = fence_src.material_slots[0].material ob = finish("Zone_Fence", collect_bm([fence_src]), {"clone": "zones", "material": "Wood", "texture": "Zone_Fence_Wood.png", "color": [255, 255, 255], "smooth": True}) # bake: give the mesh the original wood shader (needs its UVMap/FaceDims/FaceSeed layers) -> rebuild with UVs kept bpy.data.objects.remove(ob, do_unlink=True); manifest.pop() d2 = bpy.context.evaluated_depsgraph_get() me = bpy.data.meshes.new_from_object(fence_src.evaluated_get(d2)) me.transform(fence_src.matrix_world) fob = bpy.data.objects.new("Zone_Fence", me) export_coll.objects.link(fob) bake_uv = me.uv_layers.new(name="BakeUV") for l in me.uv_layers: l.active_render = (l.name == "UVMap") me.uv_layers.active = bake_uv bpy.ops.object.select_all(action="DESELECT") fob.select_set(True); bpy.context.view_layer.objects.active = fob bpy.ops.object.mode_set(mode="EDIT") bpy.ops.mesh.select_all(action="SELECT") bpy.ops.uv.smart_project(angle_limit=math.radians(66), island_margin=0.004, scale_to_bounds=True) bpy.ops.object.mode_set(mode="OBJECT") img = bpy.data.images.new("Zone_Fence_Wood", BAKE_SIZE, BAKE_SIZE, alpha=False) bake_mat = wood_mat.copy() tex = bake_mat.node_tree.nodes.new("ShaderNodeTexImage"); tex.image = img bake_mat.node_tree.nodes.active = tex me.materials.clear(); me.materials.append(bake_mat) sc = bpy.context.scene sc.render.engine = "CYCLES" sc.cycles.samples = 4 sc.cycles.device = "CPU" sc.render.bake.use_pass_direct = False sc.render.bake.use_pass_indirect = False sc.render.bake.use_pass_color = True sc.render.bake.margin = 6 bpy.ops.object.bake(type="DIFFUSE", pass_filter={"COLOR"}, use_clear=True) img.filepath_raw = os.path.join(OUT, "Zone_Fence_Wood.png"); img.file_format = "PNG"; img.save() # keep only the bake UV + a plain textured material for export for l in [l for l in me.uv_layers if l.name != "BakeUV"]: me.uv_layers.remove(l) exp_mat = bpy.data.materials.new("Zone_Fence_Wood_Mat") try: exp_mat.use_nodes = True except Exception: pass nt = exp_mat.node_tree bsdf = next(n for n in nt.nodes if n.type == "BSDF_PRINCIPLED") t2 = nt.nodes.new("ShaderNodeTexImage"); t2.image = img nt.links.new(t2.outputs["Color"], bsdf.inputs["Base Color"]) me.materials.clear(); me.materials.append(exp_mat) cs = [v.co for v in me.vertices] lo = Vector((min(c.x for c in cs), min(c.y for c in cs), min(c.z for c in cs))) hi = Vector((max(c.x for c in cs), max(c.y for c in cs), max(c.z for c in cs))) ctr = (lo + hi) / 2 me.transform(Matrix.Translation(-ctr)) manifest.append({"name": "Zone_Fence", "clone": "zones", "material": "Wood", "texture": "Zone_Fence_Wood.png", "color": [255, 255, 255], "center": [round(v, 4) for v in ctr], "size": [round(v, 3) for v in hi - lo], "tris": sum(len(p.vertices) - 2 for p in me.polygons), "file": "Zone_Fence.glb"}) # ------------------------------------------------------------------ zone 1: lights (wire + glowing bulbs), snow lights_src = bpy.data.objects["Zone_01_Lights"] lmats = [s.material.name for s in lights_src.material_slots if s.material] for mname in lmats: glow = "bulb" in mname.lower() finish("Zone_" + mname, collect_bm([lights_src], mat_filter=mname, drop_modifiers=("BEVEL",)), {"clone": "zones", "material": "Neon" if glow else "SmoothPlastic", "color": lin_to_srgb255(base_color(bpy.data.materials[mname])), "glow": glow}) snow_objs = [o for o in bpy.data.objects if o.type == "MESH" and o.users_collection and o.users_collection[0].name in ("Snow_Draped_Reference", "Snow_Organic_Domes") and in_zone1(o)] finish("Zone_FenceSnow", collect_bm(snow_objs), {"clone": "zones", "material": "Snow", "color": [248, 251, 255], "count_src": len(snow_objs)}) # ------------------------------------------------------------------ plaza benches + lanterns, merged per material plaza = [o for o in bpy.data.objects if o.type in ("MESH", "CURVE") and o.users_collection and o.users_collection[0].name in ("Plaza_Benches_And_Lanterns", "Refined_Bench_Details")] by_mat = {} for o in plaza: for s in o.material_slots: if s.material: by_mat.setdefault(s.material.name, set()).add(o) RB = {"Bench_Warm_Wood": "Wood", "Lantern_Dark_Bronze": "Metal", "Lantern_Warm_Glass": "Neon", "Festive_Ribbon": "SmoothPlastic", "Snow_Full_Soft": "Snow", "Snow_Soft": "Snow"} for mname, objs in by_mat.items(): objs = sorted(objs, key=lambda o: o.name) halves = [objs] probe = collect_bm(objs, mat_filter=mname); n = len(probe.faces) * 2; probe.free() if n > MAX_TRIS: k = math.ceil(n / MAX_TRIS) halves = [objs[i::k] for i in range(k)] for hi_, part in enumerate(halves): finish("Plaza_%s_%d" % (mname, hi_), collect_bm(part, mat_filter=mname), {"clone": None, "material": RB.get(mname, "SmoothPlastic"), "color": lin_to_srgb255(base_color(bpy.data.materials[mname])), "glow": RB.get(mname) == "Neon"}) # lantern light positions (for PointLights in Studio) lamps = [[round(v, 3) for v in obj_center(o)] for o in bpy.data.objects if o.name.startswith("Warm_Lantern")] # ------------------------------------------------------------------ export each mesh as its own GLB for info in manifest: o = bpy.data.objects[info["name"]] bpy.ops.object.select_all(action="DESELECT") o.select_set(True); bpy.context.view_layer.objects.active = o bpy.ops.export_scene.gltf(filepath=os.path.join(OUT, info["file"]), export_format="GLB", use_selection=True, export_yup=True, export_apply=True) json.dump({"zone_angle_deg": ZONE_ANGLE, "zone_count": 10, "parts": manifest, "lantern_lights": lamps}, open(os.path.join(OUT, "manifest.json"), "w"), indent=1) for m in manifest: print("PART", m["name"], m["tris"], m["material"], m["color"], m.get("clone")) print("LAMPS", len(lamps))