Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tools] slicers wrapper #2249

Merged
merged 6 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ book
.direnv
result
.local

# Source multitile files
source
235 changes: 235 additions & 0 deletions tools/slicemt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
"""
Slice a multitile source image into individual images and put them in the parent folder
Uses path to detect tile name, dimentions, tileset name etc
Intended to run in a cloned tileset repository

I think it can be run only on Windows.
"""

import sys
import os
import fnmatch
import time
import subprocess
from pathlib import Path
from slice_multitile import main as mt_slicer
from slice_variants import main as var_slicer

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'

class sliceargs:
def __init__(self, image, width , height, tile, out):
self.image = image
self.width = width
self.height = height
self.tile = tile
self.out = out
self.no_json = False
self.append = True
self.iso = False
self.rearrange_top = None
self.rearrange_bottom = None
self.background = None


def show_exception_and_exit(exc_type, exc_value, tb):
import traceback
traceback.print_exception(exc_type, exc_value, tb)
input("Press Enter to exit.")
sys.exit(-1)


def CheckAffectedFiles(ArgPath,ArgTime):
NumberOfFiles = 0
for root, dirs, files in os.walk(ArgPath):
for name in files:
filename = os.path.join(root, name)
if os.stat(filename).st_mtime > ArgTime:
NumberOfFiles += 1
print('Number of affected files : ' + str(NumberOfFiles))
print()


sys.excepthook = show_exception_and_exit

# Set working path
if len(sys.argv) < 2:
FullPath = os.getcwd()
else:
FullPath = sys.argv[1]

PathHead, PathTail = os.path.split(FullPath)
if PathTail != 'source':
FullPath += '\\source'
print(f'Will work on : {bcolors.OKCYAN}' + FullPath + f'{bcolors.ENDC}')

# Set tile name
PathDetails = FullPath.split('\\')
CurrentTileName = PathDetails[-2]
print(f'Tile name is : {bcolors.OKCYAN}' + CurrentTileName + f'{bcolors.ENDC}')
# TODO: Probably should check it it looks like a tile name

# Check if working path contains tile dimentions, set them
PathLevel = fnmatch.filter(PathDetails,'*pngs*x*')
if len(PathLevel) > 0:
LevelParts = PathLevel[0].split('_')
Dimensions = LevelParts[-1]
DimX = Dimensions.split('x')[0]
DimY = Dimensions.split('x')[1]
else:
print(f'{bcolors.FAIL}Check folder. It has to be cloned repository.{bcolors.ENDC}')
quit()
print(f'Dimentions : {bcolors.OKCYAN}' + DimX + ' x ' + DimY + f'{bcolors.ENDC}')

# Set current tileset name
TilesetName = PathDetails[PathDetails.index('gfx')+1]
print(f'Tileset name : {bcolors.OKCYAN}' + TilesetName + f'{bcolors.ENDC}')
print('')

# Set updtset.cmd path
UpdateCmd = '\\'.join(PathDetails[:PathDetails.index('gfx')]) + '\\tools\\updtset.cmd ' + TilesetName
UpdatePath = '\\'.join(PathDetails[:PathDetails.index('gfx')]) + '\\tools\\'

OutPath = FullPath + '\\..\\'

# Detect source files for variants
StartTime = time.time()
Patterns = [ '*_var*.png' ]
print('1) Searching for variants :')
for Pattern in Patterns:
print(f' {bcolors.OKBLUE}' + Pattern + f'{bcolors.ENDC}')
for File in Path(FullPath).glob(Pattern):
PathHead, PathTail = os.path.split(File)
print(f' found : {bcolors.OKGREEN}' + PathTail + f'{bcolors.ENDC}')
ResultName = PathTail.split('.')[0].split('_var')[0]+'_var'
print(' result : ' + ResultName)
Args = sliceargs(
File, # image name
int(DimX), # width
int(DimY), # height
ResultName, # resulting filenames
OutPath # out path
)
var_slicer(Args)
CheckAffectedFiles( FullPath+'\\..\\', StartTime )

# Detect source files for simple multitile
StartTime = time.time()
Patterns = [ CurrentTileName +'.png' ]
print('2) Searching for simple multitile :')
for Pattern in Patterns:
print(f' {bcolors.OKBLUE}' + Pattern + f'{bcolors.ENDC}')
for File in Path(FullPath).glob(Pattern):
PathHead, PathTail = os.path.split(File)
print(f' found : {bcolors.OKGREEN}' + PathTail + f'{bcolors.ENDC}')
ResultName = CurrentTileName
print(' result : ' + ResultName)
Args = sliceargs(
File, # image name
int(DimX), # width
int(DimY), # height
ResultName, # resulting filenames
OutPath # out path
)
JsonCheck = OutPath + ResultName + '.json'
if (os.path.exists(JsonCheck)) :
print(f'{bcolors.WARNING} ignoring : ' + ResultName + '.json' + f'{bcolors.ENDC}')
Args.no_json = True
mt_slicer(Args)
CheckAffectedFiles( FullPath+'\\..\\', StartTime )

