Files
grass-cutters/ArtSource/Blender/scripts/gc_phone.py
NiemandKG d1c3761e58 Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and
Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date.
Images, audio, models and binaries go through Git LFS (.gitattributes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 18:44:40 +06:00

232 lines
8.6 KiB
Python

"""GRASS CUTTER - the crew's field phone: SM_Phone, with a child "Screen" whose UVs cover the display.
Not a shop phone but a company slate: two corners cut hard, a light line round the display and through the band,
a lens pod and cooling ribs on the back, and a stub of an antenna for the relay to Earth.
The display faces +Y (Unity -Z) and "right" as you look at it is -X (Unity +X). Origin in the middle.
Run: blender -b --factory-startup --python gc_phone.py"""
import math, os, sys
import bmesh, bpy
from mathutils import Matrix, Vector
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import common
from common import export, mat, preview, reset
W, H, D = 0.100, 0.212, 0.0088
CUT, NICK = 0.013, 0.005 # the two hard corners (top right, bottom left) and the two soft ones
ROUND = 0.0026
BEZEL = 0.003 # keep in step with Phone.ScreenW and ScreenH
SHELL, BAND, BLACK, GLOW, LENS = range(5)
MATS = ["M_PhoneShell", "M_PhoneBand", "M_PhoneBlack", "M_PhoneGlow", "M_PhoneLens"]
def sharp(w, h, cut, nick):
a, b = w / 2, h / 2
return [Vector(p) for p in ((-a + cut, -b), (a - nick, -b), (a, -b + nick), (a, b - cut), (a - cut, b), (-a + nick, b), (-a, b - nick), (-a, -b + cut))]
def inset(pts, d):
"""Every edge of a counter-clockwise outline moved inward by d."""
out = []
n = len(pts)
for i in range(n):
p, a, b = pts[i], pts[i - 1], pts[(i + 1) % n]
e0, e1 = (p - a).normalized(), (b - p).normalized()
n0, n1 = Vector((-e0.y, e0.x)), Vector((-e1.y, e1.x))
m = (n0 + n1).normalized()
out.append(p + m * (d / m.dot(n0)))
return out
def rounded(pts, r, seg=3):
out = []
n = len(pts)
for i in range(n):
p, a, b = pts[i], pts[i - 1], pts[(i + 1) % n]
q0, q1 = p + (a - p).normalized() * r, p + (b - p).normalized() * r
for s in range(seg + 1):
t = s / seg
out.append(q0 * (1 - t) ** 2 + p * 2 * t * (1 - t) + q1 * t ** 2)
return out
def outline(d):
return rounded(inset(sharp(W, H, CUT, NICK), d), max(ROUND - d * 0.5, 0.0007))
def at(p, y, shift=(0.0, 0.0)):
"""Outline coordinates, as you look at the display, to Blender."""
return Vector((-(p.x + shift[0]), y, p.y + shift[1]))
def ring(bm, pts, y):
return [bm.verts.new(at(p, y)) for p in pts]
def loft(bm, a, b, m):
n = len(a)
for i in range(n):
f = bm.faces.new((a[i], a[(i + 1) % n], b[(i + 1) % n], b[i]))
f.material_index = m
def cap(bm, vs, m):
f = bm.faces.new(vs)
f.material_index = m
return f
def plate(bm, pts, shift, y0, tall, m, top=None, side=-1.0, ch=0.0004):
"""A raised slab on the back: walls, a small chamfer and a flat top."""
y1 = y0 + side * tall
base = [bm.verts.new(at(p, y0, shift)) for p in pts]
mid = [bm.verts.new(at(p, y1 - side * ch, shift)) for p in pts]
c = Vector((sum(p.x for p in pts) / len(pts), sum(p.y for p in pts) / len(pts)))
high = [bm.verts.new(at(c + (p - c) * (1 - ch / max((p - c).length, 1e-6)), y1, shift)) for p in pts]
if side > 0:
loft(bm, base, mid, m)
loft(bm, mid, high, m)
cap(bm, high, m if top is None else top)
else:
loft(bm, mid, base, m)
loft(bm, high, mid, m)
cap(bm, list(reversed(high)), m if top is None else top)
def pill(w, h, seg=5):
r = min(w, h) / 2
a, b = w / 2 - r, h / 2 - r
pts = []
for q, c in enumerate(((a, -b), (a, b), (-a, b), (-a, -b))):
for s in range(seg + 1):
t = (q + s / seg) * math.pi / 2 - math.pi / 2
pts.append(Vector((c[0] + math.cos(t) * r, c[1] + math.sin(t) * r)))
return pts
def circle(r, n=20):
return [Vector((math.cos(i * 2 * math.pi / n) * r, math.sin(i * 2 * math.pi / n) * r)) for i in range(n)]
def blade(bm, root, tip, lean, wide, y, m, n=8):
"""One blade of grass, curved, for the company mark on the back."""
root, tip = Vector(root), Vector(tip)
bend = (root + tip) / 2 + Vector((lean, 0.0))
left, right = [], []
for i in range(n + 1):
t = i / n
p = root * (1 - t) ** 2 + bend * 2 * t * (1 - t) + tip * t ** 2
e = (root * -2 * (1 - t) + bend * 2 * (1 - 2 * t) + tip * 2 * t).normalized()
s = Vector((-e.y, e.x)) * wide * (1 - t) ** 0.7
left.append(bm.verts.new(at(p + s, y)))
right.append(bm.verts.new(at(p - s, y)))
for i in range(n):
f = bm.faces.new((left[i + 1], right[i + 1], right[i], left[i]))
f.material_index = m
def body():
bm = bmesh.new()
z = D / 2
# the band, back to front: a chamfer, the light line through the middle of the side, a chamfer
profile = [(0.0024, -z), (0.0007, -z + 0.0012), (0.0, -z + 0.0024), (0.0, -0.0004), (0.0, 0.0004), (0.0, z - 0.0024), (0.0007, z - 0.0012), (0.0017, z)]
mats = [BAND, BAND, BAND, GLOW, BAND, BAND, BAND]
rings = [ring(bm, outline(d), y) for d, y in profile]
for i, m in enumerate(mats):
loft(bm, rings[i], rings[i + 1], m)
cap(bm, list(reversed(rings[0])), SHELL)
# the light line round the display, then black glass
inner = ring(bm, outline(0.0024), z)
loft(bm, rings[-1], inner, GLOW)
cap(bm, inner, BLACK)
# the back, as you see it from behind: the pod of lenses across the top
back = -z
pod = (0.0, H / 2 - 0.03)
plate(bm, pill(0.078, 0.024), pod, back, 0.0016, BAND)
plate(bm, pill(0.072, 0.019), pod, back - 0.0016, 0.0002, SHELL)
for x, r in ((-0.024, 0.0072), (0.0, 0.0072), (0.024, 0.0054)):
s = (pod[0] + x, pod[1])
plate(bm, circle(r), s, back - 0.0018, 0.0006, BAND)
plate(bm, circle(r * 0.72), s, back - 0.0024, 0.0003, LENS, ch=0.0001)
plate(bm, pill(0.05, 0.0014), (0.0, pod[1] - 0.0165), back, 0.00012, GLOW, ch=0.00004)
# the company's mark, three blades of grass
y = back - 0.00012
blade(bm, (0.0, -0.022), (0.002, 0.03), 0.004, 0.0034, y, GLOW)
blade(bm, (-0.004, -0.022), (-0.024, 0.012), 0.012, 0.0028, y, GLOW)
blade(bm, (0.004, -0.022), (0.024, 0.01), -0.012, 0.0028, y, GLOW)
# cooling ribs low on the back
for i in range(8):
plate(bm, pill(0.056 - abs(i - 3.5) * 0.004, 0.0018), (0.0, -H / 2 + 0.024 + i * 0.0048), back, 0.0006, BAND, ch=0.0002)
# the buttons, down the edges
def button(side, v, length):
x = side * W / 2
pts = pill(0.0032, length)
vs = [Vector((-(x + side * 0.0006), p.x, v + p.y)) for p in pts]
low = [Vector((-x, p.x * 1.1, v + p.y)) for p in pts]
a, b = [bm.verts.new(p) for p in low], [bm.verts.new(p) for p in vs]
if side > 0:
loft(bm, b, a, BAND)
cap(bm, list(reversed(b)), BAND)
else:
loft(bm, a, b, BAND)
cap(bm, b, BAND)
button(1, 0.03, 0.024)
button(-1, 0.05, 0.017)
button(-1, 0.03, 0.017)
# the antenna for the relay, by the soft top corner
ax, ay = -(-W / 2 + 0.016), 0.0
for (r0, r1, h0, h1, m) in ((0.0034, 0.0024, 0.0, 0.0028, BAND), (0.0015, 0.0012, 0.0028, 0.017, BAND), (0.0024, 0.0024, 0.017, 0.0205, GLOW), (0.0024, 0.0008, 0.0205, 0.022, BAND)):
made = bmesh.ops.create_cone(bm, cap_ends=True, segments=10, radius1=r0, radius2=r1, depth=h1 - h0,
matrix=Matrix.Translation((ax, ay, H / 2 - 0.0002 + (h0 + h1) / 2)))
faces = {f for v in made["verts"] for f in v.link_faces}
for f in faces:
f.material_index = m
bm.normal_update()
me = bpy.data.meshes.new("SM_Phone")
bm.to_mesh(me)
bm.free()
o = bpy.data.objects.new("SM_Phone", me)
bpy.context.collection.objects.link(o)
for name in MATS:
me.materials.append(mat(name))
return o
def screen(parent):
"""The display as one flat fan, UVs over its bounding box so the panel's corners are what the cut corners take off."""
pts = outline(BEZEL)
sw, sh = W - BEZEL * 2, H - BEZEL * 2
bm = bmesh.new()
uv = bm.loops.layers.uv.new("UVMap")
f = bm.faces.new([bm.verts.new(at(p, D / 2 + 0.0002)) for p in pts])
for l, p in zip(f.loops, pts):
l[uv].uv = (p.x / sw + 0.5, p.y / sh + 0.5)
bmesh.ops.triangulate(bm, faces=[f])
me = bpy.data.meshes.new("Screen")
bm.to_mesh(me)
bm.free()
me.materials.append(mat("M_PhoneIdle"))
o = bpy.data.objects.new("Screen", me)
bpy.context.collection.objects.link(o)
o.parent = parent
return o
def main():
reset()
a = body()
screen(a)
print("SM_Phone", len(a.data.polygons), "faces")
export("SM_Phone")
preview("SM_Phone_front", elev=8, azim=162, res=700, bg=(0.2, 0.24, 0.22))
preview("SM_Phone_back", elev=12, azim=-24, res=700, bg=(0.62, 0.7, 0.66))
if __name__ == "__main__":
main()