Created
February 21, 2021 03:47
-
-
Save insomniacUNDERSCORElemon/d6a50b0e410ed1c0576998f13db0af79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#made by insomniac_lemon | |
#it works... theoretically http://i.imgur.com/2FnWB3R.jpg | |
#DIRECTIONS: | |
# | |
#1. Create tile sheet (make sure dimensions and layout are in line with the settings in this script) | |
#2. (Optional) export it somewhere (within the documents folder recommended) with a discriptive name. | |
#3. Open tile sheet in GIMP from what you exported. Alternatively, if you skipped 2, just make sure your layer has a desired/descriptive name | |
#4. Make sure the desired tile sheet is the only image open in GIMP | |
#5. Copy this script in entirety, in GIMP go to filters>python-fu>console | |
#6. Hit ctrl+v | |
#7. Contemplate your existence | |
#8. Read the green stuff outputted by the python console at the bottom to find out where your files are. Hint: it says insomniac_lemon-gimp_script in it. | |
#it should be easy enough to edit tiles_w and tiles_h and last_tile in order to split different sized atlases | |
from gimpfu import * | |
import os | |
import errno | |
def makedirectory(path): | |
try: | |
os.makedirs(path) | |
except OSError as exception: | |
if exception.errno != errno.EEXIST: | |
raise | |
home = os.path.expanduser("~") | |
def refresh(): | |
global images | |
global image | |
global layer | |
images = gimp.image_list() | |
image = images[0] | |
layer = image.layers[0] | |
refresh() | |
w = pdb.gimp_image_width(image) | |
h = pdb.gimp_image_height(image) | |
#size of optifine overlay sheet is 7x3 tiles | |
tiles_w = 7 | |
tiles_h = 3 | |
current_tile = 0 | |
#last tile for optifine overlay should be 16 | |
last_tile = 16 | |
resolution_w = w/tiles_w | |
resolution_h = h/tiles_h | |
resolution = 0 | |
print('Tile grid is ' + str(tiles_w) + 'x' + str(tiles_h)) | |
print('Resolution SHOULD BE ' + str(resolution_w) + 'x' + '...') | |
#heya if you understand python-fu and know a way I can avoid using that block if statement below, that'd be neato. | |
#I've tried quit() and sys.exit() and raise SystemExit(0) and they all exit the python console but allow all code after to run | |
#and I want the exact OPPOSITE of that save for the warning at saying that things are broken | |
if (resolution_w == resolution_h): | |
resolution = resolution_w | |
print('resolution IS ' + str(resolution) + 'x' + '! Congrats.') | |
translate = resolution*(tiles_w-1) | |
layer_name = str(pdb.gimp_layer_get_name(layer)) + '/' | |
print(layer_name) | |
file_path_base = home + '/insomniac_lemon-gimp_script/' | |
file_path_base_textures = str(file_path_base) + str(layer_name) | |
deleted = False | |
if ((os.path.exists(file_path_base_textures)) and file_path_base != home): | |
deleted = True | |
for root, dirs, files in os.walk(file_path_base_textures, topdown=False): | |
for name in files: | |
os.remove(os.path.join(root, name)) | |
for name in dirs: | |
os.rmdir(os.path.join(root, name)) | |
makedirectory(file_path_base_textures) | |
print('Made the directory' + str(file_path_base_textures)) | |
pdb.gimp_image_resize(image, resolution, resolution, 0, 0) | |
last_tile+=1 #logical fix | |
while (current_tile <= last_tile): | |
new_image = pdb.gimp_image_duplicate(image) | |
new_layer = new_image.layers[0] | |
pdb.gimp_image_crop(new_image, resolution, resolution, 0, 0) | |
filepath = str(file_path_base_textures) + str(current_tile) + '.png' | |
pdb.file_png_save2(new_image, new_layer, filepath, filepath, 0, 9, 0, 0, 0,0,0,0,0) | |
gimp.delete(new_image) | |
refresh() | |
print(current_tile) | |
current_tile += 1 | |
if (current_tile == last_tile): | |
pdb.gimp_image_resize_to_layers(image) | |
break | |
if (current_tile % tiles_w != 0): | |
print('shift right') | |
pdb.gimp_layer_translate(layer, -resolution, 0) | |
elif (current_tile % tiles_w == 0): | |
print('new line') | |
pdb.gimp_layer_translate(layer, translate, -resolution) | |
if (deleted == True): | |
print('Files were deleted!') | |
print('Tile grid is ' + str(tiles_w) + 'x' + str(tiles_h)) | |
print('resolution is ' + str(resolution) + 'x') | |
print('Look for your files in ' + str(file_path_base_textures)) | |
print('Your original image was NOT modified... but if you run the script where the folders/files it needs to create exist already, the ALL previously outputted files WILL be deleted!') | |
print('So be warned(!): These files should not be treated as an archive, get those CTM files somewhere else!') | |
print('done') | |
if (resolution_w != resolution_h): | |
print('Woah there hoss, this image does not match the' + '\n' + 'specified tile grid!') | |
print('you can ignore me, I am but a humble pawn to make sure the line before me runs') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment