"""Props: bubbles, boxes, flattened box, wrap roll, wrap ball, packing peanut. Run: blender -b --factory-startup --python props.py -- [names] """ import sys, os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from common import * def bubble_dome(): o = revolve("SM_BubbleDome", [(0.5, 0.0), (0.495, 0.14), (0.42, 0.32), (0.25, 0.425), (0.0, 0.45)], segs=12, m="M_BubblePlastic", close_top=False) smooth(o, 80) return dict(elev=30) def bubble_popped(): segs, radii = 14, [0.0, 0.17, 0.33, 0.44, 0.5] bm = bmesh.new() rings = [] tear = 3 # segment index of the tear for ri, r in enumerate(radii): if r == 0: rings.append([bm.verts.new((0.02, -0.01, 0.07))]) continue row = [] for i in range(segs): a = 2 * math.pi * i / segs env = 1 - (r / 0.5) ** 2 z = env * 0.12 * random.uniform(0.35, 1.0) rr = r * (1 + random.uniform(-0.06, 0.03) * env) if ri < len(radii) - 1 and i in (tear, tear + 1): z *= 0.15 # sunken tear area row.append(bm.verts.new((rr * math.cos(a), rr * math.sin(a), max(z, 0.0) + (0.004 if r < 0.5 else 0)))) rings.append(row) for a, b in zip(rings, rings[1:]): for i in range(segs): if len(a) == 1: bm.faces.new((a[0], b[i], b[(i + 1) % segs])) else: bm.faces.new((a[i], a[(i + 1) % segs], b[(i + 1) % segs], b[i])) bmesh.ops.recalc_face_normals(bm, faces=bm.faces) for f in bm.faces: if f.normal.z < 0: f.normal_flip() o = mesh_obj("SM_BubblePopped", bm) set_mats(o, "M_BubblePlastic") smooth(o, 28) return dict(elev=40) # ------------------------------------------------------------------ boxes def make_box(name, w, d, h, t=0.011, tape_w=0.075): # Blender: X width, Y depth, Z up. Unity +Z (N) == Blender -Y; Unity +X (E) == Blender -X. bm = bmesh.new() bmesh.ops.create_cube(bm, size=1.0) bmesh.ops.scale(bm, vec=(w, d, h), verts=bm.verts) bmesh.ops.translate(bm, vec=(0, 0, h / 2), verts=bm.verts) top = [f for f in bm.faces if f.normal.z > 0.9] bmesh.ops.delete(bm, geom=top, context="FACES_ONLY") # round vertical corners vert_edges = [e for e in bm.edges if abs(e.verts[0].co.z - e.verts[1].co.z) > h * 0.9] bmesh.ops.bevel(bm, geom=vert_edges, offset=0.03, segments=4, affect="EDGES", profile=0.5) uv = bm.loops.layers.uv.verify() for f in bm.faces: n = f.normal if abs(n.z) > 0.9: f.material_index = 1 for l in f.loops: l[uv].uv = (l.vert.co.x / w + 0.5, l.vert.co.y / d + 0.5) continue f.material_index = 0 for l in f.loops: p = l.vert.co if abs(n.y) >= abs(n.x): u = (p.x / w + 0.5) if n.y < 0 else (0.5 - p.x / w) else: u = (0.5 - p.y / d) if n.x > 0 else (p.y / d + 0.5) l[uv].uv = (u, p.z / h) body = mesh_obj("Body", bm) set_mats(body, "M_BoxPrint", "M_Cardboard") mod(body, "SOLIDIFY", thickness=t, offset=-1, material_offset=1, material_offset_rim=1, use_even_offset=True) mod(body, "BEVEL", width=0.004, segments=2, limit_method="ANGLE", angle_limit=math.radians(40)) bake(body) smooth(body, 35) def flap(nm, sx, sy, cx, cy, z, hinge): f = rbox(nm, (sx, sy, 0.008), (cx, cy, z), bevel=0.0035, segs=2, m="M_Cardboard") uv_box(f, 2.0) smooth(f, 35) set_origin(f, hinge) return f gap = 0.004 zt = h + 0.004 # lower flaps (N/S, Unity +-Z) zt2 = h + 0.012 # upper flaps (E/W, meet at the seam x=0) fn = flap("Flap_N", w - 0.03, d * 0.42, 0, -d / 2 + d * 0.21, zt, (0, -d / 2, h)) fs = flap("Flap_S", w - 0.03, d * 0.42, 0, d / 2 - d * 0.21, zt, (0, d / 2, h)) fe = flap("Flap_E", w / 2 - gap, d - 0.006, -w / 4 - gap / 2, 0, zt2, (-w / 2, 0, h)) fw = flap("Flap_W", w / 2 - gap, d - 0.006, w / 4 + gap / 2, 0, zt2, (w / 2, 0, h)) # tape strip along Blender Y (Unity Z) over the seam, draping down the N/S faces top_z = zt2 + 0.0045 drop = min(0.11, h * 0.3) ey = d / 2 + 0.0012 path = [(-ey, h - drop), (-ey, top_z - 0.004), (-ey + 0.004, top_z), (ey - 0.004, top_z), (ey, top_z - 0.004), (ey, h - drop)] bm = bmesh.new() uvl = bm.loops.layers.uv.verify() rows = [] for (y, z) in path: rows.append((bm.verts.new((-tape_w / 2, y, z)), bm.verts.new((tape_w / 2, y, z)))) acc = [0.0] for i in range(1, len(path)): acc.append(acc[-1] + math.dist(path[i], path[i - 1])) aspect = (tape_w / 0.15) for i in range(len(rows) - 1): a, b = rows[i], rows[i + 1] fce = bm.faces.new((a[0], a[1], b[1], b[0])) for l in fce.loops: j = i if l.vert in a else i + 1 side = 0 if l.vert in (a[0], b[0]) else 1 l[uvl].uv = (acc[j] / aspect, 0.42 + side * 0.15) bmesh.ops.recalc_face_normals(bm, faces=bm.faces) tape = mesh_obj("Tape", bm) # make normals point outward (up / away from box) tb = bmesh.new(); tb.from_mesh(tape.data) for f in tb.faces: c = f.calc_center_median() out = Vector((0, c.y, c.z - h * 0.5)).normalized() if f.normal.dot(out) < 0: f.normal_flip() tb.to_mesh(tape.data); tb.free() set_mats(tape, "M_Tape") mod(tape, "SOLIDIFY", thickness=0.0015, offset=1) bake(tape) smooth(tape, 30) return dict(elev=32) def box_small(): return make_box("SM_Box_Small", 0.5, 0.4, 0.4, tape_w=0.07) def box_large(): return make_box("SM_Box_Large", 0.8, 0.6, 0.6, t=0.013, tape_w=0.09) def box_flat(): w, d = 0.9, 0.6 a = rbox("A", (w, d, 0.014), (0, 0, 0.007), bevel=0.005, segs=2) b = rbox("B", (w * 0.96, d * 0.47, 0.012), (0.01, -d * 0.26, 0.02), bevel=0.005, segs=2) c = rbox("C", (w * 0.95, d * 0.47, 0.012), (-0.008, d * 0.25, 0.019), bevel=0.005, segs=2) b.rotation_euler = (0, math.radians(0.6), math.radians(0.8)) c.rotation_euler = (math.radians(-0.5), 0, math.radians(-0.6)) o = join([a, b, c], "SM_BoxFlat") set_mats(o, "M_BoxPrint", "M_Cardboard") bm = bmesh.new(); bm.from_mesh(o.data) uv = bm.loops.layers.uv.verify() for f in bm.faces: if f.normal.z > 0.9 and f.calc_center_median().z > 0.02: f.material_index = 0 for l in f.loops: l[uv].uv = (l.vert.co.x / w + 0.5, l.vert.co.y / d * 1.0 + 0.5) else: f.material_index = 1 for l in f.loops: l[uv].uv = (l.vert.co.x * 2 + l.vert.co.z * 2, l.vert.co.y * 2 + l.vert.co.z * 2) bm.to_mesh(o.data); bm.free() smooth(o, 35) return dict(elev=45) # ------------------------------------------------------------------ wrap def wrap_roll(): R, L = 0.175, 0.8 outer = cyl("outer", R, L - 0.03, axis="X", verts=48, cap=False) uv_cyl(outer, axis=0, circ_scale=4, len_scale=3.3) set_mats(outer, "M_WrapRoll") parts = [outer] for s in (-1, 1): # layered end face: wavy rings from core to outer edge prof = [] n = 9 for i in range(n + 1): r = 0.047 + (R - 0.047) * i / n x = (L / 2 - 0.015) + (0.006 if i % 2 else 0.0) + (0.015 * (1 - ((i / n) - 1) ** 2) if i == n else 0) prof.append((r, x)) prof[-1] = (R, L / 2 - 0.015) end = revolve("end", [(r, x * s) for r, x in prof], segs=48, axis="X", close_top=False) bm = bmesh.new(); bm.from_mesh(end.data) for f in bm.faces: if f.normal.x * s < 0: f.normal_flip() bm.to_mesh(end.data); bm.free() uv_box(end, 3.3) set_mats(end, "M_WrapRoll") parts.append(end) core = revolve("core", [(0.034, -L / 2 - 0.012), (0.047, -L / 2 - 0.012), (0.047, L / 2 + 0.012), (0.034, L / 2 + 0.012), (0.034, -L / 2 - 0.012)], segs=32, axis="X", close_top=False) uv_cyl(core, axis=0, circ_scale=1, len_scale=2) set_mats(core, "M_Cardboard") parts.append(core) # loose tail of wrap peeling off the roll bm = bmesh.new() uvl = bm.loops.layers.uv.verify() pts = [] for i in range(5): # attached part hugging the roll (back -> bottom) a = math.radians(-35 + i * 8.75) pts.append((R * 1.02 * math.sin(a) * -1 + 0.0, -R * 1.02 * math.cos(a))) for i in range(1, 6): # flat tail on the floor towards -Y (Unity +Z / front) pts.append((-i * 0.05, -R - 0.002 + (0.006 if i == 1 else 0.0))) rows = [] for (y, z) in pts: rows.append((bm.verts.new((-L / 2 + 0.02, y, z)), bm.verts.new((L / 2 - 0.02, y, z)))) for i in range(len(rows) - 1): a, b = rows[i], rows[i + 1] f = bm.faces.new((a[0], a[1], b[1], b[0])) for l in f.loops: j = i if l.vert in a else i + 1 l[uvl].uv = ((l.vert.co.x) * 3.3, j * 0.04 * 3.3) tail = mesh_obj("tail", bm) set_mats(tail, "M_WrapRoll") mod(tail, "SOLIDIFY", thickness=0.004, offset=0) bake(tail) parts.append(tail) o = join(parts, "SM_WrapRoll") smooth(o, 50) return dict(elev=25, azim=-50) def wrap_ball(): bm = bmesh.new() bmesh.ops.create_icosphere(bm, subdivisions=4, radius=0.2) rnd = random.Random(7) # big crumples: push random clusters in/out centers = [Vector((rnd.uniform(-1, 1), rnd.uniform(-1, 1), rnd.uniform(-1, 1))).normalized() for _ in range(14)] amps = [rnd.uniform(-0.05, 0.045) for _ in centers] for v in bm.verts: n = v.co.normalized() dsp = 0 for c, a in zip(centers, amps): dsp += a * max(0, n.dot(c) - 0.55) / 0.45 dsp += rnd.uniform(-0.012, 0.012) v.co = n * (0.2 + dsp) bmesh.ops.scale(bm, vec=(1.08, 1.0, 0.92), verts=bm.verts) o = mesh_obj("SM_WrapBall", bm) set_mats(o, "M_WrapRoll") uv_box(o, 3.3) smooth(o, 32) return dict(elev=25) def peanut(): pts = [(-0.012, 0, -0.002), (-0.007, 0, 0.004), (0.0, 0, 0.0), (0.007, 0, -0.004), (0.012, 0, 0.002)] o = tube("SM_PackingPeanut", pts, 0.0055, m="M_PaintWhite", res=4, segs=8) o.scale = (1, 0.65, 1) apply_xform(o) smooth(o, 60) return dict(elev=30) if __name__ == "__main__": run({ "SM_BubbleDome": bubble_dome, "SM_BubblePopped": bubble_popped, "SM_Box_Small": box_small, "SM_Box_Large": box_large, "SM_BoxFlat": box_flat, "SM_WrapRoll": wrap_roll, "SM_WrapBall": wrap_ball, "SM_PackingPeanut": peanut, })