bloxy-data-v1 / code /bloxy_data /luau_build.py
Radinkazemian's picture
Update Bloxy training code
6da8440 verified
Raw History Blame Contribute Delete
3.63 kB
"""Renders a list of parts into the compact Luau build format Bloxy learns to write.
Every build Bloxy produces uses the same small helper, so the model only has to
learn one pattern:
P(name, size, position, material, hexColor [, rotationDegrees [, transparency]])
Positions are relative to the build's origin (center of the footprint, on the
ground). The script puts the finished Model at the current camera focus so it
works when pasted into the Studio command bar.
"""
from __future__ import annotations
from dataclasses import dataclass, field
HEADER = """local V, M = Vector3.new, Enum.Material
local build = Instance.new("Model")
build.Name = "{model_name}"
local origin = workspace.CurrentCamera.Focus.Position * Vector3.new(1, 0, 1)
local function P(name, size, pos, material, color, rot, transparency)
\tlocal p = Instance.new("Part")
\tp.Name = name
\tp.Anchored = true
\tp.Size = size
\tp.Material = material
\tp.Color = Color3.fromHex(color)
\tp.TopSurface, p.BottomSurface = Enum.SurfaceType.Smooth, Enum.SurfaceType.Smooth
\tp.CFrame = CFrame.new(origin + pos)
\t\t* (rot and CFrame.Angles(math.rad(rot.X), math.rad(rot.Y), math.rad(rot.Z)) or CFrame.identity)
\tp.Transparency = transparency or 0
\tp.Parent = build
\treturn p
end
"""
FOOTER = """
build.Parent = workspace
"""
def _num(x: float) -> str:
r = round(x, 2)
if r == int(r):
return str(int(r))
return f"{r:.2f}".rstrip("0").rstrip(".")
def vec(v) -> str:
return f"V({_num(v[0])}, {_num(v[1])}, {_num(v[2])})"
@dataclass
class Part:
name: str
size: tuple[float, float, float]
pos: tuple[float, float, float]
material: str # Enum.Material item name, e.g. "WoodPlanks"
color: str # hex without '#'
rot: tuple[float, float, float] | None = None
transparency: float = 0.0
def line(self) -> str:
args = [
f'"{self.name}"',
vec(self.size),
vec(self.pos),
f"M.{self.material}",
f'"{self.color.lower()}"',
]
if self.rot or self.transparency:
args.append(vec(self.rot) if self.rot else "nil")
if self.transparency:
args.append(_num(self.transparency))
return "P(" + ", ".join(args) + ")"
@dataclass
class Build:
model_name: str
parts: list[Part] = field(default_factory=list)
sections: list[tuple[str, list[Part]]] = field(default_factory=list)
def add(self, *parts: Part) -> None:
self.parts.extend(parts)
def section(self, title: str, parts: list[Part]) -> None:
self.sections.append((title, parts))
def all_parts(self) -> list[Part]:
out = list(self.parts)
for _, ps in self.sections:
out.extend(ps)
return out
def bounds(self) -> tuple[float, float, float]:
ps = self.all_parts()
if not ps:
return (0, 0, 0)
mins = [min(p.pos[i] - p.size[i] / 2 for p in ps) for i in range(3)]
maxs = [max(p.pos[i] + p.size[i] / 2 for p in ps) for i in range(3)]
return tuple(round(maxs[i] - mins[i], 1) for i in range(3)) # type: ignore[return-value]
def to_luau(self) -> str:
lines = [HEADER.format(model_name=self.model_name)]
if self.parts:
lines.extend(p.line() for p in self.parts)
for title, ps in self.sections:
lines.append(f"\n-- {title}")
lines.extend(p.line() for p in ps)
lines.append(FOOTER)
return "\n".join(lines).strip() + "\n"
def answer(intro: str, build: Build) -> str:
return f"{intro.strip()}\n\n```lua\n{build.to_luau()}```"