"""A trading-card mesh for SL, real card size (63 x 88 mm, rounded corners), for a friend's idea (2026-10-12). "C:/Program Files/Blender Foundation/Blender 5.2/blender.exe" -b -P gen_card.py -- [--out card.glb] Faces (SL orders faces by material name): 0 back, 1 edge, 2 art (front; animate it with llSetTextureAnim), 3 overlay (0.3 mm in front of the art; give it a texture with transparency and the art shows through), 4 spacer. SL won't make an object thinner than 1 cm, so a tiny spacer triangle sits 1 cm behind the card to make the bounding box 1 cm deep while the card itself stays 0.6 mm thin; set face 4 fully transparent (the script does). UVs: art/overlay/back each map their whole texture onto the card (back mirrored so it reads from behind). Exported Y-up (the default), so it rezzes standing, front facing the object's +X. Claude Opus 5.5 (Tessel). """ import math import os import sys import bmesh import bpy argv = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] OUT = argv[argv.index("--out") + 1] if "--out" in argv else os.path.join(os.path.dirname(os.path.abspath(__file__)), "card.glb") W, H, T, R, SEG = 0.063, 0.088, 0.0006, 0.0035, 8 # width (y), height (z), thickness (x), corner radius GAP = 0.0003 # overlay in front of the art DEPTH = 0.0101 # bounding-box depth forced by the spacer for o in list(bpy.data.objects): bpy.data.objects.remove(o) def mat(name, rgb): m = bpy.data.materials.new(name) m.use_nodes = True m.node_tree.nodes["Principled BSDF"].inputs["Base Color"].default_value = (*rgb, 1) return m MATS = [mat("0_back", (0.1, 0.15, 0.35)), mat("1_edge", (0.9, 0.85, 0.7)), mat("2_art", (1, 1, 1)), mat("3_overlay", (1, 1, 1)), mat("4_spacer", (0, 0, 0))] outline = [] # (y, z) round the rounded rectangle, counter-clockwise from +x view for cy, cz, a0 in ((W / 2 - R, H / 2 - R, 0), (-W / 2 + R, H / 2 - R, 90), (-W / 2 + R, -H / 2 + R, 180), (W / 2 - R, -H / 2 + R, 270)): for k in range(SEG + 1): a = math.radians(a0 + 90 * k / SEG) outline.append((cy + R * math.cos(a), cz + R * math.sin(a))) me = bpy.data.meshes.new("card") ob = bpy.data.objects.new("card", me) bpy.context.collection.objects.link(ob) for m in MATS: me.materials.append(m) bm = bmesh.new() uvl = bm.loops.layers.uv.new("UVMap") def uv_front(y, z): return ((y + W / 2) / W, (z + H / 2) / H) def cap(x, idx, mirror, flip): vs = [bm.verts.new((x, y, z)) for y, z in outline] f = bm.faces.new(list(reversed(vs)) if flip else vs) f.material_index = idx for l in f.loops: u, v = uv_front(l.vert.co.y, l.vert.co.z) l[uvl].uv = (1 - u if mirror else u, v) return vs front = cap(T / 2, 2, False, False) # faces +x back = cap(-T / 2, 0, True, True) # faces -x, mirrored so it reads from behind n = len(outline) for i in range(n): j = (i + 1) % n f = bm.faces.new((back[i], back[j], front[j], front[i])) f.material_index = 1 for l in f.loops: l[uvl].uv = (i / n, 0.5) cap(T / 2 + GAP, 3, False, False) # overlay s = [bm.verts.new((T / 2 + GAP - DEPTH, dy, dz)) for dy, dz in ((0, 0), (0.0002, 0), (0, 0.0002))] f = bm.faces.new(s) f.material_index = 4 bm.normal_update() bm.to_mesh(me) bm.free() bpy.ops.object.select_all(action="DESELECT") ob.select_set(True) bpy.context.view_layer.objects.active = ob bpy.ops.export_scene.gltf(filepath=OUT, export_format="GLB", use_selection=True, export_apply=True) print("wrote", OUT) if "--check" in argv: from collections import defaultdict acc = defaultdict(lambda: [0, 0, 0]) for p in me.polygons: c = p.center out = (c.x * p.normal.x + c.y * p.normal.y + c.z * p.normal.z) # + = normal points away from the centre acc[p.material_index][0] += 1 acc[p.material_index][1] += p.normal.x acc[p.material_index][2] += out > 0 for k, (cnt, nx, outward) in sorted(acc.items()): print(f"face {k}: {cnt} polys, mean normal x {nx / cnt:+.2f}, outward {outward}/{cnt}")