# Detect source files for transparent multitile
StartTime = time.time()
Patterns = [ CurrentTileName +'_t*.png' ]
print('3) Searching for transparent multitile :')
for Pattern in Patterns:
print(f' {bcolors.OKBLUE}' + Pattern + f'{bcolors.ENDC}')
for File in Path(FullPath).glob(Pattern):
PathHead, PathTail = os.path.split(File)
print(f' found : {bcolors.OKGREEN}' + PathTail + f'{bcolors.ENDC}')
ResultName = CurrentTileName + '_transparent'
print(' result : ' + ResultName)
Args = sliceargs(
File, # image name
int(DimX), # width
int(DimY), # height
ResultName, # resulting filenames
OutPath # out path
)
JsonCheck = OutPath + ResultName + '.json'
if (os.path.exists(JsonCheck)) :
print(f'{bcolors.WARNING} ignoring : ' + ResultName + '.json' + f'{bcolors.ENDC}')
Args.no_json = True
mt_slicer(Args)
CheckAffectedFiles( FullPath+'\\..\\', StartTime )

# Detect source files for seasonal multitiles
StartTime = time.time()
Patterns = [ CurrentTileName + '*_winter.png',
CurrentTileName + '*_spring.png',
CurrentTileName + '*_summer.png',
CurrentTileName + '*_autumn.png' ]
print('4) Searching for simple multitiles :')
for Pattern in Patterns:
print(f' {bcolors.OKBLUE}' + Pattern + f'{bcolors.ENDC}')
for File in Path(FullPath).glob(Pattern):
PathHead, PathTail = os.path.split(File)
Season = str(PathTail).split('.')[0].split('_')[-1]
print(' '+Season+f' : {bcolors.OKGREEN}' + PathTail + f'{bcolors.ENDC}')
ResultName = CurrentTileName + '_season_' + Season
print(' result : ' + ResultName)
Args = sliceargs(
File, # image name
int(DimX), # width
int(DimY), # height
ResultName, # resulting filenames
OutPath # out path
)
JsonCheck = OutPath + ResultName + '.json'
if (os.path.exists(JsonCheck)) :
print(f'{bcolors.WARNING} ignoring : ' + ResultName + '.json' + f'{bcolors.ENDC}')
Args.no_json = True
mt_slicer(Args)
CheckAffectedFiles( FullPath+'\\..\\', StartTime )

# Detect source files for seasonal transparent multitiles
StartTime = time.time()
Patterns = [ CurrentTileName + '*_winter_t*.png',
CurrentTileName + '*_spring_t*.png',
CurrentTileName + '*_summer_t*.png',
CurrentTileName + '*_autumn_t*.png' ]
print('5) Searching for seasonal transparent multitiles :')
for Pattern in Patterns:
print(f' {bcolors.OKBLUE}' + Pattern + f'{bcolors.ENDC}')
for File in Path(FullPath).glob(Pattern):
PathHead, PathTail = os.path.split(File)
Season = str(PathTail).split('.')[0].split('_')[-2]
print(' '+Season+f' : {bcolors.OKGREEN}' + PathTail + f'{bcolors.ENDC}')
ResultName = CurrentTileName + '_season_' + Season + '_transparent'
print(' result : ' + ResultName)
Args = sliceargs(
File, # image name
int(DimX), # width
int(DimY), # height
ResultName, # resulting filenames
OutPath # out path
)
JsonCheck = OutPath + ResultName + '.json'
if (os.path.exists(JsonCheck)) :
print(f'{bcolors.WARNING} ignoring : ' + ResultName + '.json' + f'{bcolors.ENDC}')
Args.no_json = True
mt_slicer(Args)
CheckAffectedFiles( FullPath+'\\..\\', StartTime )

input('Press Enter to update tileset...')

subprocess.Popen(UpdateCmd, shell = True, cwd = UpdatePath)
19 changes: 17 additions & 2 deletions tools/updtset.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ if errorlevel 1 (
if errorlevel 1 (
echo ERROR: No Python found!
echo If you are sure that Python is installed - please check 'path' environment variable.
echo Script will try to install Python 3.12, try to run this script again after this.
echo Script will try to install Python 3.10, try to run this script again after this.
echo.
winget install Python.Python.3.12 --disable-interactivity
winget install Python.Python.3.10 --disable-interactivity
goto stop
) else (
set APP=python
Expand All @@ -196,6 +196,8 @@ if errorlevel 1 (
if /i [!verbose!] EQU [YES] (echo - %APP% found.)
%APP% --version

pip install --upgrade pip --no-color 1>nul

pip show pyvips --no-color 1>nul
if errorlevel 1 (
if /i [!verbose!] EQU [YES] (echo - NO python 'pyvips' module found. Will try to install it.)
Expand All @@ -207,6 +209,19 @@ if errorlevel 1 (
) else (
if /i [!verbose!] EQU [YES] (echo - Python 'pyvips' module found.)
)

pip show numpy --no-color 1>nul
if errorlevel 1 (
if /i [!verbose!] EQU [YES] (echo - NO python 'numpy' module found. Will try to install it.)
%APP% -m pip install numpy --no-color >nul
if errorlevel 1 (
echo - Python 'numpy' failed to install. && goto stop
)
if /i [!verbose!] EQU [YES] (echo - Python 'numpy' installed.)
) else (
if /i [!verbose!] EQU [YES] (echo - Python 'numpy' module found.)
)

where /q %LIBVIPS_PATH%\bin:vips.exe
if errorlevel 1 (
echo ERROR^^^! No 'libvips' library found. Please refer installation manual:
Expand Down