Files
gymkhana-actions/scripts/generate-items.py
2023-08-26 16:56:57 +02:00

123 lines
3.2 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Example command: ./generate-items.py /home/`whoami`/Nextcloud/gynkhana/items/ ../gymkhana/items/inventory/ turno_cocina
#
import shutil, sys, glob, os, math
from pathlib import Path
from PIL import Image
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
overWrite=False
params = sys.argv
print(params)
if(len(params) < 4):
print("Missing params")
sys.exit()
inputDir = params[1]
outputDir = params[2]
itemPrefix = params[3]
def printHelp():
print ('./generate-items -i <inputDir> -o <outputDir> -p <prefix>')
def generateItem(inputFile):
baseName = itemPrefix + "_" + Path(inputFile).resolve().stem
print(f"Generating: {bcolors.BOLD}{baseName}{bcolors.ENDC}")
generateImage(inputFile, baseName)
generateESCScript(baseName)
generateScene(baseName)
def generateImage(inputFile, baseName):
originalImage = Image.open(inputFile)
biggerSide = originalImage.width
if(originalImage.width < originalImage.height):
biggerSide = originalImage.height
scale = 128 / biggerSide
#scale = 0.15
newImage = originalImage.resize((math.floor(originalImage.width * scale),math.floor(originalImage.height * scale)))
newImageOutputPath = outputDir + "assets/" + baseName + ".png"
print(f"{bcolors.OKCYAN}Image:{bcolors.ENDC} {newImageOutputPath} {bcolors.OKGREEN}generated.{bcolors.ENDC}")
newImage.save(newImageOutputPath)
def generateESCScript(baseName):
scriptName = baseName + ".esc"
scriptOutputPath = outputDir + scriptName
if(os.path.isfile(scriptOutputPath) and not(overWrite)):
print(f"{bcolors.OKCYAN}Script:{bcolors.ENDC} {scriptOutputPath} {bcolors.WARNING}already exists.{bcolors.ENDC}")
return
print(f"{bcolors.OKCYAN}Script:{bcolors.ENDC} {scriptOutputPath} {bcolors.OKGREEN}generated.{bcolors.ENDC}")
shutil.copy('item-template.esc', scriptOutputPath)
def generateScene(baseName):
esceneName = baseName + ".tscn"
esceneOutputPath = outputDir + esceneName
if(os.path.isfile(esceneOutputPath) and not(overWrite)):
print(f"{bcolors.OKCYAN}Scene:{bcolors.ENDC} {esceneOutputPath} {bcolors.WARNING}already exists.{bcolors.ENDC}")
return
newScene = open(esceneOutputPath, mode='wt', encoding='utf-8')
newSceneTemplate = esceneTemplate
newSceneTemplate = newSceneTemplate.replace("BASENAME", baseName)
newScene.write(newSceneTemplate)
newScene.close()
print(f"{bcolors.OKCYAN}Scene:{bcolors.ENDC} {esceneOutputPath} {bcolors.OKGREEN}generated.{bcolors.ENDC}")
if inputDir == "":
print ('Input directory is required')
printHelp()
sys.exit()
if outputDir == "":
print ('Output directory is required')
printHelp()
sys.exit()
if itemPrefix == "":
print ('Item prefix is required')
printHelp()
sys.exit()
if(outputDir[-1] != "/"):
outputDir += "/"
esceneFile = open('./item-template.tscn')
esceneTemplate = esceneFile.read()
files = glob.glob(inputDir + "*.png")
for file in files:
generateItem(file)
esceneFile.close()