Created
May 31, 2016 05:43
-
-
Save thecodewarrior/65b806f4340fdac207340ed5413e3679 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 | |
import math, os, re | |
from gimpfu import * | |
MERGE_MODE = 1 | |
PATH_PREFIX = "!pth " | |
FILE_PREFIX = "!file " | |
TEXTURE_PREFIX = "!!" | |
REFERENCE_PREFIX = "!ref" | |
def log(str): | |
print(str) | |
pdb.gimp_message(str) | |
def msg(str): | |
print(">" + str) | |
pdb.gimp_progress_set_text(str) | |
def format_filename(filepath, layer): | |
if not pdb.gimp_item_is_group(layer): | |
return None | |
if not filepath.endswith("/"): | |
filepath = filepath + "/" | |
filename = None | |
for check in layer.layers: | |
if check.name.startswith(FILE_PREFIX): | |
filename = check.name[len(FILE_PREFIX):] | |
if filename == None: | |
msg("No filename '" + FILE_PREFIX + " [NAME]'") | |
return filepath + filename + ".png" | |
def get_groups(layers): | |
result = [] | |
for layer in layers: | |
print("Testing " + layer.name) | |
if pdb.gimp_item_is_group(layer): | |
print(" it's a group") | |
if layer.name.startswith(TEXTURE_PREFIX): | |
print(" it's name matches") | |
result.append(layer) | |
return result | |
def export_layers(img, drawable): | |
msg("Starting") | |
dupe = img.duplicate() | |
groups = get_groups(dupe.layers); | |
path = None | |
msg("Found " + str(len(groups)) + " groups") | |
for layer in dupe.layers: | |
if layer.name.startswith(PATH_PREFIX): | |
path = layer.name[len(PATH_PREFIX):] | |
if path == None: | |
msg("No path layer! '" + PATH_PREFIX + "[DIR]'") | |
return | |
if not path.endswith("/"): | |
path = path + "/" | |
for layer in dupe.layers: | |
if layer.name.startswith(REFERENCE_PREFIX): | |
layer.visible = 0 | |
for group in groups: | |
group.visible = 0 | |
for group in groups: | |
msg("Exporting " + group.name + "..."); | |
group.visible = 1 | |
boundlayer = None | |
for layer in group.layers: | |
if layer.name.startswith(FILE_PREFIX): | |
boundlayer = layer | |
if boundlayer == None: | |
msg("No bounds layer! '" + FILE_PREFIX + "[*]'") | |
continue | |
boundlayer.visible = 0 | |
log("Bounds: " + str(boundlayer.width) + "x" + str(boundlayer.height) + " @ " + str(boundlayer.offsets[0]) + "x" + str(boundlayer.offsets[1])) | |
filename = format_filename(path, group) | |
tmp = dupe.duplicate() | |
tmp.resize(boundlayer.width, boundlayer.height, -boundlayer.offsets[0], -boundlayer.offsets[1]) | |
layer = tmp.merge_visible_layers(MERGE_MODE) | |
subpath = os.path.dirname(filename) | |
if not os.path.exists(subpath): | |
os.makedirs(subpath) | |
log(" Exporting...") | |
pdb.file_png_save_defaults(tmp, layer, filename, filename) | |
log(" Exported.") | |
group.visible = 0 | |
msg("Done.") | |
register( | |
"export_folders", | |
"Export folders as images", | |
"Folder name format - 'EX (name) (W)x(H) (X)x(Y)", | |
"Pierce Corcoran", | |
"Pierce Corcoran", | |
"2016", | |
"<Image>/Tools/Export Folders...", | |
"*", | |
[], | |
[], | |
export_layers) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment