-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcg_export.py
139 lines (126 loc) · 6.27 KB
/
tcg_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
#
# TCG Export - GIMP plug-in that exports a template as separate images with layers' values replaced by data from a CSV file
#
# Copyright (C) 2022 christopher king <[email protected]>
#
# TCG Export is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TCG Export is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TCG Export. If not, see <https://www.gnu.org/licenses/>.
import os
import csv
from gimpfu import *
def find_layer_by_name (image, name):
for layer in image.layers:
if layer.name == name:
return layer
if(pdb.gimp_item_is_group(layer)):
potential = find_layer_name_in_group(layer, name)
if(potential != None):
return potential
return None
def find_layer_name_in_group(layerGroup, name):
gr = layerGroup
gr_items = pdb.gimp_item_get_children(gr)
for index in gr_items[1]:
item = gimp.Item.from_id(index)
if item.name == name:
return item
elif(pdb.gimp_item_is_group(item)):
potential = find_layer_name_in_group(item, name)
if(potential != None):
return potential
return None
def plugin_main(timg, tdrawable, data_dir, data_out, data_csv):
pdb.gimp_image_undo_group_start(timg)
pdb.gimp_message(data_csv)
with open(data_csv) as csvfile:
header = csvfile.readline()
#parse header row
columns = header.strip().split(",")
pdb.gimp_message(columns)
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
#for each row
for row_index, row in enumerate(spamreader):
#for each column
pdb.gimp_message(row)
name = None
for col_index, col in enumerate(columns):
#check if there is a layer with that header name
layer = find_layer_by_name(timg, col)
if(col == "name"):
name = row[col_index]
if layer != None:
pdb.gimp_message("Layer index " + layer.name)
pdb.gimp_image_set_active_layer(timg, layer)
path = os.path.join(data_dir, col)
#if there exists a folder in the directory with the same name as the column, the value is a file name
if row[col_index] != None and row[col_index] != '' and os.path.exists(path):
pdb.gimp_message("found an asset folder for " + col)
pdb.gimp_message("Loading file " + row[col_index])
img_path = os.path.join(path, row[col_index])
new_asset = pdb.gimp_file_load_layer(timg, img_path)
if new_asset != None:
#add the new image, scaled to the right height and in the correct position and merge it down
new_asset.name = col
opacity_before = layer.opacity
timg.add_layer(new_asset, -1)
pdb.gimp_message("Height: " + str(new_asset.height))
scalefactor = new_asset.width / float(layer.width)
pdb.gimp_message("Scale: " + str(scalefactor))
extraheight = ((new_asset.height / scalefactor) - layer.height) / 2
pdb.gimp_item_transform_scale(new_asset, layer.offsets[0], layer.offsets[1] - extraheight, layer.width + layer.offsets[0], layer.height + layer.offsets[1] + extraheight)
pdb.gimp_layer_resize_to_image_size(new_asset)
layer = pdb.gimp_image_merge_down(timg, new_asset, 2)
layer.opacity = opacity_before
#if the value in the layer is a color, recolor the layer
elif row[col_index] != None and row[col_index] != '' and row[col_index].find("#") != -1:
pdb.gimp_message("found a color for " + col)
pdb.gimp_selection_none(timg)
pdb.gimp_image_select_item(timg, 2, layer)
pdb.gimp_context_set_foreground(row[col_index])
pdb.gimp_drawable_edit_fill(layer,FILL_FOREGROUND)
pdb.gimp_selection_none(timg)
#it is a text layer
else:
try:
pdb.gimp_message("text layer " + col)
pdb.gimp_text_layer_set_text(layer, row[col_index])
except:
pdb.gimp_message("nothing set")
#end of each column processing, template ready for export
if(name != None):
new_image = pdb.gimp_image_duplicate(timg)
layer = pdb.gimp_image_merge_visible_layers(new_image, CLIP_TO_IMAGE)
pdb.gimp_file_save(new_image, layer, os.path.join(data_out, name + '.png'), '?')
pdb.gimp_image_delete(new_image)
pdb.gimp_image_undo_group_end(timg)
register(
"python_fu_tcg",
"Loads in a CSV file and a directory of art assets and exports a playing card using this template",
"Loads in a CSV file and a directory of art assets and exports a playing card using this template",
"Christopher King",
"Christopher King",
"2022",
"<Image>/File/Export TCG files",
"RGB*, GRAY*",
[
#get the project directory
(PF_FILE, "data_dir", "Project Directory", "/"),
#get the output directory
(PF_FILE, "data_out", "Output Directory", "/"),
#get the csv file
(PF_FILE, "data_csv", "Card Data CSV", "")
],
[],
plugin_main)
main()