Files
pack
build_pack.py
README.md
requirements.txt
Code
14 325
build_pack.py
from pathlib import Path
import shutil
import zipfile
from PIL import Image, ImageDraw
PROJECT_ROOT = Path(__file__).resolve().parent
PACK_SOURCE = PROJECT_ROOT / "pack"
SOURCE_IMAGE = PROJECT_ROOT / "source" / "weapons.png"
BUILD_DIRECTORY = PROJECT_ROOT / "build" / "Arcane_Weapon_Variations"
ZIP_FILE = PROJECT_ROOT / "Arcane_Weapon_Variations.zip"
TEXTURE_DIRECTORY = BUILD_DIRECTORY / "assets" / "minecraft" / "textures" / "item"
TEXTURE_CROPS = {
"custom_item_1": (5, 5, 507, 508),
"custom_item_2": (519, 5, 1020, 508),
"custom_item_3": (1032, 5, 1534, 508),
"custom_item_4": (241, 521, 765, 1018),
"custom_item_5": (777, 521, 1269, 1018),
}
PLACEHOLDER_COLORS = {
"custom_item_1": (255, 84, 20, 255),
"custom_item_2": (30, 177, 255, 255),
"custom_item_3": (98, 219, 255, 255),
"custom_item_4": (221, 36, 57, 255),
"custom_item_5": (143, 50, 255, 255),
}
def create_placeholder(texture_name: str) -> Image.Image:
image = Image.new("RGBA", (128, 128), (0, 0, 0, 0))
draw = ImageDraw.Draw(image)
color = PLACEHOLDER_COLORS[texture_name]
outline = (30, 30, 40, 255)
if texture_name in {"custom_item_1", "custom_item_3"}:
draw.polygon([(20, 110), (42, 110), (110, 22), (92, 12)], fill=outline)
draw.polygon([(31, 105), (43, 105), (103, 25), (91, 21)], fill=color)
draw.rectangle((16, 96, 45, 108), fill=outline)
draw.rectangle((20, 98, 40, 105), fill=color)
elif texture_name == "custom_item_2":
draw.arc((18, 10, 112, 116), 280, 80, fill=outline, width=13)
draw.arc((18, 10, 112, 116), 280, 80, fill=color, width=7)
draw.line((31, 95, 104, 32), fill=outline, width=6)
draw.line((34, 91, 100, 35), fill=(220, 245, 255, 255), width=2)
else:
draw.line((25, 111, 91, 45), fill=outline, width=18)
draw.line((27, 109, 87, 49), fill=color, width=10)
draw.polygon([(67, 16), (116, 22), (111, 70), (76, 80)], fill=outline)
draw.polygon([(74, 23), (108, 28), (104, 62), (80, 70)], fill=color)
return image
def create_texture(texture_name: str, source: Image.Image | None) -> None:
destination = TEXTURE_DIRECTORY / f"{texture_name}.png"
if source is None:
texture = create_placeholder(texture_name)
else:
texture = source.crop(TEXTURE_CROPS[texture_name]).convert("RGBA")
texture.thumbnail((256, 256), Image.Resampling.LANCZOS)
canvas = Image.new("RGBA", (256, 256), (0, 0, 0, 0))
position = ((256 - texture.width) // 2, (256 - texture.height) // 2)
canvas.alpha_composite(texture, position)
texture = canvas
texture.save(destination, "PNG")
def create_pack_icon(source: Image.Image | None) -> None:
destination = BUILD_DIRECTORY / "pack.png"
if source is None:
icon = create_placeholder("custom_item_1")
else:
icon = source.crop(TEXTURE_CROPS["custom_item_1"]).convert("RGBA")
icon.thumbnail((128, 128), Image.Resampling.LANCZOS)
icon.save(destination, "PNG")
def write_zip() -> None:
if ZIP_FILE.exists():
ZIP_FILE.unlink()
with zipfile.ZipFile(ZIP_FILE, "w", zipfile.ZIP_DEFLATED) as archive:
for path in BUILD_DIRECTORY.rglob("*"):
if path.is_file():
archive.write(path, path.relative_to(BUILD_DIRECTORY))
def main() -> None:
if BUILD_DIRECTORY.exists():
shutil.rmtree(BUILD_DIRECTORY)
shutil.copytree(PACK_SOURCE, BUILD_DIRECTORY)
TEXTURE_DIRECTORY.mkdir(parents=True, exist_ok=True)
source = Image.open(SOURCE_IMAGE).convert("RGBA") if SOURCE_IMAGE.exists() else None
for texture_name in TEXTURE_CROPS:
create_texture(texture_name, source)
create_pack_icon(source)
write_zip()
source_status = "cropped the supplied weapon sheet" if source else "generated fallback placeholder textures"
print(f"Created {ZIP_FILE.name} and {source_status}.")
if __name__ == "__main__":
main()