"""GRASS CUTTER - shared kit of the model upgrade for the base and the world (gc_up_base.py, gc_up_world.py). Builds on the carpentry of gc_farm_machines. Blender space: metres, Z up, front = -Y (Unity +Z), Unity +X = Blender -X.""" import sys, os, zlib sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import common from gc_farm_machines import * # noqa: F401,F403 from os_rig import quad as rig_quad, M4 PK, PKW, PKD = "M_Deck", "M_DeckWorn", "M_DeckDark" SKY = "M_UpSkyBlue" ROCK = "M_Rock" PACK = "D:/PROJECTS/ASSETS/_extract_POLYGON_Farm/flat/Models/" SHOTS = os.environ.get("GC_SHOTS", os.path.join(os.environ.get("TEMP", "."), "gc_up_shots")) def ffin(P, name, uv=1.0, ang=38, wn=True, ms=None): """gc_farm_machines.ffin with one fix: bmesh names new UV layers Float2 in this Blender, the pack parts and decal quads use UVMap, and joining the two gives a mesh with two layers, the active one empty. Every part is put on the one layer UVMap first. ms: UV scale per first material of a part, so worn paint can read as patches and not as spots.""" P = [o for o in P if o is not None] for o in P: m0 = o.data.materials[0].name if o.data.materials and o.data.materials[0] else "" uv_part(o, (ms or {}).get(m0, uv)) me = o.data if not me.uv_layers: me.uv_layers.new(name="UVMap") for l in list(me.uv_layers)[1:]: me.uv_layers.remove(l) me.uv_layers[0].name = "UVMap" o = join(P, name) smooth(o, ang) if wn: weighted(o) return o def inset(pts, d): """Offset a counter-clockwise (x, z) outline inward by d.""" n = len(pts) out = [] for i in range(n): p, a, b = Vector(pts[i]), Vector(pts[i - 1]), Vector(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(tuple(p + m_ * (d / max(0.25, m_.dot(n0))))) return out def fpart(P, name, origin, **kw): o = ffin(P, name, **kw) set_origin(o, origin) return o def plain(x0, x1, y0, y1, z0, z1, m=TIM, name="plain"): """Unbevelled box: twelve triangles, for the small stuff.""" return bx(min(x0, x1), max(x0, x1), min(y0, y1), max(y0, y1), min(z0, z1), max(z0, z1), m, bev=0, name=name) def xg(o, **kw): """xf that turns the grain hint of the part with it.""" g_ = o.get("grain") rx, ry, rz = kw.get("rx", 0.0), kw.get("ry", 0.0), kw.get("rz", 0.0) xf(o, **kw) if g_ is not None: grain(o, Euler((rx, ry, rz)).to_matrix() @ Vector(g_)) return o def xgs(parts, **kw): for o in parts: xg(o, **kw) return parts def plank_x(x0, x1, yc, w, z0, z1, m=PK, rng=None, ch=0.012, along=0.535): """A sawn deck board along X with chamfered top edges (20 tris), UV on one board of the 6-board T_Deck.""" rng = rng or random pts = [(yc - w / 2, z0), (yc + w / 2, z0), (yc + w / 2, z1 - ch), (yc + w / 2 - ch, z1), (yc - w / 2 + ch, z1), (yc - w / 2, z1 - ch)] o = prism("plank", pts, "YZ", x0, x1, m) me = o.data bm = bmesh.new() bm.from_mesh(me) uvl = bm.loops.layers.uv.verify() k, uo = rng.randrange(6), rng.random() for f in bm.faces: for l in f.loops: p = l.vert.co l[uvl].uv = (p.x * along + uo, (k + 0.5 + (p.y - yc) / w * 0.86) / 6) bm.to_mesh(me) bm.free() return uv_keep(o) def nut(P, p, nrm, r=0.022, h=0.014, m=IRON): p, nrm = Vector(p), Vector(nrm).normalized() P.append(cyl_between("nut", p - nrm * 0.004, p + nrm * h, r, m, v=6)) def rock(r, loc, seed=1, sc=(1, 1, 1), m="M_Concrete", sub=1, amp=0.2, name="rock"): """A faceted stone.""" bm = bmesh.new() bmesh.ops.create_icosphere(bm, subdivisions=sub, radius=r) rr = random.Random(seed) for v in bm.verts: v.co *= 1 + rr.uniform(-amp, amp) v.co = Vector((v.co.x * sc[0], v.co.y * sc[1], v.co.z * sc[2])) bmesh.ops.translate(bm, vec=Vector(loc), verts=bm.verts) o = mesh_obj(name, bm) set_mats(o, m) return uv_wall(o) def straws(P, c, n, spread, rng, L=(0.1, 0.2), z=0.004): """Loose straw lying on a floor.""" for k in range(n): a = rng.uniform(0, 2 * PI) p = Vector((c[0] + rng.uniform(-spread, spread), c[1] + rng.uniform(-spread, spread), c[2] + z)) d = Vector((math.cos(a), math.sin(a), 0)) * rng.uniform(*L) P.append(beam(p - d, p + d, 0.012, 0.006, STRAW, bev=0, name="straw")) def rope_turns(P, c, R, n=2, r=0.012, seg=6, step=0.024): """Rope lashed round a post (vertical axis through c).""" for k in range(n): P.append(torus("lash", R, r, (c[0], c[1], c[2] + (k - (n - 1) / 2) * step), "Z", seg=seg, rseg=3, m=ROPE)) def pack_part(fn, loc=(0, 0, 0), rz=0.0, scale=1.0): """Import a binary pack FBX as a part, UVs kept, under M_Pack (the atlas of the pack). Returns one object.""" before = set(bpy.data.objects) bpy.ops.import_scene.fbx(filepath=PACK + fn + ".fbx") new = [o for o in bpy.data.objects if o not in before] meshes = [o for o in new if o.type == "MESH"] bpy.context.view_layer.update() for o in meshes: mw = o.matrix_world.copy() o.data.materials.clear() o.data.materials.append(mat("M_Pack")) for p in o.data.polygons: p.material_index = 0 o.data.transform(mw) o.parent = None o.matrix_world = Matrix.Identity(4) me = o.data for l in list(me.uv_layers)[1:]: me.uv_layers.remove(l) me.uv_layers[0].name = "UVMap" for o in new: if o.type != "MESH": bpy.data.objects.remove(o) o = join(meshes, "pack_" + fn) o.data.transform(Matrix.Translation(Vector(loc)) @ Matrix.Rotation(rz, 4, "Z") @ Matrix.Scale(scale, 4)) return uv_keep(o) def scale_man(x=0.0, y=0.0, z=0.0): """The 1.3 m crew member, for the review shots only (never exported).""" cu = [cyl("sm", 0.2, 0.9, (x, y, z + 0.5), "Z", verts=10, m="M_PaintYellow"), sphere("sm", 0.16, (x, y, z + 1.14), seg=10, rings=6, m="M_PaintYellow")] for o in cu: o["preview_only"] = True return cu def build_main(builders, budgets): """argv after '--': model names, optional --quick (skip the standard preview). Review shots go to $GC_SHOTS.""" args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else [] names = [a for a in args if a in builders] or list(builders) quick = "--quick" in args os.makedirs(SHOTS, exist_ok=True) for n in names: reset() random.seed(zlib.crc32(n.encode()) & 0xffff) pv = builders[n]() or {} views = pv.pop("views", []) man = pv.pop("man", None) frame = [o.name for o in bpy.context.scene.objects if o.type == "MESH"] t = tri_count() for o in sorted(bpy.context.scene.objects, key=lambda o: o.name): print(f"[{n}] {o.type:6s} {o.name:14s} tris={tri_count([o]) if o.type == 'MESH' else '-'} loc={tuple(round(v, 3) for v in o.location)}") print(f"[{n}] tris={t} budget={budgets.get(n)} {'OVER' if t > budgets.get(n, 1 << 30) else 'ok'}") export(n) save_blend(n) if not quick: preview(n, **pv) old = common.PREVIEWS common.PREVIEWS = SHOTS for tag, kw in views: if man is not None: scale_man(*man) preview(n + tag, **{**pv, **kw, "frame": frame}) common.PREVIEWS = old print(f"[{n}] done")